Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. - - PowerPoint PPT Presentation

chapter 4 control structures i selection
SMART_READER_LITE
LIVE PREVIEW

Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. - - PowerPoint PPT Presentation

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 4: Control Structures I (Selection) C++ Programming 1 Dr. Adriana Badulescu Kallas Introduction to Computer Science &


slide-1
SLIDE 1

1

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Chapter 4: Control Structures I (Selection)

slide-2
SLIDE 2

2

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Objectives

Learn about control structures Examine relational and logical

  • perators

Explore how to form and evaluate logical (Boolean) expressions Discover how to use the selection control structures if, if...else, and switch in a program Learn how to avoid bugs by avoiding partially understood concepts Learn to use the assert function to terminate a program

slide-3
SLIDE 3

3

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Control Structures

A computer can proceed:

In sequence Selectively (branch): making a choice Repetitively (iteratively): looping

Some statements are executed only if certain

conditions are met

A condition is met if it evaluates to true

slide-4
SLIDE 4

4

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Control Structures

false true

Expression

Statement false true

Expression

Statement1 Statement2 Statement1 Statement2 Statement3

Sequence Selection Repetition

slide-5
SLIDE 5

5

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Relational Operators

A condition is represented by a logical (Boolean)

expression that can be true or false

Relational operators:

Allow comparisons between two operands (binary) Evaluate to true or false

slide-6
SLIDE 6

6

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Relational Operators and Simple Data Types

You can use the relational operators with all

three simple data types:

(8 < 15)

evaluates to true

(6 != 6)

evaluates to false

(2.5 > 5.8)

evaluates to false

(5.9 <= 7.5)

evaluates to true

(‘a’ == 97)

evaluates to true

(97.0 == 97)

evaluates to true

(‘A’ > ‘a’)

evaluates to false(65<97)

(a = ‘0’)

evaluates to true

slide-7
SLIDE 7

7

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Relational Operators and Simple Data Types

Logical (Boolean) expressions

Expression with relational operators Returns an integer value of 1 if the logical

expression evaluates to true

Returns an integer value of 0 otherwise

cout << (‘a’ == 97); cout << (‘a’ > ‘z’); cout << (97.0 == 97); 101

slide-8
SLIDE 8

8

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Relational Operators and the string Type

  • Relational operators can be applied to strings
  • Strings are compared character by character,

starting with the first character

  • Comparison continues until either a mismatch is

found or all characters are found equal

  • If two strings of different lengths are compared

and the comparison is equal to the last character

  • f the shorter string

–The shorter string is less than the larger string

slide-9
SLIDE 9

9

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

“Hello” < “Hi” true “Hello” > “Hen” false “Air” < “An” true “Hello” == “hello” false “Air” <= “Bill” true “Hi” > “Bill” true “Hello” > “hello” false “Bill” >= “Billy” false “Big” <= “Bigger” true

Relational Operators and the string Type

slide-10
SLIDE 10

10

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Logical Operators and Logical Expressions

Logical (Boolean) operators enable you to

combine logical expressions

slide-11
SLIDE 11

11

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Logical Operators and Logical Expressions

The negation (!) operator

slide-12
SLIDE 12

12

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Logical Operators and Logical Expressions

The AND (&&) Operator

slide-13
SLIDE 13

13

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Logical Operators and Logical Expressions

The OR ( || ) Operator

slide-14
SLIDE 14

14

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Logical Operators and Logical Expressions

Logical Operators AND (&&) and OR (||) have

2 symbols

If you use only one symbol, you will get the

Bitwise operator: Bitwise AND (&) and Bitwise OR (|) which are done per bit and lead to different results

1 && 2 -> 1 1 & 2 -> 0

1= 0 0 0 0 0 0 0 1 2= 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 =0

slide-15
SLIDE 15

15

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Order of Precedence

Relational and logical operators are evaluated

from left to right

The associativity is left to right Parentheses can override precedence

slide-16
SLIDE 16

16

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Order of Precedence

  • 1. + (positive) , - (negative), ! (not)
  • 2. ++ (increment), -- (decrement)
  • 3. * (multiplication), / (division), % (modulus)
  • 4. + (addition), - (subtraction)
  • 5. <, <=, >, >= (relational comparison)
  • 6. == (equal-to), != (not-equal-to)
  • 7. && (and)
  • 8. || (or)
  • 9. = (assignment), +=, -=, *=, /=, %= (compound assignment)
slide-17
SLIDE 17

17

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

9 && 8 == 7 < 0 / 4 + 1 - 1

Order of Precedence

9 && 8 == 7 < !6 * - 5 / 4 + 3 % 2 - 1 9 && 8 == 7 < 0 * (- 5) / 4 + 3 % 2 - 1 9 && 8 == 7 < 9 && 8 == 9 && 9 && 8 == 7 < 0 + 1 - 1 x = x = x = x = x = x = x = x =

slide-18
SLIDE 18

18

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

int and bool Data Type and Logical Expressions

Logical expressions evaluate to either true (=1) or

false (=0)

The data type bool has logical (Boolean) values true

and false

bool legalAge = (age >= 21);

Earlier versions of C++ did not provide built-in data types

that had Boolean values, so the int data type was used to manipulate logical (Boolean) expressions

int legalAge = (age >= 21);

slide-19
SLIDE 19

19

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Selection: if and if...else

One-Way Selection Two-Way Selection Compound (Block of) Statements Multiple Selections: Nested if Comparing if...else Statements with a

Series of if Statements

slide-20
SLIDE 20

20

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

One-Way Selection

The syntax is:

The Statement is executed if the value of the Expression is true The Statement is bypassed if the value is false and the

program goes to the next statement if (Expression) Statement

false true

Expression Statement

slide-21
SLIDE 21

21

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Examples:

if (score >= 60); grade = ‘P’; if score >= 60 grade = ‘P’; grade = ‘F’; if (score >= 60) grade = ‘P’;

One-Way Selection

CORRECT – but we do need the grade = ‘F’; assignment before the if to make sure the grade will have a value if the score is lower than 60 INCORRECT – missing parentheses SYNTACTICALLY CORRECT – it has a valid if with an empty statement – that does not do (if (score >=

60);) , followed by an assignment grade = ‘P’ but

LOGICALLY INCORRECT – it will make the grade =

‘P’ for any score so you need to delete the ; at the

end of the first line

if (score >= 60) grade = ‘P’;

SYNTACTICALLY CORRECT but LOGICALLY INCORRECT – it will make the grade = ‘P’ for scores larger than 60 but will not assign any value for scores smaller than 60

slide-22
SLIDE 22

22

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

One-Way Selection

slide-23
SLIDE 23

23

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Two-Way Selection

If expression is true,

Statement1 is executed;

  • therwise, Statement2 is

executed if (Expression) Statement1 else Statement2

false true

Expression Statement1 Statement2

slide-24
SLIDE 24

24

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Two-Way Selection

Example:

if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours – 40.0); else wages = hours * rate;

slide-25
SLIDE 25

25

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Compound (Block of) Statements

Compound statement (block of statements): A compound statement is a single statement

{ Statement1; Statement2; … StatementN; }

Statement1 Statement2 StatementN

slide-26
SLIDE 26

26

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Compound (Block of) Statements

Example:

if (age > 18) { cout << "Eligible to vote." << endl; cout << "No longer a minor." << endl; } else { cout << "Not eligible to vote." << endl; cout << "Still a minor." << endl; }

slide-27
SLIDE 27

27

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Multiple Selections: Nested if

Nesting: one control statement

in another

An else is associated with the

most recent if that has not been paired with an else

You can nest control statement

in compound statements to remove some of the ambiguity

  • r organize your code

if (expression1) statement1; else if (expression2) statement2; else statement3; if (expression1) statement1; else { if (expression2) statement2; else statement3; }

slide-28
SLIDE 28

28

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Multiple Selections: Nested if

slide-29
SLIDE 29

29

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Multiple Selections: Nested if

slide-30
SLIDE 30

30

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Comparing if…else Statements with a Series of if Statements

slide-31
SLIDE 31

31

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Short-Circuit Evaluation

Short-circuit evaluation: evaluation of a logical

expression stops as soon as the value of the expression is known

Example:

(age >= 21) || ( x == 5) (grade == 'A') && (x >= 7)

slide-32
SLIDE 32

32

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Comparing Floating-Point Numbers for Equality: A Precaution

Comparison of floating-point numbers for equality may

not behave as you would expect

Math:

  • =
  • =
  • =1

C++:

int: 3/7 + 2/7 + 2/7 = 0 !=1 float: 3.0/7.0 + 2.0/7.0 + 2.0/7.0 = 0.42857142857+0.28571428571+0.28571428571 = 0.9999994 !=1

Use a tolerance value fabs(x – y) < 0.000001

slide-33
SLIDE 33

33

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Associativity of Relational Operators: A Precaution

slide-34
SLIDE 34

34

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Associativity of Relational Operators: A Precaution

0<=num<=10 0<=num && num<=10

  • 5

5 15 0<=-5 <=10 0<=5 <=10 0<=15<=10 0 <=10 1 <=10 1 <=10 1 1 1 0 10

Always

true

  • 5

5 15 0<=-5 && -5<=10 0<=5 && 5<=10 0<=15 && 15<=10 0 && 1 1 && 1 1 && 0 1 0 10 true

  • nly if

between 0 and 10

slide-35
SLIDE 35

35

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques

Must use concepts and techniques correctly;

Otherwise solution will be either incorrect or

deficient

If you do not understand a concept or technique

completely

Don’t use it Save yourself an enormous amount of

debugging time

slide-36
SLIDE 36

36

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Input Failure and the if Statement

If input stream enters a fail state

– All subsequent input statements associated with that

stream are ignored

– Program continues to execute – May produce erroneous results

Can use if statements to check status of input

stream

If stream enters the fail state, include

instructions that stop program execution

slide-37
SLIDE 37

37

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Confusion Between the Equality (==) and Assignment (=) Operators

C++ allows you to use any expression that can be

evaluated to either true or false as an expression in the if statement: (x=5) or (x==5)

The appearance of ‘=‘ in place of ‘==‘ resembles a

silent killer

It is not a syntax error, it is a logical error

if (x = 5) cout << "The value is five." << endl; else cout << "The value is five." << endl;

slide-38
SLIDE 38

38

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Conditional Operator (?:)

Conditional operator (?:) takes three arguments

(ternary operator)

Syntax for using the conditional operator:

expression1 ? expression2 : expression3

If expression1 is true, the result of the

conditional expression is expression2

Otherwise, the result is expression3

max = (a >= b) ? a : b;

slide-39
SLIDE 39

39

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Conditional Operator (?:)

With conditional operator:

variable = expression1 ? expression2 : expression3

With if…else statement:

if (expression1) variable = expression2; else variable = expression3;

Example:

max = (a >= b) ? a : b;

slide-40
SLIDE 40

40

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Conditional Operator (?:)

With conditional operator:

cout << expression1 ? expression2 : expression3

With if…else statement:

if (expression1) cout << expression2; else cout << expression3;

Example:

cout << ( (a >= b) ? a : b );

slide-41
SLIDE 41

41

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Program Style and Form (Revisited): Indentation

If your program is properly indented

– Spot and fix errors quickly – Show the natural grouping of statements

Insert a blank line between statements that are

naturally separate

Two commonly used styles for placing braces

– On a line by themselves – Or left brace is placed after the expression, and

the right brace is on a line by itself

slide-42
SLIDE 42

42

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Using Pseudocode to Develop, Test, and Debug a Program

Pseudocode, or just pseudo

Informal mixture of C++ and ordinary language Helps you quickly develop the correct structure

  • f the program and avoid making common

errors

Use a wide range of values in a walk-through to

evaluate the program

slide-43
SLIDE 43

43

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

switch Structures

  • switch structure:

alternate to if-else

  • switch (integral)

Expression is evaluated first

  • Value of the expression

determines which corresponding action is taken

  • Expression is sometimes

called the selector

switch (Expression) { case Value1: Statement1 break; case Value2: Statement2 break; … case ValueN: StatementN break; default: Statements }

slide-44
SLIDE 44

44

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming false true

Expression ==Value1 Expression ==Value1 Statement1

false true

Expression ==Value2 Expression ==Value2 Statement2

false true

Expression ==ValueN Expression ==ValueN StatementN

Statements

switch Structures

slide-45
SLIDE 45

45

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

switch Structures

One or more statements may follow a case label Braces are not needed to turn multiple

statements into a single compound statement

The break statement may or may not appear

after each statement

switch, case, break, and default are reserved

words

slide-46
SLIDE 46

46

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

switch Structures

slide-47
SLIDE 47

47

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques: Revisited

A missing break statement will cause the next

statement to be called (even if the case value is met)

To output results correctly

The switch structure must include a break

statement after each cout statement

slide-48
SLIDE 48

48

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Terminating a Program with the assert Function

Certain types of errors that are very difficult to

catch can occur in a program

Example: division by zero can be difficult to

catch using any of the programming techniques examined so far

quotient=numerator / denominator;

slide-49
SLIDE 49

49

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

The assert Function

  • The predefined function, assert, is useful in

stopping program execution when certain elusive errors occur

  • Syntax:
  • Expression is any logical expression
  • If Expression evaluates to true, the next statement

executes

  • If Expression evaluates to false, the program

terminates and indicates where in the program the error

  • ccurred
  • To use assert, include cassert header file

assert (Expression);

slide-50
SLIDE 50

50

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

The assert Function

assert is useful for enforcing programming

constraints during program development

After developing and testing a program, remove or

disable assert statements

Place the preprocessor directive #define NDEBUG

before the directive #include <cassert>

#define NDEBUG #include <cassert> assert(denominator); //stops if denominator is 0 quotient = numerator / denominator;

slide-51
SLIDE 51

51

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

This programming example calculates a

customer’s bill for a local cable company

There are two types of customers:

Residential Business

Two rates for calculating a cable bill:

One for residential customers One for business customers

slide-52
SLIDE 52

52

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

Rates

  • For residential customer:

– Bill processing fee: $4.50 – Basic service fee: $20.50 – Premium channel: $7.50 per channel

  • For business customer:

– Bill processing fee: $15.00 – Basic service fee: $75.00 for first 10 connections/$5.00 for

each additional one

– Premium channel cost: $50.00 per channel for any number of

connections

slide-53
SLIDE 53

53

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

Requirements

Ask user for account number and customer code Assume R or r stands for residential customer and

B or b stands for business customer

slide-54
SLIDE 54

54

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

  • Input:

– Customer account number – Customer code – Number of premium channels – For business customers, number of basic service

connections

  • Output:

– Customer’s account number – Billing amount

slide-55
SLIDE 55

55

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

Program Analysis

  • Purpose: calculate and print billing amount
  • Calculating billing amount requires:

– Customer for whom the billing amount is calculated (residential or

business)

– Number of premium channels to which the customer subscribes – Bill processing fees and the cost of a premium channel

  • For a business customer, you need:

– Number of basic service connections – Number of premium channels

– The program should print the billing amount to two decimal

places

slide-56
SLIDE 56

56

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

Algorithm Design

  • Set precision to two decimal places
  • Prompt user for account number and customer type
  • If customer type is R or r

– Prompt user for number of premium channels – Compute and print the bill

  • If customer type is B or b

– Prompt user for number of basic service connections and

number of premium channels

– Compute and print the bill

slide-57
SLIDE 57

57

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Variables and Named Constants

Programming Example: Cable Company Billing

slide-58
SLIDE 58

58

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

Formula for billing for residential

customers:

amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels * RES_COST_PREM_CHANNEL;

slide-59
SLIDE 59

59

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

Formula for billing for business customers:

if (numOfBasicServConn <= 10) amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL; else amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfBasicServConn - 10) * BUS_BASIC_CONN_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL;

slide-60
SLIDE 60

60

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

  • Algorithm
  • 1. Output floating-point numbers in fixed decimal

with decimal point and trailing zeros

Output floating-point numbers with two decimal places

and set the precision to two decimal places

  • 2. Prompt user to enter account number
  • 3. Get customer account number
  • 4. Prompt user to enter customer code
  • 5. Get customer code
slide-61
SLIDE 61

61

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

  • 6. If the customer code is r or R,

Prompt user to enter number of premium

channels

Get the number of premium channels Calculate the billing amount Print account number and billing amount

slide-62
SLIDE 62

62

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Programming Example: Cable Company Billing

  • 7. If customer code is b or B,

– Prompt user to enter number of basic service

connections

– Get number of basic service connections – Prompt user to enter number of premium channels – Get number of premium channels – Calculate billing amount – Print account number and billing amount

  • 8. If customer code is other than r, R, b, or B, output an

error message

slide-63
SLIDE 63

63

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Summary

Control structures alter normal control flow Most common control structures are selection and repetition Relational

  • perators: ==, <,

<=, >, >=, != Logical expressions evaluate to 1 (true) or 0 (false) Logical operators: ! (not), && (and), || (or)

slide-64
SLIDE 64

64

  • Dr. Adriana Badulescu Kallas

Introduction to Computer Science & Programming Programming Fundamentals I C++ Programming

Summary

Two selection structures: one-way selection and two- way selection The expression in an if or if...else structure is usually a logical expression A sequence of statements enclosed between braces, { and }, is called a compound statement or block of statements Using assignment in place of the equality

  • perator creates a

semantic error switch structure handles multiway selection break statement ends switch statement Use assert to terminate a program if certain conditions are not met