Learn Create Your Own Split Screen

Next Page

Here's an example of using a while loop.

First we "make a variable" called corner, and set it equal to zero.

var corner= 0;

Then we start a while loop, which is similar to a Scratch repeat block, saying we want to keep doing something for as long as our 'corner' variable is less than 100.

while (corner < 100) {
Note that we use parentheses (round brackets) around the "test condition", but braces (curly brackets) to show what group of statements we want to do while that condition holds true.

Inside the curly brackets, we stroke a rectangle, and change our corner variable by five. It keeps getting larger by five, and when it's no longer less than 100, the while-loop will finish.

ctx.strokeRect(corner, corner, 50, 50); corner= corner + 5; }