Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout << "Excellent.\n"; break; case 'P': cout << "Pass.\n"; break; case.

Similar presentations


Presentation on theme: "Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout << "Excellent.\n"; break; case 'P': cout << "Pass.\n"; break; case."— Presentation transcript:

1 Selection The Switch Statement 2/16/11

2 grade = 'P'; switch (grade){ case 'A': cout << "Excellent.\n"; break; case 'P': cout << "Pass.\n"; break; case 'F': cout << "Fail.\n"; break; default: cout << "Invalid.\n"; }

3 Switch Statement Selection based on a single variable or expression Controlling Expression – char or int – Not double or floating point or string Must have break – Falls through to next statement without it

4

5

6

7

8

9 Rewrite the if-else structure in ch3/factor.cpp so that it uses a switch instead of the if-else chain. Write a switch statement

10 Boolean Variables Type bool Keep track of whether a event occurred or condition has changed.

11 Boolean Variables Example bool validDay; if (1 <= day && day <= 31) validDay = true; else validDay = false; if (!validDay) cout << “Incorrect day input.\n” else cout << “Oct. “ << day << end;

12 Short Circuit Evaluation With an && or || operation, if the first condition determines the value of the operation the second condition isn't evaluated. weekend.cpp candy.cpp

13 What is the Output? int choice =1; switch (choice * 2 ){ case 1: cout << “Roast beef.\n”; break; case 2: cout << “Fried chicken.\n”; case 3: cout << “Mashed potatoes.\n” break; default: cout << “Bon appetit!\n”; }


Download ppt "Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout << "Excellent.\n"; break; case 'P': cout << "Pass.\n"; break; case."

Similar presentations


Ads by Google