To showcase a practical application of the while loop, we’ve developed a straightforward “countdown” program:
int countdown = 3;
while (countdown > 0) { printf(“Happy New Year!!\n”); |
To illustrate a practical use case of the while loop in conjunction with an if…else statement, let’s consider a game of Yatzy:
Output “Yatzy!” if the dice number equals 6:
int dice = 1;
while (dice <= 6) { |
For values ranging from 1 to 5, the loop prints “No Yatzy”. Upon encountering the value 6, it prints “Yatzy!”.