The Loop Control structure
The Loop Control structure

Types of Loop Control structure C++

Loops are basically certain conditions that repeats certain steps until the condition becomes false , i.e a loop will continue to go on until the condition is true as soon as it becomes false the loop will break .

TYPES OF LOOP IN C++

There are 3 types of loops :-

  • for loop
  • while loop
  • do-while loop

Loop Control structure: FOR LOOP

This loop is also known as counter controlled loop ,i.e a counter is set that for how many times the loop shall run and after that the condition will become false and the loop will break,

Syntax:

for(start,condition , increment/decrement)

{

-------------

--------------

}

start means from where to start off from i.e set the counter.

condition , here the condition is tested if its true then one can enter the loop else it wont.

increment/decrement here the counter is incremented or decremented according to the condition else how will the loop come to an end .

i.e if you wont increment or decrement the loop it will become an infinite loop

NOTE:

To come out of an infinite loop use ctrl+break+del

Lets see the example .

for loop structure
Output:
for loop
for loop

 

Loop Control structure: WHILE LOOP

This loop is also known as entry controlled loop because the condition is checked as the complier enters the loop . If the condition is true then it enters the loop and executes the steps else it just ignore the loop part and goes to the next part of the statement .

Syntax:

while(condition )

{

-------------

// while body

--------------

}

start means from where to start off from i.e set the counter.

condition , here the condition is tested if its true then one can enter the loop else it wont.

increment/decrement here the counter is incremented or decremented according to the condition else how will the loop come to an end .

i.e if you wont increment or decrement the loop it will become an infinite loop

Lets see the example .
while loop structure

 

Output:

 

while loop

Loop Control structure: DO-WHILE LOOP

This loop is also known as exit controlled loop because the condition is checked after the complier entered the loop . If the condition is true then it continue to run the loop and executes the steps . Here it enters the loop atleast once whether the condition comes out to be true or not , which was not the case with while or for loop.

Syntax:

do

{

-------------

// do-while body

--------------

} while (condition);

start means from where to start off from i.e set the counter.

condition , here the condition is tested if its true then one can enter the loop else it wont.

increment/decre-ment here the counter is incremented or decremented according to the condition else how will the loop come to an end .

i.e if you wont increment or decrement the loop it will become an infinite loop

Lets see the example .

do-while loop structure

Output:

do-while loop

Have something to add on Types of Loop Control structure ? Please add in comments.

Follow us on Facebook, Google Plus and Twitter to get more Tech News and reviews.

2 COMMENTS