While loop: Difference between revisions
From Coderwiki
More actions
new: info, example, do while |
replace 'String' with 'string' in code example |
||
| Line 5: | Line 5: | ||
== Example == | == Example == | ||
The following [[C Sharp]] code will loop until the [[User input|user enters]] 'stop':<syntaxhighlight lang="cs" line="1"> | The following [[C Sharp]] code will loop until the [[User input|user enters]] 'stop':<syntaxhighlight lang="cs" line="1"> | ||
string input; | |||
while (input != "stop") | while (input != "stop") | ||
{ | { | ||
Latest revision as of 11:13, 15 August 2025
A while loop will repeat (iterate) a code block while a condition is true.
This type of loop is known as indefinite iteration.
Example
edit edit sourceThe following C Sharp code will loop until the user enters 'stop':
string input;
while (input != "stop")
{
input = Console.ReadLine();
}
Do while loop
edit edit sourceIf you want the code to run at least once before checking the condition, you can use a do while loop.
See: Do while loop