What is For, While and do-while loop in c++.

What is For, While and do-while loop in c++.

What is a loop control statement?

Many times problem needs that a set of statements executed more than one time. Each time changing the value of the variable is so that every statement is different from the previous one.

This kind of repetitive execution of a set of statements is known as a loop. Looping involves repeating some portion of the program either a specified number of times or until a particular condition is true.

Types of loops

  1. For statement:- For statement is count controlled loop which means the program knows in advance how many times the statement has to be executed .

It's syntax

For(initialization; condition; updation){

    Statement block;}

Initialization:- Initialization means setting an initial value.

Condition:- Condition means whether the loop is going to execute or stop.

Updation:- incrementing or decrementing the value of the loop counter.

  1. While – loop:- while – loop statement is suitable for the program where it is not known in advance how many times the statement should be executed .

The while loop statement first checks the condition and then executes the program. If the condition is false the loop will not execute.

This type of loop is known as a pre-test loop.

It's syntax

While(condition){

   Statement block;

   i++;}
  1. do-while loop:- do–while loop is mostly the same as the while loop and is used in the problem where it is known in advance how many times the stamen should be execute.

The only difference is that it first executes the loop and then checks the condition.

do – while loop executes at least once.

That’s why it is known as the post-test loop.

It’s syntax

do{

statement block;

updation;

}while(condition)