Please enable JavaScript.
Coggle requires JavaScript to display documents.
Repetition control structure - Coggle Diagram
Repetition control structure
Components of loops statement
Initialization
As the loop starts initialization expression is
executed once. This expression initializes the loop.
Termination
The loop terminates if the value of termination
expression equal to false.
Increment-Decrement
With each iteration this expression is
executed. This expression can increment or decrement the value.
Types of looping
WHILE LOOP
Repeats a statement or group of statements while a given condition is true
• It tests the condition before executing the loop body
Syntax
DO WHILE LOOP
The do...while Loop is similar to while loop with one very important difference.
In while loop, check expression is checked at first before body of loop but in case of do...while loop, body of loop is executed first then only text expression is checked.
That's why the body of do...while loop is executed at least once.
Syntax
Differences
WHILE
The condition is evaluated first and if it is immediately false, the body is never executed
In the while statement, the conditional expression is evaluated at the top of the loop.
DO WHILE
The body is executed before the condition is tested and so guaranteed to be executed at least once.
In the do-while statement, the conditional expression is evaluated at the bottom of the loop.
FOR
Repeats the section of your program based on a specified number of
times.
For loop will be tested at the beginning of the loop structure.
If condition is FALSE, for loop is not executed.
Initialize is evaluated before the loop begins and it is one of assignment statement
Looping continues until the condition becomes TRUE
Every time the body of the loop repeats, the Counter executes,
usually increment /decrement.
Syntax
WHILE
FOR
DO WHILE
WHICH LOOP DO I USE?
WHILE
Definition: Pre-test loop that repeats until a specified condition is false
When to use: Use when you are not certain the number of times the loop should be executed or even if it should at all
DO-WHILE
Definition: Post-test loop that executes the loop before texting the condition, then repeats until the condition is false.
When to use: Use when you know that code must be executed at least once and possibly more times depending on the condition
FOR
Definition: Loop that contains an initialized counter and increments the counter with each run through the loop. Repeats until the condition is false.
When to use: Use when you need to execute a loop a specific number of times, or when you need to increment through a set of data. The counter an also be used as an index for accessing data one item at a time.