Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 09:38, 15 August 2025 by Dylan (talk | contribs) (new: info, example, while)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A do while loop will repeat (iterate) a code block while a condition is true, but will first check the condition after first running the codeblock.

This type of loop is known as indefinite iteration.

Example

The following C Sharp code will loop until the user enters 'stop', but the condition is only checked after the code block has first executed. This means that, even though it seems like the loop should stop right away as the condition is false, it still runs once (then the value is overwritten anyway):

String input = "stop";
do {
    input = Console.ReadLine();
} while (input != "stop")

While loop

If you do not want the code to run at least once before checking the condition but instead want to check the condition first, you can use a while loop.

See: While loop