Description
While loop statement is used when programmer doesn't knows how many times the code will be executing before starting the loop, as it depends upon the users input.Syntax
while (test expression is true){
statement;
statement;
}
Executes the loop till the test expression condition is true.
Source code
- //Using while loop statement
- #include <stdio.h>
- #include <conio.h>
- void main()
- {
- int a=5;
- clrscr();
- while(a!=0)
- {
- scanf("%d",&a);
- }
- getch();
- }
Program working steps
First 7 lines of the program are common about comment, preprocessor directives, void main function, initialization and clearing the console screen.As we have declared the value of a = 5, the while loop statement test expression results true entering the program in while loop body asking for the user to enter the number using scanf statement.
When number entered by the user is 0, the loop and program terminates.
Thank you for sharing Your Knowledge with others who need It.-Team LetML
Post a Comment