40
1 Chapter 9 Additiona l Control Structure s Dale/Weems

1 Chapter 9 Additional Control Structures Dale/Weems

Embed Size (px)

Citation preview

Page 1: 1 Chapter 9 Additional Control Structures Dale/Weems

1

Chapter 9

Additional Control

Structures

Dale/Weems

Page 2: 1 Chapter 9 Additional Control Structures Dale/Weems

2

Chapter 9 Topics

Switch Statement for Multi-Way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements

Page 3: 1 Chapter 9 Additional Control Structures Dale/Weems

3

Switch StatementThe Switch statement is a selection control structure for

multi-way branching

switch (IntegralExpression){

case Constant1 :Statement(s); // optional

case Constant2 :Statement(s); // optional

. . .

default : // optionalStatement(s); // optional

}

Page 4: 1 Chapter 9 Additional Control Structures Dale/Weems

4

float weightInPounds = 165.8;char weightUnit;

. . . // User enters letter for desired weightUnitswitch (weightUnit){ case ‘P’ : case ‘p’ :

cout << weightInPounds << “ pounds “ << endl;break;

case ‘O’ : case ‘o’ :

cout << 16.0 * weightInPounds << “ ounces “ << endl;break;

case ‘K’ : case ‘k’ :

cout << weightInPounds / 2.2 << “ kilos “ << endl;break;

case ‘G’ : case ‘g’ :

cout << 454.0 * weightInPounds << “ grams “ << endl;break;

default :cout << “That unit is not handled! “ << endl;break;

} 4

Page 5: 1 Chapter 9 Additional Control Structures Dale/Weems

5

Switch Statement

The value of IntegralExpression (of char, short, int, long or enum type) determines which branch is executed

Case labels are constant (possibly named) integral expressions

Several case labels can precede a statement

Page 6: 1 Chapter 9 Additional Control Structures Dale/Weems

6

Control in Switch Statement Control branches to the statement following the case

label that matches the value of IntegralExpression Control proceeds through all remaining statements,

including the default, unless redirected with break

If no case label matches the value of IntegralExpression, control branches to the default label, if present--otherwise control passes to the statement following the entire switch statement

Forgetting to use break can cause logical errors because after a branch is taken, control proceeds sequentially until either break or the end of the switch statement occurs

Page 7: 1 Chapter 9 Additional Control Structures Dale/Weems

7

Do-While StatementDo-While is a looping control structure in which

the loop condition is tested after each iteration of the loop

SYNTAX

do

{

Statement

} while (Expression);

Loop body statement can be a single statement or a block

Page 8: 1 Chapter 9 Additional Control Structures Dale/Weems

8

void GetYesOrNo (/* out */ char& response)// Inputs a character from the user// Postcondition: response has been input // && response == ‘y’ or ‘n’{ do { cin >> response; // Skips leading whitespace

if ((response != ‘y’) && (response != ‘n’)) cout << “Please type y or n : “; } while ((response != ‘y’) && (response != ‘n’));}

Example of Do-While

8

Page 9: 1 Chapter 9 Additional Control Structures Dale/Weems

9

Do-While Loop vs. While Loop

POST-TEST loop (exit-condition)

The looping condition is tested after executing the loop body

Loop body is always executed at least once

PRE-TEST loop (entry-condition)

The looping condition is tested before executing the loop body

Loop body may not be executed at all

Page 10: 1 Chapter 9 Additional Control Structures Dale/Weems

10

Do-While Loop

When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the Do-while statement

Statement

Expression

DO

WHILE

FALSE

TRUE

Page 11: 1 Chapter 9 Additional Control Structures Dale/Weems

11

For Loop

SYNTAX

for (initialization; test expression; update) {

Zero or more statements to repeat

}

Page 12: 1 Chapter 9 Additional Control Structures Dale/Weems

12

For loop contains An initialization

An expression to test for continuing

An update to execute after each iteration of the body

Page 13: 1 Chapter 9 Additional Control Structures Dale/Weems

13

Example of For Loop

int num;

for (num = 1; num <= 3; num++) { cout << num << “Potato” << endl;}

Page 14: 1 Chapter 9 Additional Control Structures Dale/Weems

14

Example of Repetition num

int num;

for (num = 1; num <= 3; num++) cout << num << “Potato” << endl;

OUTPUT

?

Page 15: 1 Chapter 9 Additional Control Structures Dale/Weems

15

Example of Repetition

int num;

for (num = 1; num <= 3; num++) cout << num << “Potato” << endl;

num

OUTPUT

1

Page 16: 1 Chapter 9 Additional Control Structures Dale/Weems

16

Example of Repetition num

OUTPUT

1

int num;

for(num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

true

Page 17: 1 Chapter 9 Additional Control Structures Dale/Weems

17

Example of Repetition num

int num;

for (num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

OUTPUT

1

1Potato

Page 18: 1 Chapter 9 Additional Control Structures Dale/Weems

18

Example of Repetition num

OUTPUT

2

int num;

for (num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

1Potato

Page 19: 1 Chapter 9 Additional Control Structures Dale/Weems

19

Example of Repetition num

OUTPUT

2

true

1Potato

int num;

for(num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

Page 20: 1 Chapter 9 Additional Control Structures Dale/Weems

20

Example of Repetition num

int num;

for (num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

OUTPUT

2

1Potato

2Potato

Page 21: 1 Chapter 9 Additional Control Structures Dale/Weems

21

Example of Repetition num

OUTPUT

3

int num;

for (num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

1Potato

2Potato

Page 22: 1 Chapter 9 Additional Control Structures Dale/Weems

22

Example of Repetition num

OUTPUT

3

true

1Potato

2Potato

int num;

for(num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

Page 23: 1 Chapter 9 Additional Control Structures Dale/Weems

23

Example of Repetition num

int num;

for(num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

OUTPUT

3

1Potato

2Potato

3Potato

Page 24: 1 Chapter 9 Additional Control Structures Dale/Weems

24

Example of Repetition num

OUTPUT

4

int num;

for (num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

1Potato

2Potato

3Potato

Page 25: 1 Chapter 9 Additional Control Structures Dale/Weems

25

Example of Repetition num

OUTPUT

4

false

1Potato

2Potato

3Potato

int num;

for(num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

Page 26: 1 Chapter 9 Additional Control Structures Dale/Weems

26

Example of Repetition num

When the loop control condition is evaluated and has value false, theloop is said to be “satisfied” and control passes to the statementfollowing the For statement

4

falseint num;

for(num = 1; num <= 3; num++)

cout << num << “Potato” << endl;

Page 27: 1 Chapter 9 Additional Control Structures Dale/Weems

27

Output

The output was1Potato2Potato3Potato

Page 28: 1 Chapter 9 Additional Control Structures Dale/Weems

28

int count;

for (count = 4; count > 0; count--)

{

cout << count << endl;

}

cout << “Done” << endl;

Count-controlled Loop

OUTPUT: 4321Done

Page 29: 1 Chapter 9 Additional Control Structures Dale/Weems

29

What is output?

int count;

for (count = 0; count < 10; count++)

{

cout << “*”;

}

Page 30: 1 Chapter 9 Additional Control Structures Dale/Weems

30

Answer

**********

The 10 asterisks are all on one line. Why?

Page 31: 1 Chapter 9 Additional Control Structures Dale/Weems

31

What output from this loop?

int count;

for (count = 0; count < 10; count++);

{

cout << “*”;

}

Page 32: 1 Chapter 9 Additional Control Structures Dale/Weems

32

No output from the for loop! Why? The semicolon after the () means that the

body statement is a null statement In general, the body of the For loop is whatever

statement immediately follows the () That statement can be a single statement, a

block, or a null statement Actually, the code outputs one * after the loop

completes counting to 10

Answer

Page 33: 1 Chapter 9 Additional Control Structures Dale/Weems

33

Several Statements in Body Blockconst int MONTHS = 12;

int count;

float bill;

float sum = 0.0;

for (count = 1; count <= MONTHS; count++)

{

cout << “Enter bill: “;

cin >> bill;

sum = sum + bill;

}

cout << “Your total bill is : “ << sum << endl;

33

Page 34: 1 Chapter 9 Additional Control Structures Dale/Weems

34

Break Statement The Break statement can be used with

Switch or any of the 3 looping structures

It causes an immediate exit from the Switch, While, Do-While, or For statement in which it appears

If the Break statement is inside nested structures, control exits only the innermost structure containing it

Page 35: 1 Chapter 9 Additional Control Structures Dale/Weems

35

Guidelines for Choosing Looping Statement

For a simple count-controlled loop, use the For statement

For an event-controlled loop whose body always executes once, use of Do-While statement

For an event-controlled loop about which nothing is known, use a While statement

When in doubt, use a While statement

Page 36: 1 Chapter 9 Additional Control Structures Dale/Weems

36

Continue Statement

The Continue statement is valid only within loops

It terminates the current loop iteration, but not the entire loop

In a For or While, Continue causes the rest of the body of the statement to be skipped; in a For statement, the update is done

In a Do-While, the exit condition is tested, and if true, the next loop iteration is begun

Page 37: 1 Chapter 9 Additional Control Structures Dale/Weems

37

Problem

Given a character, a length, and a width, draw a box

For example, given the values ‘&’, 4, and 6, you would display

&&&&&&

&&&&&&

&&&&&&

&&&&&&

Page 38: 1 Chapter 9 Additional Control Structures Dale/Weems

38

Prototype for Void Function

Call your function DrawBox () with 3 parameters, the first is type char, the other 2 are type int.

void DrawBox(char, int, int);

Identifiers may appear in prototypes, but are not necessary

void DrawBox(char letter, int num1,

int num2);

Page 39: 1 Chapter 9 Additional Control Structures Dale/Weems

39

void DrawBox(char what, int down, int across) // 3 parameters

{

int row, col; // 2 local variables

for (row = 0; row < down; row++)

{

for (col = 0; col < across; col++)

{

cout << what;

}

cout << endl;

}

return;

} 39

Page 40: 1 Chapter 9 Additional Control Structures Dale/Weems

40

#include <iostream>

void DrawBox (char, int, int); // Prototype

int main ()

{

char letter = ‘&’;

DrawBox(letter, 4, 2*3); // Function call

DrawBox(‘V’, 9, 3); // Function call

return 0;

}

The Driver Program