
So what is a loop ?
loops allow us to execute a statement or a block of statements multiple times. Like conditional statements, they are controlled by boolean expressions.
The while Statement
- A while statement has the following syntax:
- If the condition is true, the statement is executed
- Then the condition is evaluated again, and if it is still true, the statement is executed again
- The statement is executed repeatedly until the condition becomes false
while(condition){ statement(); }
The do Statement
The body of a do loop executes at least once
do { statement(); } while( condition );
Example :
object Demo { def main(args: Array[String]) { var x = 0; while (x < 10) { println("x = "+ x); x +=1; // x = x + 1 } var y = 0; do { println("y = " + y); y +=1; } while (y < 10); } }
Leave a Reply