While Loop

The while statement continually executes a block of code while a particular condition is true. It will continue to loop for as long as the condition remain true.

Syntax

while (boolean condition){
    //These statements will be executed
    //for as long as the condition is true
}

Example

This code snippet will count from 1 to 10 and print the output. We increment the value of count so that the condition will eventually become false and the loop will stop.

int count = 1;
while(count <= 10){
    //print value of count here
    count = count + 1;
}

Infinite Loop

This loop will never exit, since the condition is always true

while(true){
    //Continue running statements here
}

Example - Calculate the power of a number

TODO - Add the code here

results matching ""

    No results matching ""