0
0
Cprogramming~5 mins

While loop in C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a while loop in C?
A while loop repeatedly executes a block of code as long as a given condition is true.
Click to reveal answer
beginner
How does the while loop check its condition?
Before each iteration, the while loop checks the condition. If it is true, the loop runs the code inside; if false, it stops.
Click to reveal answer
beginner
What happens if the while loop condition is false at the start?
The loop body does not run even once if the condition is false initially.
Click to reveal answer
beginner
Write a simple while loop that prints numbers 1 to 3.
int i = 1; while (i <= 3) { printf("%d\n", i); i++; }
Click to reveal answer
beginner
Why is it important to update the loop variable inside a while loop?
Updating the loop variable prevents an infinite loop by eventually making the condition false.
Click to reveal answer
What does a while loop do in C?
ARepeats code while a condition is true
BRuns code once regardless of condition
CRuns code only if condition is false
DRuns code a fixed number of times
When is the condition in a while loop checked?
ANever
BAfter each iteration
COnly once at the start
DBefore each iteration
What happens if the loop condition is false at the start?
ALoop runs once
BLoop body does not run
CLoop runs infinitely
DLoop runs twice
Which of these can cause an infinite while loop?
ANot updating the loop variable
BUpdating the loop variable correctly
CUsing <code>for</code> loop instead
DPrinting inside the loop
What is the output of this code? int i = 1; while (i <= 2) { printf("%d ", i); i++; }
A2 3
B1 2 3
C1 2
DNo output
Explain how a while loop works in C and why updating the loop variable is important.
Think about what happens if the condition never changes.
You got /3 concepts.
    Write a simple while loop in C that counts down from 5 to 1 and prints each number.
    Start with int i = 5 and decrease i each time.
    You got /4 concepts.