powerpoint 5

25
Looping Increment/Decrement Switch

Upload: brielle-brock

Post on 02-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Powerpoint 5. Looping Increment/Decrement Switch. Flow of Control. Iteration/Switch Statements. Iteration. Loop: A portion of a program that repeats itself a number of times Body: Repeated group of statements Iteration: Each repetition. Iteration. While (logical expression) { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Powerpoint 5

Looping

Increment/Decrement

Switch

Page 2: Powerpoint 5

Flow of Control

Iteration/Switch Statements

Page 3: Powerpoint 5

Iteration

Loop: A portion of a program that repeats itself a number of times

Body: Repeated group of statements

Iteration: Each repetition

Page 4: Powerpoint 5

Iteration

While (logical expression)

{

statement(s);

}

Page 5: Powerpoint 5

Iteration

do

{

statement(s);

}

while (logical expression);

Page 6: Powerpoint 5

Iteration

While (expression)

.

.

– logical expression is checked before loop execution

Do

.

while (expression);

– Logical expression is checked after loop execution

Page 7: Powerpoint 5

With a while loop: - Always give a value to a control variable in two places o prior to entering the loop o last statement in the loop

With a do/while loop: - Always give a value to a control variable in one place o last statement in the loop

Iteration

Page 8: Powerpoint 5

which_player=-1; //which_player is control variablewhile (which_player < 0){ cout<<“Enter the following: “; cout<<“**********************”<<endl; cout<<setw(10)<<“1: “<<lname1<<“,”<<fname1<<endl; cout<<setw(10)<<“2: ”<< lname2<<“,”<<fname2<<endl; which_player=thisclass. read_convert_to_int (); if(which_player < 1 || which_player >2) { which_player = -1; }}

Example 1

Page 9: Powerpoint 5

do { cout<<“Enter the following: “; cout<<“**********************”<<endl; cout<<setw(10)<<“1: “<<lname1<<“,”<<fname1<<endl; cout<<setw(10)<<“2: ”<< lname2<<“,”<<fname2<<endl; which_player=thisclass. read_convert_to_int (); if(which_player < 1 || which_player >2) { which_player = -1; }while (which_player == -1)}

Example 2

Page 10: Powerpoint 5

Increment/Decrement Operators A variable may be

incremented/decremented using a shortcut

The increment/decrement takes place before or after the actual operation on a variable dependending upon placement of the operator

Page 11: Powerpoint 5

Increment/Decrement Operators

postincrement: when the postincrement: when the operator is physically after the operator is physically after the variable to be incrementedvariable to be incremented• k = i ++;k = i ++;• if i were = 3; k would be 3if i were = 3; k would be 3

preincrement: when the preincrement: when the operator is physically before the operator is physically before the variable to be incrementedvariable to be incremented• k = ++i; k = ++i; • if i were = 3; k would be 4if i were = 3; k would be 4

Page 12: Powerpoint 5

Increment/Decrement Operators

postdecrement: when the postdecrement: when the operator is physically after the operator is physically after the variable to be incrementedvariable to be incremented• k = i --;k = i --;• if i were = 3; k would be 3if i were = 3; k would be 3

predecrement: when the predecrement: when the operator is physically before the operator is physically before the variable to be incrementedvariable to be incremented• k = - - i;k = - - i;• if i were = 3; k would be 2if i were = 3; k would be 2

Page 13: Powerpoint 5

Increment/Decrement operation

k += I;

k *=4;

k -=y;

k /= y + w + z;

k % = 13;

equivalent

k = k + I;

k = k * 4;

K = k - y;

k = k / (y + w + z);

k = k % 13;

Page 14: Powerpoint 5

int count = 3;while (count -- > 0){ cout<<count<<“ “;

int count = 3;while (-- count > 0){ cout<<count<<“ “;

Self Test, pages 394-95

Page 15: Powerpoint 5

Iteration

for (init exp; test; increment)

{

statement(s);

};

Page 16: Powerpoint 5

Iteration

For loop– Initializing

variable/number/ expression;declarations are permissable

– if test expression is true, statements are executed

– increment/decrement counter one or more

Page 17: Powerpoint 5

For Loop Continued for (<init-exp>;<test>;<increment/decrement)for (<init-exp>;<test>;<increment/decrement)

{{

stmt(s)stmt(s)

}} Initializing expression, is executedInitializing expression, is executed Expression evaluated for true/false; Expression evaluated for true/false;

terminates if falseterminates if false Otherwise statements are executedOtherwise statements are executed Increment/decrement is executedIncrement/decrement is executed

Page 18: Powerpoint 5

For Loop Class Exercise

Write a class function to: 1) input n variables, 2) sum each into a field called total

The number, n is passed to the function as an argument.

The function definition is:

void abc::getgrades (int cnt)

Page 19: Powerpoint 5

For Loop Class Exercise

void abc::getgrades (int cnt) { cout<<“Enter a Grade: “<<endl; for (int k=1;k<cnt;k++) { x= thisclass. read_convert_to_int (); while(x < 1|| x > 2) { cout<<“Input error. Try Again!”<,endl; x= thisclass. read_convert_to_int (); } }

Page 20: Powerpoint 5

For or While??

Use a for loop when there is a numerical calculation changed by an equal amount each iteration

if circumstances are such that you want the loop to be executed at least one time, use the do-while

if it is possible that the loop be skipped sometimes, use the while-loop

Page 21: Powerpoint 5

Switch Statement

Page 22: Powerpoint 5

switch (control expression){ case constant: statement(s) break; case constant: statement(s) break; . . default: statement(s)}

Page 23: Powerpoint 5

1. When statement is executed, one of a number of branches is determined by the control statement.

2. The control statement is in () after the switch

3. Note preferred indenting pattern

4. Control statement must always return a char or an integer

Page 24: Powerpoint 5

1. Upon execution, the control statement is evaluated

2. The computer then looks at the values after each case until it finds a match, I.e. constant equal to the return and executes the code until a “break” statement is encountered!

3. Note that you may not have more than one occurance of any constant

Page 25: Powerpoint 5

Let’s Practice

Write a sequence of code that will access a function, based upon the constant within a switch statement

Assume that an “a” means to call a function, addit, that receives two integer values - x and y

An “m” means to call a function, multit, that receives two integer values - x and y

A “d” means to call a function, divit, that again receives two integer values - x and y

A “r” means to call a function, modit, that again receives the same two values

If the constant is not recognized, it is an error