#Mastery22 – #TC1017

22 1017

Two types of repetition structures: pretest and posttest loops

Pretest:

  1. Loop condition appears at beginning of pretest loop
  2. Determines number of times instructions w/in loop body are processed

  1. while
  2. for

  1. Loop condition appears at end of posttest loop
  2. Determines number of times instructions w/in loop body are processed
  3. HOWEVER, instructions processed at least once–the first time!

  1. do…while while

  1. the name of a control variable (or loop counter)
  2. the initial value of the control variable
  3. the loop-continuation condition that tests for the final value of the control variable to determine when to exit
  4. the control variable to be incremented (or decremented) each time through the loop

Using while Loop:

    • Executes from zero to many times, depending on expression
    • while is reserved word
    • Syntax of while statement:
while (expression)
  statement;
  • Expression provides entry condition
  • Expression (parentheses must be included as part of syntax) must evaluate to true to invoke loop statement(s)
  • Statement(s) can be simple or compound (block)
  • Statement(s) is/are body of loop
  • BE SURE to include an exit condition that will eventually evaluate expression to be false
  • Infinite loop: statement(s) continue(s) to execute endlessly
  • Loop invocation:
  1. Statement(s) execute(s) if expression evaluates to true
  2. Loop condition reevaluated
  3. Statement(s) continue(s) to execute until expression false
    • Executes a set number of times determined by the counter
    • Statement can be a single statement or a compound (block) statement.
    • for is reserved word
    • Simplifies writing of count-controlled while loop
    • Syntax of for statement (for loop control statements):
for (initial statement; test statement (loop condition); update statement)
     statement;

CC BY 4.0 #Mastery22 – #TC1017 by Jose Eduardo Sanchez is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.