while loop
When we exit any game then we watch some text like this –
Abort game : y/n
Are you really exit the game : Y/N etc.
Here if user press ‘Y’ then the game will be exit neither continue. Actually in our program behave of while loop like this.
Structure of while loop :
while (condition) { Statement 1; ---------------- Statement n; } |
At first computer work from the condition parts. Here we use the conditional expression. If value of the condition is not 0 or false then the statement will be evaluated neither not.
Example :
While (ch != ‘q’) { printf(“You did not press q”); } printf(“You press q”); |
Here if you did not press q key then the statement will execute neither the second printf() function execute.
#include <stdio.h> void main() { printf(\n Press any key without N or n for a noisy sound”); ch = getche(); //read character and store it to ch while ((ch != ‘N’ ) && (ch != ‘n’)) { putch(‘\a’); //echo by speaker putch(‘\b’); // backspace ch = getche(); // read character and store to ch } printf(“\n\n\n Thank you for not hearing a noisy sound”); } |
After run this program, when you type any key without ‘N’ or ‘n’ then the speaker of computer make a bip sound but when you press just “n’ or ‘N’ then no sound occurred and the while loop condition is false then while loop is stop and “printf(“\n\n\n Thank you for not hearing a noisy sound”);”
this statement will execute.
Written by ‘Shojib
No comments:
Post a Comment