Flow Chart and Psuedo Code II

Embed Size (px)

Citation preview

Flow chart and Pseudocode for Repetition Introduction Repetition control structures while dowhile for

The Essentials of Repetition Loop Group of instructions the computer executes repeatedly while some loop-continuation condition remains true

Counter-controlled repetition Definite repetition; i.e., number of repetitions is known Control variable is used to count repetitions and incremented (or decremented) each time the group of instructions is performed

Sentinel-controlled repetition Indefinite repetition; i.e., number of repetitions is not known in advance Sentinel value indicates end of data It must be distinct from regular data items

Counter-controlled Repetition Counter-controlled repetition requires The name of a control variable (or loop counter) The initial value of the control variable The condition that tests for the final value of the control variable (when looping should terminate) The increment (or decrement) by which the control variable is modified each time through the loop

The while Repetition Structure Repetition structures Used to repeat specific actions while some condition remains true

The while repetition structure Repeat specific actions while some condition remains true Syntax: Placing a semicolon herewhile ( condition ) statement would generate a logic error!

How does it work? while loop is repeated until condition becomes false statement is executed when while loop repeats

In other words, while loop is repeated as long as condition remains true

The while Repetition Structure Example: Write a program segment that finds the first power of 2 larger than 1000 Pseudocode:initialize product to 2 while product is less than or equal to 1000 int product = 2; Names product, set product = 2 * product

Code in Java:int product = 2; while ( product