What if your computer could do boring, repeated tasks for you perfectly every time?
Why While loop in Ruby? - Purpose & Use Cases
Imagine you want to count from 1 to 10 by writing each number on a piece of paper. Doing this by hand means writing each number one by one, which takes time and effort.
Manually writing or repeating the same steps over and over is slow and boring. It's easy to make mistakes, like skipping a number or writing the wrong one. If you want to count to 100 or more, it becomes a big hassle.
A while loop lets the computer repeat a task automatically as long as a condition is true. Instead of writing each step, you tell the computer to keep going until it reaches the goal. This saves time and avoids errors.
puts 1 puts 2 puts 3 puts 4 puts 5
i = 1 while i <= 5 puts i i += 1 end
It makes the computer do repetitive tasks quickly and correctly without needing you to write every single step.
Think about a game where you want to keep asking the player if they want to play again until they say no. A while loop can keep asking automatically until the player decides to stop.
Manually repeating tasks is slow and error-prone.
While loops automate repetition based on a condition.
This saves time and reduces mistakes in your programs.