Loop In C
The looping can be defined as repeating the same
process multiple times until a specific condition
satisfies. It is known as iteration also.
2
What is Loop
3
Type of Loop
While
Loop
Do-
While
Loop
For
Loop
There are Three types of Loop. They Are..
While Statement
4
4
The while loop loops through a block of code if a specified condition
is true.
Syntax:
while (condition)
{
// code block to be executed
}
Example:
In the following example, the code in the loop will
run, repeatedly, if a variable (i) is less than 10:
while(i<10)
{
printf("%d n",i);
i++;
}
The do while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat
the loop as long as the condition is true.
Syntax:
do {
// code block to be executed
}
while (condition);
Example: In the following example, the code in the
loop will run, repeatedly, if a variable (i) is less than 10:
do {
printf("%d n",i);
i++;
}
while(i<=10);
5
Do-While Statement
6
For Statement
When you know exactly how many times you want to loop
through a block of code, use the for loop instead of a while
loop:
Syntax:
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
Example: In the following example, the code in the loop will
run, repeatedly, if a variable (i) is less than 10:
for (i = 0; i < 10; i++)
{
printf("%dn", i);
}
7

C Programming Loop

  • 1.
  • 2.
    The looping canbe defined as repeating the same process multiple times until a specific condition satisfies. It is known as iteration also. 2 What is Loop
  • 3.
  • 4.
    While Statement 4 4 The whileloop loops through a block of code if a specified condition is true. Syntax: while (condition) { // code block to be executed } Example: In the following example, the code in the loop will run, repeatedly, if a variable (i) is less than 10: while(i<10) { printf("%d n",i); i++; }
  • 5.
    The do whileloop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. Syntax: do { // code block to be executed } while (condition); Example: In the following example, the code in the loop will run, repeatedly, if a variable (i) is less than 10: do { printf("%d n",i); i++; } while(i<=10); 5 Do-While Statement
  • 6.
    6 For Statement When youknow exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: Syntax: for (statement 1; statement 2; statement 3) { // code block to be executed } Example: In the following example, the code in the loop will run, repeatedly, if a variable (i) is less than 10: for (i = 0; i < 10; i++) { printf("%dn", i); }
  • 7.