Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

lesson 7 Iteration Structures The eternal question: "Where is my next instruction?" Think of code as a stream.

The sequential structure flows smoothly down. The decision structure might branch the stream, or skip further downstream. The iteration structure, uniquely, has the potential to flow upstream. An iteration structure, or loop, is used when you want a block of code to run more than once. For instance, let's say we want the ball from the previous examples to bounce up and down exactly ten times. The basic looping structure is the while statement.

The for loop The statements in the for loop repeat continuously for a specific number of times. The while and do-while loops repeat until a certain condition is met. The for loop repeats until a specific count is met. Use a for loop when the number of repetition is know, or can be supplied by the user. The coding format is: for(startExpression; testExpression; countExpression) { block of code; } The startExpression is evaluated before the loop begins. It is acceptable to declare and assign in the startExpression (such as int x = 1;). This startExpression is evaluated only once at the beginning of the loop. The testExpression will evaluate to TRUE (nonzero) or FALSE (zero). While TRUE, the body of the loop repeats. When the testExpression becomes FALSE, the looping stops and the program continues with the statement immediately following the for loop body in the program code. The countExpression executes after each trip through the loop. The count may increase/decrease by an increment of 1 or of some other value. Braces are not required if the body of the for loop consists of only ONE statement. Please indent the body of the loop for readability. CAREFUL: When a for loop terminates, the value stored in the computer's memory under the looping variable will be "beyond" the testExpression in the loop. It must be sufficiently large (or small) to cause a false condition. Consider: for (x = 0; x <= 13; x++) cout<<"Melody"; When this loop is finished, the value 14 is stored in x.

The following program fragment prints the numbers 1 - 20.

for: int ctr; for(ctr=1;ctr<=20; ctr++) { cout<< ctr <<"\n"; } Common Error: If you wish the body of a for loop to be executed, DO NOT put a semicolon after the for's parentheses. Doing so will cause the for statement to execute only the counting portion of the loop, creating the illusion of a timing loop. The body of the loop will never be executed. Semicolons are required inside the parentheses of the for loop. The for loop is the only statement that requires such semicolon placement.

The while Structure within C++ Example 1: C++ source code: while loop_response = 'y'; while (loop_response == 'y') { cout << "\nWhat is your age? "; cin >> age_user; cout << "\nWhat is your friend's age? "; cin >> age_friend; cout >> "\nTogether your ages add up to: "; cout >> (age_user + age_friend); cout << "\nDo you want to do it again? y or n "; cin >> loop_response; }

For Loop Example //The Caesar cipher //The input string to encrypt Local StringVar inString := {Customer.Customer Name}; Local NumberVar shift := 5; Local StringVar outString := ""; Local NumberVar i; For i := 1 To Length(inString) Do ( Local StringVar inC := inString [i]; Local StringVar outC; Local BooleanVar isChar := LowerCase(inC) In "a" To "z"; Local BooleanVar isUCaseChar := isChar And (UpperCase (inC) = inC); inC := LCase(inC); If isChar Then ( Local NumberVar offset :=

(Asc(inC) + shift - Asc("a")) Mod (Asc("z") - Asc("a") + 1); outC := Chr(offset + Asc("a")); If isUCaseChar Then outC := UpperCase(outC) ) Else outC := inC; outString := outString + outC ); outString

lesson 8 The break Statement The break statement terminates the execution of the nearest enclosing loop or conditional statement in which it appears. Control passes to the statement that follows the terminated statement, if any. The continue Statement Forces transfer of control to the controlling expression of the smallest enclosing do, for, or while loop. The goto Statement Performs an unconditional transfer of control to the named label. The

You might also like