Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,

Similar presentations


Presentation on theme: "1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,"— Presentation transcript:

1 1 CS 101 Fall 2001 Lecture 3

2 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y, x>=y, x 4); would assign 0 to x.

3 3 if(.03) cout <<3; will print 3 to the screen since.03 is not 0 and therefore true. If x = 3, y = 5; an assignment statement x = y; will have a value of 5 and so if we have if(x=y) cout << 1, this will print 1 since 5 is true. In math, we can write x<y<z and this will mean x<y and y<z, but in C++, if x=3, y=2, z=3 then if(x<y<z)cout<<1 will print 1 to the screen since x<y<z is treated as (x<y)<z, which is 0<z since x<y is false and therefore equal 0, and 0<z is true. We have to write if(x<y&&y<z) to get the effect we want. Parentheses are not necessary since < is stronger than &&. Thus writing x=y when we mean x==y is not a syntax, but a logical error, and similarly with x<y<z.

4 4 Ambiguity of nested if elses Suppose we have x=3, y=4, z=5 and if(x>y) if(y y) {if(y y) { if(y<z) cout<<y;} else cout<<z; cout<<x;

5 5 Sequential is, else if, else if,... Suppose that we want to change a number score to a letter grade and that th integer variable x holds the numerical score. Then if(x>=90)cout =80)cout’B’; else if(x>=70)cout =60)cout =90)cout =80)cout’B’; else{ if(x>=70)cout =60)cout<<‘D’; else cout<<‘F’;}}} but we can avoid writing the enclosing braces. Thus this counts as one statement.

6 6 if, else if, else if, … program Here is a program which uses a sequence of if, else if, … #include main( ){ cout >x;cout =90)cout =85)cout =80)cout =75)cout =70)cout =60)cout<<‘D’; else cout<<“F’; cout<<“.\n”; } You should copy, compile and run this to see that it works.

7 7 The switch statement If x is an integer variable holding a numerical score as in the previous two slides, and / is integer division, then we may write switch(x/5) { case (20): case(19):case 18: cout<<‘A’; break; case(17): cout<<“B+”;break; case(16): cout<<‘B’;break; case(15): cout<<“C+”;break; case(14): cout<<‘C’;break; case(12): cout<<‘D’;break; default: cout<<‘F’; The reason for writing break statements is that the switch statement exhibits what is called drop through behavior, i.e., if the break statements weren’t there and x was 15, C+ would print out, but also, CDF would also print out.

8 8 switch program The program in slide 13 can also be written as #include main( ){ cout >x;cout<<“You made an “; switch(x/5){ case 20: case 19:case 18:cout<<‘A’;break; case 17:cout<<“B+”; break; case 16:cout<<‘B’; break; case 15:cout<<“C+”; break; case 14:cout<<‘C’; break; case 12:cout<<‘D’; break; default: cout<<“F’; cout<<“.\n”; } You should copy, compile and run this to see that it works.


Download ppt "1 CS 101 Fall 2001 Lecture 3. 2 Boolean expressions The expressions that are allowed to be in the parentheses following an if can be of the form x>y,"

Similar presentations


Ads by Google