As we discussed before the loop have three parts.
- Initialization
- Condition Checking
- Increment Or Decrement Part
while loop is known as entry control loop, in this if the condition is true then the statement inside loop will be executed.
Syntax
variable=0; while(condition) { code statement variable++; or variable--; //increment or decrement part }
Example is below
#include<stdio.h> #include<conio.h> void main() { int i = 0; // declaration and initialization at the same time clrscr(); printf("\nPrinting numbers using while loop from 0 to 9\n\n"); /* while i is less than 10 */ while(i<10) { printf("%d\n",i); i++; // same as i=i+1; } getch(); }
OutPut
Printing numbers using while loop from 0 to 9 0 1 2 3 4 5 6 7 8 9