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)
{
statement(s);
}
Iteration
do
{
statement(s);
}
while (logical expression);
Iteration
While (expression)
.
.
– logical expression is checked before loop execution
Do
.
while (expression);
– Logical expression is checked after loop execution
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
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
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
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
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
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
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;
int count = 3;while (count -- > 0){ cout<<count<<“ “;
int count = 3;while (-- count > 0){ cout<<count<<“ “;
Self Test, pages 394-95
Iteration
for (init exp; test; increment)
{
statement(s);
};
Iteration
For loop– Initializing
variable/number/ expression;declarations are permissable
– if test expression is true, statements are executed
– increment/decrement counter one or more
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
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)
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 (); } }
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
Switch Statement
switch (control expression){ case constant: statement(s) break; case constant: statement(s) break; . . default: statement(s)}
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
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
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
Recommended