Looping  is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

There are 3 type of basic loop-

(1)For Loop

(2)While Loop

(3)Do-While Loop

For Loop

  • Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

Syntax

for(initialization;condition;increment/decrement)
{
statement(s);
}

Flow Diagram

For loop

While Loop

  •  Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
  • In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute.
  • When condition returns false, the control comes out of loop and jumps to the next statement after while loop.

Syntax

while(condition)
{
statement(s);
}

Flow Diagram

While loop

Do-While Loop

  •  In Do-while,the statements inside loop execute and then the condition gets evaluated.
  •  If the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.

Syntax

do{
statement(s);
}
while(condition);

Flow Diagram

While loop

Loop Control Statement

  • Loop control statements change execution from its normal sequence.
  • When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

There are 2 type of Loop Control Statement-

(1)-break

(2)-continue

break​​​​​​​​​​​​​​

  • Sometimes,it is needed to stop a loop suddenly when condition is satisfief.This can be done with the help of break statement.
  • A break statement is used for unusual termination of a block.

Syntax

break;

Flow Diagram

 break statement

continue

  • The statement continue,is just opposite of break statement.
  • As soon as the continue statement is executed in a loop,the control skips rest of the statements for that value and resumes for the next iteration.

Syntax

continue;