Skip to content

Repeat with while

You can repeat code using a while loop. This tests a condition at the start of the instructions. If the condition is true, then the loop is entered and runs to its end before returning to the condition to see if the instructions should be run again.

Example while loop

Example

using static SplashKitSDK.SplashKit;
using static System.Convert;
string line;
int value;
int i;
Write("Enter a number: ");
line = ReadLine();
value = ToInt32(line);
// Use a counter variable (i for historic reasons)
i = 0;
// Loop while i is less than the user's value
while( i < value )
{
WriteLine("Hello World! " + i);
// Making sure to change i so that we eventually end!
i++; // i = i + 1;
}
// For graphical applications
OpenWindow("Test Window", 400, 300);
// You can loop while the user has not asked to quit
while( ! QuitRequested() )
{
// We can update what the user sees...
FillCircle(RandomColor(), Rnd(400), Rnd(300), Rnd(50));
RefreshScreen();
// Then check if the quit condition has changed
// Inside ProcessEvents, SplashKit checks if the user has asked to quit
// So this will ensure that `QuitRequested` will eventually be true.
ProcessEvents();
}