While Loop in C Language with Examples

While Loop in C Language with Examples



while:
'while' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
Syntax:
while(condition)
{
    statement 1
    statement 2
    ..
    ..
}


In the above syntax if the condition is true the statements of while block is execute infinite times, if the condition is false while block terminated.

Display 1 to 10 values using while loop

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

Output:

1
2
3
4
5
6
7
8
9
10


More Programs

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

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

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

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

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

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

Nested while loop
One while loop within another while loop is knows as Nested while Loop Syntax:
while(condition)
{
    while(condition)
    {
         statement 1
         statement 2
    }
    statement 3
    statement 4
      
}



#include <stdio.h>
                                                                         
int main() {
    int x=1,y;
    while(x<=6)
    {
         y=1;
         while(y<=3)
         {
             printf("*");
             y++;
         }
         printf("\n");
         x++;
    }
    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