Looping#
do-while
while
for
while
statement#
Repeating an operation in a fast manner is a superpower of a computer. while
repeats a block of operations as long as the given condition is true
.
var solution = 84;
Console.WriteLine("Guess the number!");
var guess = int.Parse(Console.ReadLine());
while (solution != guess)
{
Console.WriteLine($"Is {(solution > guess ? "greater" : "less")}. Try again.");
guess = int.Parse(Console.ReadLine());
}
Console.WriteLine("🎉");
Activity 16
Is a
do-while
loop better suited for the program above? Why? Why not?Rewrite the code as a
do-while
loop and compare your results.
Question to ponder
How can we represent a while loop in a flowchart?