Computer

Looping

Looping

Looping control structures are used to avoid the repetition of the same code.

Parts of Looping Control Structure

Control Variable

It is a variable which controls and initial value to determine the number of repetition.

Test Condition

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.

Step Value

It shows the increments or decrements of the control variable until the test condition becomes false.

Types of Looping Control Structure

Looping

For Loop

A for loop is used to repeat a particular task for a finite number of times.
Syntax
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 .

Variations in For Loop

A for loop can have multiple variations that make it efficient.

Multiple Initialisation and Update Expression

A for loop may multiple initializations and update expressions. The multiple expression must be separated by commas.

Optional Expressions

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.

Infinite Loop

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.

Empty 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.

Loop

A loop can be applied to a program where a number of iterations are not fixed.
Syntax
Initialisation expression;
(test condition)
{
statement ;
}

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.
Syntax
Initialisation expression;
do
{
statements
}
While (test condition);

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Study Worms Would you like to receive notifications on latest updates? No Yes

Adblock Detected

Please consider supporting us by disabling your ad blocker