Loop in C with Examples: For, While, Do..While Loops

Loop in C with Examples: For, While, Do..While Loops



Looping Statements In C
Looping Statements are used to execute a statement several number of times based on the condition.In java language these are classified into following types.

1.For Loop
2.While Loop
3.Do While Loop
for:
'for' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
Syntax:
for(initilization;condition;increment/decrement)
{
    statement 1
    statement 2
    ..
    ..
}


In the above syntax
initilization is a starting value
condition is a ending value
increment/decrement is incrementation(++) or decrementation(--)


Display 1 to 10 values using for loop

#include <stdio.h>
                                                                         
int main() {
    int x;
    for(x=1;x<=10;x++)
    {
        printf("%d\n",x);
    }
    return 0;
}
                                            

Output:

1
2
3
4
5
6
7
8
9
10


More Programs

Write a C-program to print 1 to 10 values using for loop

Write a C-program to print 10 to 1 values using for loop

Write a C-program to print 1 to n values using for loop

Write a C-program to print even numbers using for loop

Write a C-program to print even number or odd number based on the n value using for loop

Write a C-program to print even number or odd number based on the n value using single for loop

Write a C-program to count the number of divisible numbers in a given range(1-100)

Write a C-program to print count the number of divisible numbers in a given range(1-100) without using loop

Write a C-program to print factorial number using for loop

Write a C-program to print factorial of n numbers using for loop

Write a C-program to print sum of natural numbers using for loop

Write a C-program to print sum of even numbers using for loop

Write a C-program to print sum of odd numbers using for loop

Write a C-program to print sum of even or odd numbers based on the n value using for loop

Write a C-program to print sum of even or odd numbers based on the n value using single loop

Write a C-program to display table using for loop

Write a C-program to print a to z

Write a C-program to print ASCII values of a to z

Nested For loop
One for loop within another for loop is knows as Nested For Loop Syntax:
for(initilization;condition;increment/decrement)
{
    for(initilization;condition;increment/decrement)
    {
         statement 1
         statement 2
    }
    statement 3
    statement 4
      
}



#include <stdio.h>
                                                                         
int main() {
    int x,y;
    for(x=1;x<=6;x++)
    {
         for(y=1;y<=3;y++)
         {
             printf("*");
         }
         printf("\n");
    }
    return 0;
}
                                            

Output:

***
***
***
***
***
***


More Programs

Write a C-program to print ASCII values of A to Z

Write a C-program to print ASCII values of Numbers