Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail.

Similar presentations


Presentation on theme: "1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail."— Presentation transcript:

1 1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail

2 2 The if Statement It’s a selection or decision statement if (test-condition) statement; –test-condition is an expression that evaluates to true or false –If the condition is true, then the statement will execute; otherwise not if (10 <= 11) cout << “hello” << endl; cout << “I am after if”; Can have a block of statements instead of just one Pitfall: if(marks = 100) {cout << “Congrats!”;} –Write if(100 == marks). Now compiler catches single =

3 3 The if else Statement Choice between two alternatives –Pseudocode: if( ) { } else { } If is TRUE (non-zero), then are executed If is FALSE (zero), then are executed

4 4 The if else Statement Example: ATM Withdrawal program #include int main() { int balance = 10000; int withdrawal; cout << "Please enter the withdrawal amount: "; cin >> withdrawal; if(withdrawal <= balance) { cout << "Here is your money." << endl; } else { cout << "Sorry, insufficient balance." <<endl; } return 0; }

5 5 Nested ifs An if statement that is the target of another if or an else An else refers to the nearest if statement within the same block that doesn’t have an else if(i) { if(j) statement1; if(k) statement2; // this if else statement3; // is associated with // this else } else statement4; // associated with?

6 6 Nested ifs The inside the braces can contain any valid C++ statements, including other if-else statements! if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance?(y/n) "; cin >> answer; if (answer == 'y') { cout << "Your balance is "<<balance<< endl; } else { cout << "Ok then. "; } else { cout << "Here is your money." << endl; } cout << "Goodbye." << endl;

7 7 Nested ifs Another example #include int main() { int a, b, c; cout << “Enter three numbers a, b, c:\n”; cin >> a >> b >> c; if (a==b) if(b==c) cout << “a, b, c are equal\n”; else cout << “a and b are different\n”; return 0; } Let a=2, b=c=3. What’s printed?

8 8 The if-else-if Construction More than 2 choices. if else is itself a statement, so can follow an else: if (ch == ‘A’) agrade++;//choice #1 else if (ch == ‘B’)//choice #2 bgrade++; else SoSo++;//choice #3 Easier to read format: if (ch == ‘A’) agrade++;//choice #1 else if (ch == ‘B’) bgrade++;//choice #2 else SoSo++;//choice #3

9 9 The if-else-if Ladder Conditional expressions evaluated top-down; The first true condition’s statements evaluated; rest of the ladder skipped { int num_credits; cout<<"Please enter workload: "; cin>>num_credits; if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton" << endl; }

10 10 The switch Statement If a large number of choices that all depend on one expression’s value, better to use switch than the if-else-if construct switch (expression) { case constant1: statement(s); break; case constant2: statement(s); break; … default: statement(s); break;// not required } expression must evaluate to an integer value (includes characters); constant must be integer (or character) constant

11 11 The switch Statement int main() { int x=1; switch (x) { case 0: cout<<"zero \n"; break; case 1: cout<<“one \n"; break; default: cout<<“sorry! \n"; break; } return 0; } char c = ‘M’; switch(c). What case will match now? case‘M’ will match too; so will case(‘L’ + 1) int x = 1, y = 2; switch(x<y). What case will match?

12 12 The switch Statement int main() { int x=1; switch (x) { case 0: cout<<"zero \n"; break; case 1: cout<<“one \n"; default: cout<<“sorry! \n“; } return 0; } break necessary to exit from switch, otherwise all subsequent executed

13 13 The switch Statement break and default are optional but important. No two cases can have same value Can have same statements for multiple cases int medal; cout<<"Enter your rank in class: "; cin>>medal; switch (medal) { case 1: case 2: case 3: cout<<"Wow! Medal winner \n"; break; default: cout<<"Better luck next time \n"; }

14 14 The switch Statement int main() { int i; for(i=0; i<5; i++) { switch(i) { case 0: cout << "less than 1\n"; case 1: cout << "less than 2\n"; case 2: cout << "less than 3\n"; case 3: cout << "less than 4\n"; case 4: cout << "less than 5\n"; } cout << '\n'; } return 0; } Output? switch more efficient than if else if when more than a few constant choices Do nested switch statements yourself


Download ppt "1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail."

Similar presentations


Ads by Google