Programmer Coding

Looping in C Language

iterative Statements

Iteration statement is used to execute the statement repeatedly until the specified time.

The instruction is incorrect. C supports three types of iteration statements, also called loops

Types of Loop in C

  1. for loop
  2. while loop
  3. do–while loop
  • For loop

The for loop in C is a control flow statement that allows you to loop through a block of code one by one. This is especially important when you know exactly how many times you want to run a piece of code.

Example

#include <stdio.h>

int main() {

int i;

for (i = 1; i <= 5; i++) {

printf(“%d “, i);

}

return 0;

}

Output

1 2 3 4 5

  • While Loop

Loops generally consist of two parts: one or more control expressions which (not surprisingly) control the execution of the loop, and the body, which is the statement or set of statements which is executed over and over A while loop starts out like an if statement: if the condition expressed by the expression is true, the statement is executed. However, after executing the statement, the condition is tested again, and if it’s still true, the statement is executed again. (Presumably, the condition depends on some value which is changed in the body of the loop.) As long as the condition remains true, the body of the loop is executed over and over again

Example

#include <stdio.h>

int main() {

int i = 1;

while (i <= 5) {

printf(“%d “, i);

i++;

}

return 0;

}

Output

1 2 3 4 5

  • Do- While Loop

The do-while is necessary, or at least convenient, since at least one character must be installed in the array s, even if n is zero. We also used braces around the single statement that makes up the body of the do-while, even though they are unnecessary, so the hasty reader will not mistake the while part for the beginning of a while loop

Example

#include <stdio.h>

int main() {

int i = 1;

do {

printf(“%d “, i);

i++;

} while (i <= 5);

return 0;

}

Output

1 2 3 4 5

Leave a Comment

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

Scroll to Top