usi ng namespace st d; i nt mai n( ) { i nt gr ade;
cout <<" Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n" ; br eak; case 8: cout <<" B\ n" ; br eak; case 7: cout <<" C\ n" ; br eak; case 6: cout <<" D\ n" ; br eak; def aul t : cout <<" F\ n" ; } r et ur n 0; } What will happen if the user enters invalid grade? i nt gr ade; cout <<" Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
/* can grade be less than zero and greater than 100 simultaneously?*/
i f ( gr ade<0 gr ade>100) { cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n" ; ci n>>gr ade; }
swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n" ; br eak; case 8: cout <<" B\ n" ; br eak; case 7: cout <<" C\ n" ; br eak; case 6: cout <<" D\ n" ; br eak; def aul t : cout <<" F\ n" ; } What need to be here? why?!
i nt gr ade; cout <<" Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
i f ( gr ade<0 | | gr ade>100) { cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n" ; ci n>>gr ade; }
swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n" ; br eak; case 8: cout <<" B\ n" ; br eak; case 7: cout <<" C\ n" ; br eak; case 6: cout <<" D\ n" ; br eak; def aul t : cout <<" F\ n" ; }
i nt gr ade; cout <<"Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
i f ( gr ade<0 | | gr ade>100) {cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n"; ci n>>gr ade; }
i f ( gr ade<0 | | gr ade>100) {cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n"; ci n>>gr ade; }
i f ( gr ade<0 | | gr ade>100) {cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n"; ci n>>gr ade; } / * af t er N i f st at ement , t he swi t ch wi l l st i l l be eval uat ed */ swi t ch( gr ade/ 10) { . . . } Loops (Iterative Constructs) Loops allow a code segment to be executed many times. Three constructs while statement for statement do-while statement The whi l e Statement
while (condition) { Action; } How it works: if condition is true then execute Action repeat this process until condition evaluates to false Action is either a single statement or a group of statements within braces. condition Action true false While Loop Syntax The whi l e Loop while (it's raining) { <keep the umbrella up> } Slide 11 Expression provides an entry condition Statement executes if the expression initially evaluates to true Loop condition is then reevaluated Statement continues to execute until the expression is no longer true If statement will be executed only once, while statement will continue repeating while the condition is true. The whi l e Loop (continued)
i nt gr ade; cout <<" Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
whi l e ( gr ade<0 | | gr ade>100) { cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n" ; ci n>>gr ade; }
swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n" ; br eak; case 8: cout <<" B\ n" ; br eak; case 7: cout <<" C\ n" ; br eak; case 6: cout <<" D\ n" ; br eak; def aul t : cout <<" F\ n" ; }
i nt gr ade; cout <<" Pl ease ent er your gr ade\ n" ; ci n>>gr ade; whi l e ( gr ade<0 | | gr ade>100) { cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n" ; } swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n" ; br eak; case 8: cout <<" B\ n" ; br eak; case 7: cout <<" C\ n" ; br eak; case 6: cout <<" D\ n" ; br eak; def aul t : cout <<" F\ n" ; } What will happen here? Slide 14 The whi l e Loop (continued) Infinite loop: continues to execute endlessly Can be avoided by including statements in the loop body that assure exit condition will eventually be false
Slide 15 Initial Value In previous code, the initial value is set by the user using cin>>grade; While (condition) (grade < 0 || grade > 100) Update Value Infinite loops may generate when you forget to update the variable used in the condition Three Things to remember
i nt gr ade; cout <<" Pl ease ent er your gr ade\ n" ; ci n>>gr ade; whi l e ( gr ade<0 | | gr ade>100) { cout <<" pl ease r e- ent er val i d gr ade bet ween 0- 100\ n" ; ci n>>gr ade; } swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n" ; br eak; case 8: cout <<" B\ n" ; br eak; case 7: cout <<" C\ n" ; br eak; case 6: cout <<" D\ n" ; br eak; def aul t : cout <<" F\ n" ; } Flowchart whi l e Loop Slide 18 Two ways to use while loop Sentinel while: If you dont know how many times you have to loop Use Sentinel value Indicates end of data entry Variable is tested in the condition and loop ends when sentinel is encountered Sentinel value chosen so it cannot be confused with a regular input (such as -1)
The whi l e Loop Slide 19 Two ways to use while loop counter-controlled while: If you know exactly how many times you need to repeat Loop repeated until counter reaches a certain value
The whi l e Loop Slide 20
counter-controlled while: If you need to loop specific times Loop repeated until counter reaches a certain value
counter-controlled while: Example: write a program that calculates the average of five grades
counter-controlled while: Example Counter while loop FlowChart Start Sum=0 Count = 1 Input Grade Count <=5 Sum = Sum + Grade Count = Count + 1 Average = Sum/(Count-1) Stop No Yes
i nt mai n( ) { i nt gr ade;
cout <<"Pl ease ent er t he gr ade" <<endl ; ci n>>gr ade;
r et ur n 0; }
i nt mai n( ) { i nt gr ade, Sum=0;
cout <<"Pl ease ent er t he gr ade" <<endl ; ci n>>gr ade;
Sum=Sum+gr ade;
r et ur n 0; } What will happen if variable Sum is not initialized ?!
i nt mai n( ) { i nt gr ade, Sum=0, Aver age;
whi l e( ) { cout <<"Pl ease ent er t he gr ade" <<endl ; ci n>>gr ade; Sum=Sum+gr ade; }
r et ur n 0; } What should be here?!
i nt mai n( ) { i nt gr ade, count =1, Sum=0, Aver age;
whi l e( count <=5) { cout <<"Pl ease ent er t he gr ade" <<endl ; ci n>>gr ade; Sum=Sum+gr ade; count =count +1; }
r et ur n 0; }
i nt mai n( ) { i nt gr ade, count =1, Sum=0, Aver age;
whi l e( count <=5) { cout <<"Pl ease ent er t he gr ade" <<endl ; ci n>>gr ade; Sum=Sum+gr ade; count =count +1; }
Aver age=Sum/ ; cout <<"The aver age i s " <<Aver age;
r et ur n 0; } What do we need here!
i nt mai n( ) { i nt gr ade, count =1, Sum=0, Aver age;
whi l e( count <=5) { cout <<"Pl ease ent er t he gr ade" <<endl ; ci n>>gr ade; Sum=Sum+gr ade; count =count +1; }
Aver age=Sum/ ( count - 1) ; cout <<"The aver age i s " <<Aver age;
r et ur n 0; } Sentinel while:
- Variable is tested in the condition and loop ends when sentinel is encountered (we dont know how many times we have to loop)
Example: write a program that calculates average of the grade that the user will enter, the user will enter negative number to finish
Sentinel while loop: Example Sentinel while loop FlowChart Start Sum=0 Count = 0 Input Grade Grades>=0 Sum = Sum + Grade Count = Count + 1 Average = Sum/Count Stop No Yes
i nt mai n( ) { i nt gr ade;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
r et ur n 0; }
i nt mai n( ) { i nt gr ade, Sum=0;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
Sum=Sum+gr ade;
cout <<"Pl ease ent er one mor e gr ade\ n; ci n>>gr ade;
r et ur n 0; }
What will happen if variable Sum is not initialized ?!
i nt mai n( ) { i nt gr ade, Sum=0;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
whi l e( gr ade>=0) { Sum=Sum+gr ade;
cout <<"Pl ease ent er one mor e gr ade\ n; ci n>>gr ade; }
r et ur n 0; }
How many times we will loop?!
i nt mai n( ) { i nt gr ade, Sum=0, Aver age;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
whi l e( gr ade>=0) { Sum=Sum+gr ade;
cout <<"Pl ease ent er one mor e gr ade\ n; ci n>>gr ade; } Aver age=Sum/ ; cout <<"The aver age i s " <<Aver age; r et ur n 0; }
What do we need here!
i nt mai n( ) { i nt gr ade, count =0, Sum=0, Aver age;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
whi l e( gr ade>=0) { Sum=Sum+gr ade; count =count +1; cout <<"Pl ease ent er one mor e gr ade\ n; ci n>>gr ade; } Aver age=Sum/ count ; cout <<"The aver age i s " <<Aver age; r et ur n 0; }
/ / Anot her way of wr i t i ng ( sum = sum + grade) and / / ( count = count + 1)
i nt mai n( ) { i nt gr ade, count =0, Sum=0, Aver age;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
whi l e( gr ade>=0) { Sum+=grade; // compound assignments count+=1; // compound assignments cout <<"Pl ease ent er one mor e gr ade\ n; ci n>>gr ade; } Aver age=Sum/ count ; cout <<"The aver age i s " <<Aver age; r et ur n 0; }
36 Compound Assignments Used to update the values of a variable by performing an operation on the value that is currently stored in that variable (+=, -=, *=, / =, %=) Note that the assignment comes after the arithmetic operators Example
37 Compound Assignments Find the output of the following program
/ / A t hi r d way of wr i t i ng ( count = count + 1)
i nt mai n( ) { i nt gr ade, count =0, Sum=0, Aver age;
cout <<"Pl ease ent er gr ade\ n; ci n>>gr ade;
whi l e( gr ade>=0) { Sum+=gr ade; count ++; //this is called post increment. cout <<"Pl ease ent er one mor e gr ade\ n; ci n>>gr ade; } Aver age=Sum/ count ; cout <<"The aver age i s " <<Aver age; r et ur n 0; }
Write a C++ Program that checks if the password is correct or not, with controlled invalid password entry attempts.
i nt mai n( ) { i nt pass, count =1; cout <<"Pl ease ent er your passwor d\ n" ; ci n>>pass;
whi l e( pass ! = 1234 && count < 3) { cout <<Wr ong Passwor d! ; cout <<"pl ease r e- ent er your pass\ n; ci n>>pass; count =count +1; }
i f ( pass == 1234 && count <= 3) cout <<l ogi n successf ul \ n<<endl ; el se cout <<l ogi n f ai l ed\ n<<endl ;
r et ur n 0; }
Write a C++ program that converts integer grade to letter grade, with controlled number of invalid grade entry attempts.
/ / how many updat es you need f or t hi s whi l e?
i nt gr ade, count =1; cout <<"Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
whi l e( ( gr ade<0 | | gr ade>100) && count <=3) { cout <<"pl ease r e- ent er val i d gr ade\ n; ci n>>gr ade; count =count +1; }
i f ( count <4) swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n"; br eak; . . . def aul t : cout <<" F\ n"; } el se cout <<"i nval i d gr ades many t i mes\ n" ;
/ * i f t he condi t i on i s count<=6 and we want t he compi l er t o l oop t hr ee t i mes, what wi l l t he updat e val ue be? */
i nt gr ade, count=1; // count starts from one cout <<"Pl ease ent er your gr ade\ n" ; ci n>>gr ade;
whi l e( ( gr ade<0 | | gr ade>100) && count<=6) { cout <<"pl ease r e- ent er val i d gr ade\ n; ci n>>gr ade; count =count +( ?) ; // what will be here? } i f ( count <4) swi t ch( gr ade/ 10) { case 10: case 9: cout <<" A\ n"; br eak; . . . def aul t : cout <<" F\ n"; } el se cout <<"i nval i d gr ades many t i mes\ n;