chapter 05 (part iii) control statements: part ii

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

Upload: darleen-nash

Post on 04-Jan-2016

221 views

Category:

Documents


4 download

TRANSCRIPT

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

Chapter 05 (Part III)

Control Statements: Part II

Page 2: Chapter 05 (Part III) Control Statements: Part II

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.

Page 3: Chapter 05 (Part III) Control Statements: Part II

5.6 switch Multiple-Selection Statement

• switch statement

– Used for multiple selections

– Tests a variable or expression

Page 4: Chapter 05 (Part III) Control Statements: Part II

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

Page 5: Chapter 05 (Part III) Control Statements: Part II

The Use of switch Statementint option;

cin >> option;

switch (option)

{

case 0:

…;

break;

case 1:

…;

break;

default:

…;

break;

}

Controlling Expression

compared with the controlling expression

Page 6: Chapter 05 (Part III) Control Statements: Part II

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.

Page 7: Chapter 05 (Part III) Control Statements: Part II

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;

… }

Page 8: Chapter 05 (Part III) Control Statements: Part II

Fig. 5.12 | switch multiple-selection

statement with break statements.

Page 9: Chapter 05 (Part III) Control Statements: Part II

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;

}

Page 10: Chapter 05 (Part III) Control Statements: Part II

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’

Page 11: Chapter 05 (Part III) Control Statements: Part II

Reading character input

– Function cin.get()• Reads one character from the keyboard

– EOF• End-of-file.• Defined in <iostream>• <ctrl> d in UNIX/Linux• <ctrl> 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.

Page 12: Chapter 05 (Part III) Control Statements: Part II

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

Page 13: Chapter 05 (Part III) Control Statements: Part II

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.

Page 14: Chapter 05 (Part III) Control Statements: Part II

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

Page 15: Chapter 05 (Part III) Control Statements: Part II

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

• Exampleint option = 0;

while (option != -1)

{

cin >> option;

if (option == -1)

break; //force immediate exit

}

Page 16: Chapter 05 (Part III) Control Statements: Part II

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

Page 17: Chapter 05 (Part III) Control Statements: Part II

1 // Fig. 5.13: fig05_13.cpp

2 // break statement exiting a for statement.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 int main()

8 {

9 int count; // control variable also used after loop terminates

10

11 for ( count = 1; count <= 10; count++ ) // loop 10 times

12 {

13 if ( count == 5 )

14 break; // break loop only if x is 5

15

16 cout << count << " ";

17 } // end for

18

19 cout << "\nBroke out of loop at count = " << count << endl;

20 return 0; // indicate successful termination

21 } // end main 1 2 3 4 Broke out of loop at count = 5

Loop 10 times

Exit for statement (with a break) when count equals 5

Page 18: Chapter 05 (Part III) Control Statements: Part II

1 // Fig. 5.14: fig05_14.cpp

2 // continue statement terminating an iteration of a for statement.

3 #include <iostream>

4 using std::cout;

5 using std::endl;

6

7 int main()

8 {

9 for ( int count = 1; count <= 10; count++ ) // loop 10 times

10 {

11 if ( count == 5 ) // if count is 5,

12 continue; // skip remaining code in loop

13

14 cout << count << " ";

15 } // end for

16

17 cout << "\nUsed continue to skip printing 5" << endl;

18 return 0; // indicate successful termination

19 } // end main 1 2 3 4 6 7 8 9 10 Used continue to skip printing 5

Loop 10 times

Skip line 14 and proceed to line 9 when count equals 5

Page 19: Chapter 05 (Part III) Control Statements: Part II

Performance Tip 5.5

• The break and continue statements, when used properly, perform faster than do the corresponding structured techniques.