39
Additional Control Structures

Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Embed Size (px)

Citation preview

Page 1: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Additional Control Structures

Page 2: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Switch Statement

Is a selection control structure for multi-way branching. Syntax:

switch ( IntegralExpression ){

case Constant1 :Statement(s); // optional

case Constant2 :Statement(s); // optional

. . .

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

}

Page 4: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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 ;

}

Page 5: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Do-While Statement

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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’ ) ) ;}

Function Using Do-While

Page 9: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

A Count-Controlled Loop

SYNTAX

for ( initialization ; test expression ; update ) {

0 or more statements to repeat

}

Page 12: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

The for loop contains

an initialization

an expression to test for continuing

an update to execute after each iteration of the body

Page 13: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetitionint num;

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

endl;}

Page 14: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

int num;

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

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

OUTPUT

?

Page 15: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

1

int num;

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

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

Page 16: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

1

int num;

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

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

true

Page 17: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

int num;

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

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

OUTPUT

1

1Potato

Page 18: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

2

int num;

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

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

1Potato

Page 19: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

2

true

1Potato

int num;

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

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

Page 20: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

int num;

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

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

OUTPUT

2

1Potato

2Potato

Page 21: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

3

int num;

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

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

1Potato

2Potato

Page 22: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

3

true

1Potato

2Potato

int num;

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

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

Page 23: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

int num;

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

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

OUTPUT

3

1Potato

2Potato

3Potato

Page 24: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

4

int num;

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

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

1Potato

2Potato

3Potato

Page 25: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Example of Repetition num

OUTPUT

4

false

1Potato

2Potato

3Potato

int num;

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

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

Page 26: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

int count ;

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

{ cout << count << endl;}

cout << “Done” << endl;

Count-controlled Loop

OUTPUT: 4321Done

Page 28: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

What is output?

int count;

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

{ cout << “”;

}

Page 29: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

OUTPUT

**********

NOTE: the 10 asterisks are all on one line. Why?

Page 30: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

What output from this loop?

int count;

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

{ cout << “”;

}

Page 31: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

no output from the for loop! Why?

the ; right 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 its counting to 10

OUTPUT

Page 32: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Several Statements in Body Block

const 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 ;

Page 33: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Break Statement 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 is inside nested structures, control exits only the innermost structure containing it

Page 34: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Continue Statement is valid only within loops

terminates the current loop iteration, but not the entire loop

in a For or While, continue causes the rest of the body 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 35: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Imagine using . . .

a character, a length, and a width to draw a box, for example,

using the values ‘&’, 4, and 6 would display

&&&&&&&&&&&&&&&&&&&&&&&&

Page 36: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Write prototype for void function

called DrawBox ( ) with 3 parameters. The first is type char, the other 2 are type

int.void DrawBox( char, int , int );

NOTE: In the function prototype, more information is given if variables are included. Any valid C++ identifiers, as long as each is different, can be used.

void DrawBox( char letter, int len, int wid);

Page 37: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

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

{

int row, col; // 2 local variables

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

{

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

{

cout << what;

}

cout << endl;

}

return;

}

Page 38: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

#include <iostream>

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

int main ( ) {

char letter = ‘&’;

DrawBox(letter, 4, 2*3); // arguments DrawBox(‘V’, 9, 3); // appear in call

return 0;}

THE DRIVER PROGRAM

Page 39: Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using

Write a function using prototype

void DisplayTable ( int ) ; // prototype

The function displays a specified multiplication table. For example, the call DisplayTable(6) displays this table:

1 x 6 = 6 2 x 6 = 12 3 x 6 = 18

.

.

.

12 x 6 = 72