What is looping statement? Discuss different looping statements with suitable example of each.

This answer is restricted. Please login to view the answer of this question.

Login Now

Loop may be defined as a block of the statement which is repeatedly executed for a certain number of times or until a particular condition is satisfied. When an identical task is to be performed for a number of times, then the loop is used.

For example, When we have to print the numbers from 1 to 100. We can use a loop to print the number from 1 to 100.

We have three types of loop.

1. For Loop

For loop is useful to execute a statement for a number of times. The syntax of using for loop is

for(counter initialization, test condition, increment or decrement)
{
    statement; or block of loop
}

Flowchat

User Loaded Image | CSIT Guide

Example:

#include <stdio.h>  

int main(){
   for(int i=1; i<=10; i++){
      // This statement would be executed repeatedly until the condition  i<=10 returns false. 
      printf("The value of i = %d\n", i);
   }
   return 0;
}

The output of above program is

The value of i = 1
The value of i = 2
The value of i = 3
The value of i = 4
The value of i = 5
The value of i = 6
The value of i = 7
The value of i = 8
The value of i = 9
The value of i = 10

2. While Loop

The syntax of while loop is:

while(test condition)
{
    //body of loop
}

User Loaded Image | CSIT GuideThe test condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body once, the test-condition is again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test-condition finally becomes false and the control is transferred out of the loop. On exit, the program continues with the statement immediately after the body of the loop.

Example:

#include <stdio.h>

int main(){
   int i=1;
   // The loop would continue to print  the value of i until the given condition  i<=10 returns false
   while(i<=10){
      printf("The value of i = i", i);
      i++;
   }
}

The output of the above program is:

The value of i = 1
The value of i = 2
The value of i = 3
The value of i = 4
The value of i = 5
The value of i = 6
The value of i = 7
The value of i = 8
The value of i = 9
The value of i = 10

3. Do While Loop

The syntax of do while loop is:

do
{
   statement;
}while(test condition);

User Loaded Image | CSIT GuideIn the do while loop, the body of the loop is executed first without testing condition. At the end of the loop, the test condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition becomes false, the loop is terminated, and the control goes to the statement that appears immediately after the while statement.

Example:

#include  <stdio.h>

int main(){
   int i=1;
   do{
      printf("The value of i = i", i);
      num++;
   }while(i<=10);
   return 0;
}

The output of the above program is:

The value of i = 1
The value of i = 2
The value of i = 3
The value of i = 4
The value of i = 5
The value of i = 6
The value of i = 7
The value of i = 8
The value of i = 9
The value of i = 10

Source: https://codehelppro.com/detail/c-programming/for-loop, https://codehelppro.com/detail/c-programming/while-loop

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .