C Programming.


while loop statement in C tutorials
-
When a loop is described with a while statement , you can only specify the looping condition.
-
It is an entry controlled loop.
-
If the result of loop condition is true then the loop block is executed.
-
When the result of loop condition become false, it executes next statements, written after block scope.
Syntax:while( Condition ){Statements if condition is true;Statements if condition is true….}Statements if consition is false or when condition is completed. |
|
C Program to print no from 1 to 10 using while |
|
|
// Program name : test055.c//WAP to print no from 1 to 10 using while# include <stdio.h>#include <conio.h>void main(){int no=1;while(no<=10){printf("%d \n",no);no=no+1;}} |
Output |
12345678910 |
Explanation:
-
This program shows how a while statement is used.
-
The program starts by defining an integer variable no, having the value 1.
-
The next statement is a while statement that also sets up a loop.
-
The while statement says that , as long as the value of variable no is less than or equal to 10 the statements included in the braces are to be executed.
-
The printf statement written within the braces of while loop prints the current value of the variable number.
-
After printing value, we are increasing value of variable no by one each time the loop is executed.
-
After printing value 10 of number , when 1 is added to number , it becomes 11. At that time is not less than or equal to 10 , so the while loop is terminated and program ends here.