C Programming Loop Explain, Flowchart and Example

C Programming Loop Explain, Flowchart and Example

C Programming Loop Explain, Flowchart and Example
C Programming Loop Explain, Flowchart and Example

Explain 'for' loop. Draw the flowchart with a suitable example.

Explain loop in C prograaming is a repetition control structure that allows programms to write a loop that will be executed a specific number of times. for loop enables programmers to perform n number of steps together in a single line.

Syntax:

for(intialize expression; test expression; update expression)

{

 //

// body of for loop

}

 

1. Initialization Expression: In this expression, we assign a loop variable of loop counter to some value.

For example: int i=1;

2. Test Expression: In this expression, test conditions are performed. If the condition evaluates to true then the loop body will be executed and then an update of the loop variable is done. If the test expression becomes false then the control will exit from the loop.

For example: i<=10;

3. Update Expression: After execution of the loop body loop variable is updated by some value it could be incremented, decremented, multipied, of divided by any value.

For example: i++;

Flowchart:-

C Programming Loop Explain, Flowchart and Example

Example:-

#include<stdio.h>

#include<conio.h>

void main(){

int i;

for(i=0; i<=10; i++){

printf(“%d”,i);

}

getch();

}

Explain 'while' loop? Draw the flowchart with a suitable example.

While loop does not depend upon the number of iterations. In for loop the number of iterations was previously known to us but in the While loop, the execution is terminated on the basis of the test condition. If the test condition will become false then it will break from the while loop else body will be executed.

Syntax:-

initialization_expression;

while (test_expression)

{

        //body of the while loop

         update_expression;

}

Flowchart:-

C Programming Loop Explain, Flowchart and Example

Example:-

#include<stdio.h>

#include<conio.h>

void main(){

int i = 2; //Initialization expression

while(i <= 10) //Test expression

{

printf(“%d”,i);

i++; //Update expression

}

getch();

}

Explain 'do-while' loop? Draw the flowchart with a suitable example.

The do-while loop is similar to a while loop the only difference lies in the do-while loop test condition which is tested at the end of the body. In the do-while loop, the loop body will execute at least once irrespective of the test condition.

Syntax:-

initialization_expression;

do

{

update_expresion;

} while (test_expression);

Flowchart:-

C Programming Loop Explain, Flowchart and Example

Example:-

int i=2; //Initialization expression

do{

printf(“Hello World\n”); //Update expression

i++;

} while(i < 1); //Test expression

What is infinite loop?

An infinite loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an indefinite loop or an endless loop. It either produces a continuous output or no output.

An infinite loop is useful for those applications that accept the user input and generate the output continuously until the user exits from the application manually. In the following situations, this type of loop can be used:

1. All the operating systems run in an infinite loop as it does not exist after performing some task. It comes out of an infinite loop only when the user manually shuts down the system.

2. All the servers run in an infinite loop as the server responds to all the client requests. It comes out of an indefinite loop only when the administrator shut down the server manually.

3. All the games also run in an infinite loop. The game will accept the user requests until the user exits from the game.

We can create an infinite loop through various loop structures:

for infinite loop

for(; 😉

{

//body of the for loop.

}

while infinite loop

int i=0;

while(1)

{

i++;

printf(” i is :%d”,i);

}

do-while infinite loop

do 

{

//body of the loop.

}while(1);

Leave a Comment

Your email address will not be published. Required fields are marked *