loop in programming

loop in programming

This is our third c++ tutorial . you can also read our previous c++ tutorials

part 1    introduction to c+

part 2   types of data

In this article you are going to learn loop in  c++.

loop
loop

loop in programming

Loop are used to execute   statement a definite number of time, while condition is true. When condition become false , the loop stop and control pass to the statements following the loop.

There are three kinds of loops in c++

 for loop

 while loop

 do loop

for loop c++
for loop c++

for loop

The for loop  c++ executes a piece of code a secure number of times . for loop is usually used when you know exact number of iteration.  The for loop c++ is easiest loop to understand  concept of loops in Programming language .

Here is simple for loop  c++ program.

 

 

How does this work ? The for loop statement controls the loop. It consists of keyword for, followed by parentheses that contain three expressions separated by semicolons :

for(x=0; x<30 ; x++)

there are three statements in above code , initialization declaration , conditional declaration , and increment  declaration .

The initialization statement in for loop

In initialization declaration in for loop we assign initial value to variable , in above code x is variable and 0 is assign to its. Simply its mean loop will start from 1 .The initialization declaration is performed only once, when loop start.

Condition statement  in for loop:

The condition statement in for loop usually involve a relational operator . it is evaluated each time through the loop , just before body of loop is executed. Every time it check whether loop will be executed again . if it is true , loop is performed one time more. If it is false , loop ends.

Increment  Statement in for loop:

Increment statement for loop c++ always executed at end of loop , after loop body has been performed . in above code increment operator ++ to adds 1 to x each time through loop .

How many time loop will be execute?

Loop in example program executes exactly 30 times. The first time , x is 0 . this is initial value of loop . the last time through loop , x is 29 . this is determined by conditional statement x<30 . When x becomes 30 , loop terminate ; loop body is not performed when j has this value.

 

while loop
while loop

while loop

for loop is used when we know particular number of repetition . but if you don’t identify how many times you want to do something before you start loop? In this case a different kind of loop may be used while loop

.

here is simple code of program  while loop

 

Above while loop program prompt  the user to enter a sequence of numbers. if number entered is 0 , program will dismisses.  point to be notice in  given  program  is that there is no way for program to know in advance how many numbers will be typed before 0 appears ;  it depend on user.

It will be executed as long as conditional statement is true.

 

do while loop
do while loop

do loop 

while loop in programming , conditional statement is evaluated at beginning of loop. If test expression is false when loop is entered , loop body won’t be executed at all. But in some situation ,you want to execute loop at least once.  In that kind of case, you should use do  loop.

here is code of do while loop

 

 

Summary :

In this article you have learnt about loop in Programming.  A question may be arise in your mind when to use which loop? . for loop is appropriate when you know in advance how many times loop will be executed. The while loop and do while loop are used when you don’t know in advance when loop will terminate , do while loop when you are sure you want to execute loop body at least once.

Thanks for reading , please share it with your friends.

read our next article :

if else statement

1 thought on “loop in programming”

Leave a Comment