Recall & Review
beginner
What is a while loop in Ruby?
A while loop repeats a block of code as long as a given condition is true. It checks the condition before each repetition.
Click to reveal answer
beginner
How do you write a simple while loop in Ruby that counts from 1 to 5?
You can write:<br>
i = 1 while i <= 5 puts i i += 1 end<br>This prints numbers 1 to 5.
Click to reveal answer
intermediate
What happens if the condition in a while loop is never false?
The loop runs forever, causing an infinite loop. This can freeze your program unless you stop it manually.
Click to reveal answer
beginner
Can you use a while loop with a condition that changes inside the loop? Give an example.
Yes. For example:<br>
count = 3 while count > 0 puts count count -= 1 end<br>The condition changes as count decreases.
Click to reveal answer
intermediate
What is the difference between a while loop and an until loop in Ruby?
A while loop runs while the condition is true.<br>An until loop runs until the condition becomes true (runs while condition is false).
Click to reveal answer
What does a while loop do in Ruby?
✗ Incorrect
A while loop repeats the code block as long as the condition remains true.
What will this code print?<br>
i = 1 while i < 3 puts i i += 1 end
✗ Incorrect
The loop runs while i is less than 3, so it prints 1 and 2.
What happens if the condition in a while loop is false at the start?
✗ Incorrect
If the condition is false initially, the while loop does not run even once.
Which of these can cause an infinite while loop?
✗ Incorrect
If the condition never becomes false, the loop never stops and runs forever.
How do you stop a while loop early in Ruby?
✗ Incorrect
The break keyword immediately exits the loop.
Explain how a while loop works in Ruby and give a simple example.
Think about counting numbers using a variable.
You got /3 concepts.
What are the risks of using a while loop and how can you avoid them?
Consider what happens if the condition never changes.
You got /3 concepts.