0
0
Cprogramming~15 mins

While loop in C - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a While Loop in C
📖 Scenario: You are helping a cashier count the number of customers entering a store. The cashier wants to keep track until 5 customers have entered.
🎯 Goal: Build a C program that uses a while loop to count from 1 to 5 and print each number, simulating counting customers.
📋 What You'll Learn
Create an integer variable called count and set it to 1
Create an integer variable called max_customers and set it to 5
Use a while loop with the condition count <= max_customers
Inside the loop, print the current value of count
Inside the loop, increase count by 1
After the loop, print "Counting complete!"
💡 Why This Matters
🌍 Real World
Counting events or items repeatedly until a condition is met is common in many real-world tasks, like counting customers, steps, or inventory.
💼 Career
Understanding loops is essential for programming jobs, as loops help automate repetitive tasks efficiently.
Progress0 / 4 steps
1
Create the count variable
Create an integer variable called count and set it to 1.
C
Need a hint?

Use int count = 1; to create the variable.

2
Create the max_customers variable
Add an integer variable called max_customers and set it to 5 below the count variable.
C
Need a hint?

Use int max_customers = 5; to create the variable.

3
Write the while loop
Write a while loop with the condition count <= max_customers. Inside the loop, print the value of count using printf, then increase count by 1.
C
Need a hint?

Use while (count <= max_customers) and inside the loop use printf("%d\n", count); and count++;.

4
Print completion message
After the while loop, add a printf statement to print "Counting complete!".
C
Need a hint?

Use printf("Counting complete!\n"); after the loop.