Skip to content

While Statement

IsaacShelton edited this page Mar 21, 2022 · 1 revision

While Statement

While statements are used to conditionally execute a block of code while a condition is true

while condition {
    // ...
}
while(condition){
    // ...
}
while condition,  // ...
while condition   // ...
while(condition)  // ...
while(condition), // ...

Execution Condition

If the condition is true, then the code inside the block will be executed or be re-executed

Usage example

import basics

func main {
    value int = scanInt("Please don't enter 10: ")
    
    while value != 10 {
        value = scanInt("Please don't enter 10: ")        
    }
    
    print("You entered 10, goodbye")
}

Break and Continue

Both break and continue apply to while statements

Loop Labels

Loop labels can be used for while loops by specifying a name for the loop label followed by : before the condition

while game_running : !game.closeWasClicked() {
    if game.isGameOver(), break game_running
}

While Continue

A version of while exists called while continue

x int = 0
while continue {
    x++
    if x < 10, continue
}

See while continue for more information

Clone this wiki locally