Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Real-Life Examples

Real-Life Examples

To showcase a practical application of the while loop, we’ve developed a straightforward “countdown” program:

Example

int countdown = 3;

while (countdown > 0) {
  printf(“%d\n”, countdown);
  countdown–;
}

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:

Example

Output “Yatzy!” if the dice number equals 6:

int dice = 1;

while (dice <= 6) {
  if (dice < 6) {
    printf(“No Yatzy\n”);
  } else {
    printf(“Yatzy!\n”);
  }
  dice = dice + 1;

For values ranging from 1 to 5, the loop prints “No Yatzy”. Upon encountering the value 6, it prints “Yatzy!”.