Loop Control Statements in C Language with Flow Chart and Program



Loop control statements in C are designed to repeatedly execute operations until a specified condition is met. These Loops continue to run as long as the condition remains true. Loop control statements are used to repeat a set of statements. They are as follows ?

For Loop in C

The for loop in C allows us to repeat a set of statements a specified number of times. The for loop includes initialization, condition, and update statements within its syntax. It is used for iterating over vectors, arrays, and other data structures.

The syntax is as follows ?

for (initialization ; condition ; increment / decrement){
 body of the loop
}

The flow chart for loop is as follows ?

  • Initialization typically involves an assignment statement used to set the loop control variable.

  • The condition is a relational expression that determines when the loop will exit.

  • The increment/decrement part specifies how the loop control variable will change with each iteration is repeated.

  • The loop continues to execute as long as the condition is true.

  • Once the condition is false, the program continues with the next statement following the for loop.

Example

Following is the C program for loop control statement ?

#include <stdio.h>
void main( ){
   int k;
   for (k = 1; k<=5; k++){
      printf ("%d",k);
   }
}

When the above program is executed, it produces the following result ?

1 2 3 4 5

While Loop in C

The while loop in C determines a test condition and continues to execute the loop body as long as the condition is true. The loop exists when the condition becomes false. This is known as a pre-tested loop and it is often used when the number of iterations is not specified.

The syntax is as follows ?

while (condition){
 body of the loop
}

The flow chart for the while loop is as follows ?

  • Initialization is done before the loop.
  • The loop continues as long as the condition is true.
  • The increment and decrement part is done within the loop.

Example

Following is the C program for the while loop control statement ?

#include<stdio.h>
void main( ){
   int k;
   k = 1;
   while (k<=5){
      printf ("%d",k);
      k++;
   }
}

When the above program is executed, it produces the following result ?

1 2 3 4 5

Do-While Loop in C

A do-while loop is a control flow statement that executes a block of code and then repeats the block or exists the loop based on a given boolean condition. The construction includes a process symbol and a condition.

The syntax is as follows ?

Initialization do { body of the loop inc / dec }
while (condition);

The flow chart for the do-while loop is as follows ?

Example

Following is the C program for the do-while loop control statement ?

#include<stdio.h>
void main( ){
   int k;
   k = 1;
   do{
      printf ("%d",k);
      k++;
   }
   while (k <= 5);
}

When the above program is executed, it produces the following result ?

1 2 3 4 5
Updated on: 2024-12-12T17:08:18+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements