Download presentation
Presentation is loading. Please wait.
Published byMiranda Evans Modified over 8 years ago
1
Looping Increment/Decrement Switch
2
Flow of Control Iteration/Switch Statements
3
Iteration Loop: A portion of a program that repeats itself a number of times Body: Repeated group of statements Iteration: Each repetition
4
Iteration While (logical expression) { statement(s); }
5
Iteration do { statement(s); } while (logical expression);
6
Iteration n While (expression). – logical expression is checked before loop execution n Do. while (expression); – Logical expression is checked after loop execution
7
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
8
which_player=-1; //which_player is control variable while (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 2) { which_player = -1; } Example 1
9
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 2) { which_player = -1; } while (which_player == -1) } Example 2
10
Increment/Decrement Operators n A variable may be incremented/decremented using a shortcut n The increment/decrement takes place before or after the actual operation on a variable dependending upon placement of the operator
11
Increment/Decrement Operators n postincrement: when the operator is physically after the variable to be incremented k = i ++; k = i ++; if i were = 3; k would be 3 if i were = 3; k would be 3 n preincrement: when the operator is physically before the variable to be incremented k = ++i;k = ++i; if i were = 3; k would be 4if i were = 3; k would be 4
12
Increment/Decrement Operators n postdecrement: when the operator is physically after the variable to be incremented k = i --;k = i --; if i were = 3; k would be 3if i were = 3; k would be 3 n predecrement: when the operator is physically before the variable to be incremented k = - - i;k = - - i; if i were = 3; k would be 2if i were = 3; k would be 2
13
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;
14
int count = 3; while (count -- > 0) { cout<<count<<“ “; int count = 3; while (-- count > 0) { cout<<count<<“ “; Self Test, pages 394-95
15
Iteration for (init exp; test; increment) { statement(s); };
16
Iteration n For loop – Initializing variable/number/ expression;declarations are permissable – if test expression is true, statements are executed – increment/decrement counter one or more
17
For Loop Continued n for ( ; ; ; ;<increment/decrement) { stmt(s) stmt(s) } n Initializing expression, is executed n Expression evaluated for true/false; terminates if false n Otherwise statements are executed n Increment/decrement is executed
18
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)
19
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 2) { cout<<“Input error. Try Again!”<,endl; x= thisclass. read_convert_to_int (); }
20
For or While?? n Use a for loop when there is a numerical calculation changed by an equal amount each iteration n if circumstances are such that you want the loop to be executed at least one time, use the do-while n if it is possible that the loop be skipped sometimes, use the while-loop
21
Switch Statement
22
switch (control expression) { case constant: statement(s) break; case constant: statement(s) break;. default: statement(s) }
23
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
24
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
25
Let’s Practice n Write a sequence of code that will access a function, based upon the constant within a switch statement n Assume that an “a” means to call a function, addit, that receives two integer values - x and y n An “m” means to call a function, multit, that receives two integer values - x and y n A “d” means to call a function, divit, that again receives two integer values - x and y n A “r” means to call a function, modit, that again receives the same two values n If the constant is not recognized, it is an error
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.