Sunday 9 May 2021

Looping statements or iterative statements in C with examples

When a single statement or a group of statements will be executed again and again in a program then such type of process is called looping or iteration.

C language provides three types of looping statements

  1. For loop
  2. While loop
  3. Do-while loop

1 For lopping in C

It is looping statement which repeat a statement or group of statements until condition becomes false. The for loop is used if know in advance how many times the loop will iterate. That is, if you want to execute a loop in 10 times, 20, 100 and so on.


 

Syntax:

for(initialization statement; test condition; update statements)

{

body of the loop

}

statement-x;

The for loop works as follows 

The initialization statement is executed only ones' at the beginning of the for loop.

Then the test condition / test expression is evaluated; if it is true, the body of the loop followed by update statement are executed and control is transferred to test condition.

One again the test expression is evaluated and if it is true the body of the loop and update statements are executed once again. This process continues until test expression results becomes false.

Example

void main ()  
    {  
        int i;  
        for(i=0;i<10;i++)
        {  
            int i = 20;  
            printf("%d ",i);  
        }  
    }  

2. While looping in C

The while loop is best suited to repeat statements or set of statements as long as some condition is satisfied. The while is more suitable, if you don't know in advance how many times the loop will execute.

Syntax:

while(test condition)

{

body of the loop

}

The while loop works as follows 

It is an entry controlled loop; it test the condition and of the condition is true, then the body of the loop is executed.

After execution of the body, the test condition is once again evaluated and of it is true, the body is executed once again.

This process continues until the test condition becomes false ad the control is transferred to out of the loop.

Example

int main(){    
int i=1;      
while(i<=10){      
printf("%d",i);      
i++;      
}  
return 0;  
}

3. Do-while looping in C

It is an exist controlled loop. do-while is useful where you need to run the loop body minimum one time irrespective if the condition status.

syntax:

do

{

body of the loop

}while( test condition);

The semicolon at the end of the syntax is compulsory, if you remove it it will generate an error.

The do-while loop first executes loop body without testing the condition. If the condition is true, the loop body executed once again otherwise the control is transferred out of the do-while.

 Example

int main(){    
    int i=1;      
    do{    
    printf("%d \n",i);    
    i++;    
    }while(i<=10);   
    return 0;  
    }     

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top