Looping control structures are used to avoid the repetition of the same code.
Parts of Looping Control Structure
It is a variable which controls and initial value to determine the number of repetition.
Every loop contains a test condition which checks whether the loup has to be defeated or terminated depending on the condition, control enters into the body of the loop and execute the condition become false.
Body of the Loop
It is a set of statements that are executed within the loop simultaneously.
It shows the increments or decrements of the control variable until the test condition becomes false.
Types of Looping Control Structure
A for loop is used to repeat a particular task for a finite number of times.
for ( initialisation expression (1); conditional expression (2); increment/decrement expression (3))
Body of loop
For loop also called an entry control blue because the body of the loop is executed only after the condition evaluates true. The loop will never be executed if the condition is false in the beginning.
Variations in For Loop
A for loop can have multiple variations that make it efficient.
Multiple Initialisation and Update Expression
A for loop may contain multiple initializations and update expressions. The multiple expression must be separated by commas.
a for loop contains three types of expressions initialization test and update expression. But all these expressions are optional i.e, be can skip any or all of these expressions depending upon the requirement of the program.
A for loop will execute its statement block an infinite number of times if we skip the condition. If the test condition is not present in the loo it will never return false. Hence it will execute in Leslie and become an infinite loop.
A loop without any statement in its body is a cold and empty statement.
Declaration of Variables in a Loop
We can declare and initialize a variable at the same time in for a loop.
The while loop is an entry controlled loop. While loop keeps on executing a block of statement specified test condition evaluates to true. when the test condition becomes falls the program control is transferred out of the loop and passes to the statement after the body of the loop.
Variation in the While Loop
A while loop can either be converted into an empty loop or an infinite loop.
While Loop as an Empty Loop
If we do not write any statement in the body of a while loop in the guns and empty loop. Such a loop can be used for the time delay.
While Loop as an infinite Loop
If we do not write increment/decrement statements in a while loop it becomes an infinite loop.
Do While Loop
Do while loop first executes the block of statement and then checks the condition.
While (test condition);