While Loops

A while loop is a method of repeating a section of code until a condition of code is no longer true.

Here is an example:

var messagesToSend = 10;

while ( messagesToSend > 0 ) {
console.log("Minecraft is cool!");
messagesToSend -= 1;
}

The messagesToSend variable is called a control variable. It starts off at 10 and is decreased by one each time we send a message.

The condition which controls this loop appears between the ( )
It acts in the same way as an if() condition but the code continues to execute until it is no longer true.
In this case we execute this code until messagesToSend becomes 0 or less.

INFINITE LOOPS: if the condition in the () will never be false the loop will run forever and crash your function.

While Loops courses

JavaScript Game Design

Learn the fundamentals of JavaScript by building your own game

Exercises: While Loops

Exercises: While Loops

Exercises: While Loops

Exercises: While Loops

Exercises: While Loops

Exercises: While Loops

Exercises: While Loops

Exercises: While Loops

Fundamentals in Minecraft

Learn programming fundamentals in Minecraft

Exercises: While Loops

While Loops lessons

Wall Collision

Use loops to prevent the player from walking through wall objects

While Loops: Scaling vs tiling

Creating a function which automatically tiles images is very helpful for game design

While Loops: Bulls-eye

Draw a series of concentric circles using a while loop

While Loops: Rainbow

Reverse engineer this picture of a rainbow

Nested While Loops

A Nested while loop is a while loop enclosed within another while loop

While Loops Introduction

Learn the basics of repeat structures by completing some while loop exercises

While Loops: Counting Backwards

You've seen how to count forwards, how can you change it to count backwards

While Loops: Iterating through a range

To iterate is to count up by a certain amount. You can iterate by 2's: 0,2,4,6,8... You can iterate by 1s: 0,1,2,3,4... etc

While Loops: More Iteration

There are more ways to iterate than adding numbers...

Pig Stacking Plugin

Use loops to place a pile of entities atop one another.

While Loops Introduction

Learn the basics of repeat structures by completing some while loop exercises

Related Links

No links found
About While Loops

Repeat a section of code while a condition is true

Badge

While Looper

Rank Requirements