Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 05 (Part III) Control Statements: Part II.

Similar presentations


Presentation on theme: "Chapter 05 (Part III) Control Statements: Part II."— Presentation transcript:

1 Chapter 05 (Part III) Control Statements: Part II

2 Objectives In this part you will learn: To understand multiple selection using the switch selection statement. To use the break and continue program control statements to alter the flow of control.

3 5.6 switch Multiple-Selection Statement switch statement –Used for multiple selections –Tests a variable or expression

4 5.6 switch Multiple-Selection Statement switch statement –Controlling expression Expression in parentheses after keyword switch –case labels Compared with the controlling expression Statements following the matching case label are executed –Braces are not necessary around multiple statements in a case label

5 The Use of switch Statement int option; cin >> option; switch (option) { case 0: …; break; case 1: …; break; default: …; break; } Controlling Expression compared with the controlling expression

6 5.6 switch Multiple-Selection Statement –default case Executes if no matching case label is found This case is optional –If no match and no default case »Control simply continues after the switch A break statements causes execution to proceed with the first statement after the switch »Without a break statement, execution will fall through to the next case label Note: Forgetting a break statement when one is needed in a switch statement is a logic error.

7 5.6 switch Multiple-Selection Statement The default clause does not require a break statement. Some programmers include break for clarity and for symmetry with other cases. Specifying an expression in the case label is a syntax error. – Incorrect example: switch (option) { case a+b: …; break; … }

8 Fig. 5.12 | switch multiple-selection statement with break statements.

9 Comparison int option; cin >> option; switch (option) { case 1: result = a + b; break; case 2: result = a – b; break; default: cout << “Incorrect option!” << endl; break; } int option; cin >> option; if (option == 1) { result = a + b; } else if (option == 2) { result = a – b; } else { cout << “Incorrect option!” << endl; }

10 Calculating number of letters Declare a variable with character. Examples of character constant: ‘A’, ‘b’, ‘C’, ‘\n’… Note: Do not confuse string constant with character constant. A string is a sequence of characters. String: “A” Character: ‘A’

11 Reading character input –Function cin.get() Reads one character from the keyboard –EOF End-of-file. Defined in d in UNIX/Linux z in Windows and then press ‘Enter’. –while ((letter = cin.get()) != EOF) 1.Read one character from keyboard and assign it to letter. 2.Compare letter with EOF If letter != EOF (End-of-file), enter the loop. Otherwise (letter == EOF), leave the loop.

12 switch Multiple-Selection Statement This statement executes when letter == ‘A’ or letter == ‘a’. Without a break statement, execution will fall through to the next case label Identify newline, tab, and space character

13 Output Results Character constant String constant Note: A character constant is enclosed using single quotation marks. We can also represent a character constant using integer type. A string constant is enclosed using double quotation marks.

14 Integer Data Types Integer data types –short Abbreviation of short int Minimum range is -32,768 to 32,767 –long Abbreviation of long int Minimum range is -2,147,483,648 to 2,147,483,647 –int Equivalent to either short or long on most computers –char Can be used to represent small integers Note: Integer data types can vary in size between systems

15 5.7 break and continue Statements break / continue statements –Alter flow of control break statement –Causes immediate exit from control structure –Used in while, for, do…while or switch statements Example int option = 0; while (option != -1) { cin >> option; if (option == -1) break;//force immediate exit }

16 5.7 break and continue Statements continue statement –Skips remaining statements in loop body Proceeds to increment and condition test in for loops Proceeds to condition test in while / do…while loops –Then performs next iteration (if not terminating) –Used in while, for or do…while statements

17 Loop 10 times Exit for statement (with a break ) when count equals 5

18 Loop 10 times Skip line 14 and proceed to line 9 when count equals 5

19 Performance Tip 5.5 The break and continue statements, when used properly, perform faster than do the corresponding structured techniques.


Download ppt "Chapter 05 (Part III) Control Statements: Part II."

Similar presentations


Ads by Google