0
0
C Sharp (C#)programming~5 mins

While loop execution model in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the basic structure of a while loop in C#?
A while loop repeatedly executes a block of code as long as its condition is true. The structure is:<br>
while (condition) {<br>  // code to repeat<br>}
Click to reveal answer
beginner
When does the condition in a while loop get evaluated?
The condition is checked before each iteration. If the condition is false at the start, the loop body does not run at all.
Click to reveal answer
intermediate
What happens if the while loop condition never becomes false?
The loop runs forever, causing an infinite loop. This can freeze or crash the program if not stopped.
Click to reveal answer
intermediate
How can you safely exit a while loop before the condition becomes false?
You can use the break statement inside the loop to exit immediately when a certain condition is met.
Click to reveal answer
intermediate
Explain the difference between while and do-while loops.
while checks the condition before running the loop body, so it may run zero times.<br>do-while runs the loop body first, then checks the condition, so it runs at least once.
Click to reveal answer
When is the condition in a while loop checked?
AAfter each iteration
BBefore each iteration
COnly once before the loop starts
DOnly once after the loop ends
What happens if the while loop condition is false at the start?
AThe program crashes
BThe loop body runs once
CThe loop runs infinitely
DThe loop body does not run at all
Which statement can be used to exit a while loop early?
Acontinue
Bexit
Cbreak
Dreturn
What is a risk of a while loop if the condition never becomes false?
AAn infinite loop occurs
BThe loop runs once
CThe loop runs zero times
DThe loop throws an error
How does a do-while loop differ from a while loop?
AIt runs the loop body at least once before checking the condition
BIt checks the condition before running the loop body
CIt cannot run infinitely
DIt does not use a condition
Describe how a while loop executes step-by-step in C#.
Think about what happens before and after each time the loop runs.
You got /4 concepts.
    Explain why it is important to update variables inside a while loop.
    Consider what happens if the condition never changes.
    You got /3 concepts.