Control Statements CSCI 125 & 161 / ENGR 144 • Statements that affect the sequence of Lecture 7 execution of other statements • Normal is sequential • May want to repeat statements - iteration Martin van Bommel • May or may not want particular statement executed at all - conditional Control Statement Structure Iteration and Loops • Control line - first line (e.g. while) • Iteration - process of repeating an operation • Body - statements enclosed in { } that are • Essential for large amounts of data - repeated repeating statements shortens programs • Loop - any portion of program repeated via • Body typically indented to show range of control statement control statement • e.g. while loop The while statement while operation • Condition test performed before every cycle, while ( condition ) { including first. statements • If condition is initially false, } body of loop not executed at all. where • Condition test performed only at beginning of condition tested before first and every iteration each loop cycle. • Even if condition becomes false during cycle, statements executed each time condition true cycle is completed. 1
while example Infinite loops • Sum up digits of a number • A loop cycle that never stops – infinite loop • Condition of while statement never false sum = 0; • e.g. while (n > 0) { while (n >= 0) { sum = sum + n % 10; sum += n % 10; n = n / 10; n /= 10; } } Repeat-N-Times Idiom Index Variable in Counting Loop • for (i = 0; i < N ; i++) for (i = 0; i < N ; i++) { • Variable i is called the index variable statements to be repeated • Any variable may be used - traditionally i } • Must be declared at beginning of function • May want to add up 10 input numbers • Initial value of i in loop is 0 - first cycle for (i = 0; i < 10; i++) • Next cycles value is 1, 2, etc. { • On last cycle, value is N – 1 cin >> value; • After loop, value of i is N sum = sum + value; } Non-Zero Counting Loop for statement • It is possible to modify for loop to begin at for ( init ; test ; step ) { another number statements for (i = first ; i <= last ; i++) } • Note the <= operator instead of < where • Value of i ranges from first to last init is an expression evaluated at beginning • Used if value of i required in body of loop test is a condition for continuing the loop step is an expression to prepare next cycle 2
for example for more • Countdown from 10 to 0 • Note that expressions init, test, and step are optional, but the semicolons must appear for (t=10; t >= 0; t--) { for (;;) - infinite loop cout << t; } for (;t<10;) same as while (t<10) cout << ”Liftoff!\n”; Nested for loops Conditional Execution • Create a 10 x 10 multiplication table • May want to execute a statement only if some condition applies for (row=1; row<=10; row++) { • May wish to choose between two for (col=1; col<=10; col++) { alternative courses of actions depending on cout << (row * col); some test } • Simplest form - if statement cout << endl; } if Statement Relational & Logical Operators == Equal != Not equal if ( conditional-test ) > Greater < Less { >= Greater or equal … statements executed if test true... <= Less or equal } else { } • Logical operate on results of comparisons … statements executed if test false... ! Not (true if operand false – one operand) } && And ( true if both true ) || Or ( true if either true ) 3
Recommend
More recommend