240 likes | 252 Views
Flow of Control. INFSY 535 Spring 2003 Lecture 6. Control Structures. Sequence : one after the other Selection : selects a path based upon action Iteration : repetition. Iteration. Loop: a portion of a program that repeats itself a number of times
E N D
Flow of Control INFSY 535 Spring 2003 Lecture 6
Control Structures • Sequence: one after the other • Selection: selects a path based upon action • Iteration: repetition
Iteration • Loop: a portion of a program that repeats itself a number of times • Counter-Controlled Loop: loop that is managed by counting the number of iterations • Conditional Loop: loop which continues while a logical condition is true
while-statement • #include <iostream> • using namespace std; • void main( ) • { • int count_down; • cout << “How many greetings do you want? “; • cin >> count_down; • while (count_down > 0)// logical expression • { • cout << “Hello “; // body • count_down = count_down - 1; • } • cout << endl; • cout << “That’s all!\n”; • return; • }
do-while-statement • similar to a while-statement • loop body is always executed at least once
do-while-statement • char ans; • do • { • cout << “Hello\n”; • cout << “Do you want another greeting?\n” • << “Press y for yes, n for no, \n” • << “and then press return: ”; • cin >> ans; • } while (ans == ‘y’ || ans == ‘Y’);
while(expression) . . - logical expression is checked before loop execution do . while (expression); - logical expression is checked after loop execution Iteration
Iteration Pitfall • Infinite Loop - loop runs forever • x = 2; • while (x != 12) • { • cout << x << endl; • x + = 2; • } • Output =2, 4, 6, 8, 10, loop stops
Iteration Pitfall • x = 1; • while (x != 12) • { • cout << x << endl; • x + = 2; • } • Output = 1, 3, 5, 7, 9, 11, 13, 15, 17, ... loop
Increment/Decrement Operators • A shortcut to increase or decrease a value stored in a variable • The increment/decrement takes place before/after the actual operation on a variable depending upon placement of the operator • ++operator is the incrementoperator • --operator is the decrement operator • Used with variable of typeint
Increment/Decrement Operators • postincrement: when the operator is physically after the variable to be incremented • int m = 3; • k = m++; • 3 assigned to k; m incremented to 4 • preincrement: when the operator is physically before the variable to be incremented • int m = 3; • k = ++m; • m incremented to 4; 4 assigned to k
Increment/Decrement Operators • postdecrement: when the operator is physically after the variable to be decremented • int m = 3; • k = m--; • 3 assigned to k; m decremented to 2 • predecrement: when the operator is physically before the variable to be decremented • int m = 3; • k = --m; • m decremented to 2; 2 assigned to k
Increment/Decrement Operators • Practice with Self-Test exercises, page 419-420, 25 thru 28.
for-Statement • Initializing expression is executed once • Logical expression is evaluated for true/false; terminates if false • Otherwise statements are executed • Increment/decrement is executed
for-Statement Examples • int n; • for (n = 1; n <= 10; n+ = 2) • cout << “n is now equal to “ << n << endl; • for (int n = 0; n < 5; n++) • balance = balance - 2;
for-Statement Pitfalls • Make sure all variables used in the loop are initialized • Be careful to avoid infinite loops • Do not place a semicolon after the parentheses at the beginning of a for-loop
What kind of loop to use? • If a loop involves a numeric calculation using one variable that will be changed by an equal amount each time through the loop, use a for-loop. • If the body of a loop must be executed at least one time, use ado-while-loop. • If there are circumstances for which the loop body should not be executed, use a while-loop.
LAB • Self Test, page 440, 40 - 41 • Write a for-loop for 40. • Write a while-loop and a do-while-loop for 41.
Classes Connect • Classes are the vocabulary of an area of knowledge. • Design a system, model the terms as classes in the UML.
Class Relationships • Association between 2 classes • Multiplicity