Loop Control Statements by G Krishna Chauhan



You may encounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times.Suppose that you have to print table of 2, then you need to write 10 lines of code. By using the loop statement, you can do it by 2 or 3 lines of code only.


Types of Loop:

while loop:


Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. The syntax of while loop in c language is given below:


while(condition)
{  
//code to be executed  
}

Flowchart of while loop:



 Example Program:

#include <stdio.h>      
#include <conio.h>      
void main()
{      

int i=1;    
clrscr();          
while(i<=10)
{    

printf("%d \n",i);    
i++;    
}         
getch();      
}      

Output:

1
2
3
4
5
6
7
8
9
10



do...while loop:

It is more like a while statement, except that it tests the condition at the end of the loop body. The syntax of do-while loop in c language is given below:

do
{  
//code to be executed  
}
while(condition);  

Flowchart of do while loop:

 Example Program:

#include <stdio.h>    
#include <conio.h>    
void main()
{    
int i=1;  
clrscr();    
do
{  
printf("%d \n",i);  
i++;  
}
while(i<=10);  
getch();    
}  

Output:

1
2
3
4
5
6
7
8
9
10


for loop:


Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.The syntax of for loop in c language is given below:

for(initialization;condition;incr/decr)
{  
//code to be executed  
}  

Flowchart of for loop:



 Example Program:

#include <stdio.h>      
#include <conio.h>      
void main()
{      
int i=0;    
clrscr();      
for(i=1;i<=10;i++)
{    

printf("%d \n",i);    
}    
getch();      

}      


Output:

1
2
3
4
5
6
7
8
9
10

nested loops:

You can use one or more loops inside any other while, for, or do..while loop.C programming allows to use one loop inside another loop. 

The syntax for a nested while loop statement in C programming language is as follows −

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

Flowchart of Nested while loop:




The syntax for a nested do...while loop statement in C programming language is as follows −


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

Flowchart of Nested do while loop:


The syntax of for loop in c language is given below:


for ( init; condition; increment ) 
{
for ( init; condition; increment ) 
{
statement(s);
}
statement(s);
}


Flowchart of nested for loop:


 Example Program:

#include <stdio.h>
#include <conio.h>

int main ()
{
int i, j;
for(i = 2; i<100; i++)
{
for(j = 2; j <= (i/j); j++) 
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) printf("%d is prime\n", i);
}
return 0;
}

No comments:

Post a Comment