<< Chapter < Page Chapter >> Page >

Loop while a condition is true

A while loop executes the statements in its body for as long as a specified condition evaluates to true. The general syntax of a while loop is shown in Figure 9 .

Figure 9 . General syntax for a while loop.
while(condition is true){ //Execute statements in body of loop.}//end while statement

When the condition is no longer true...

When the conditional expression evaluates to false, control passes to the next statement following the while loop.

while loops can be nested inside of other statements, including other while loops.

An infinite loop

As with all loop statements, you must be careful to make certain that the conditional expression eventually evaluates to false. Otherwise, control will be trapped inside the while loop in what is commonly called an infinite loop.

A simple while loop

The script in Listing 5 illustrates the use of a simple while loop.

Listing 5 . A simple while loop.
<!-- File JavaScript05.html --><html><body><script language="JavaScript1.3">cnt = 4//initialize a counter variable while(cnt>= 0){//begin while loop //display value of counterdocument.write("cnt = " + cnt + "<br/>") cnt = cnt - 1//decrement counter}//end while loop document.write("The End.");</script></body></html>

A counting loop

This script initializes the value of a counter variable named cnt to 4. Control continues to loop (iterate) for as long as the value of the counteris greater than or equal to zero. During each iteration of the loop, the current value of cnt is displayed and then the value of cnt is reduced by a value of 1.

Repeat the test

At that point, control returns to the top of the loop where the test is repeated. This process produces the first five lines of output text shown in Figure 10 .

Figure 10 . Output from script in Listing 5.
cnt = 4 cnt = 3cnt = 2 cnt = 1cnt = 0 The End.

When the test returns false...

When the test in Listing 5 returns false, meaning that cnt is no longer greater than or equal to zero, control exits the while loop and goes to the next statement following the while loop. This is the statement that calls the write method to display the last line of text in Figure 10 .

Programming errors

From time to time, we all make errors when writing scripts or programs. Typical errors include typing a period instead of a comma, failing to include amatching right parenthesis, etc. Usually, when you make a programming error using JavaScript, some or all of the script simply doesn't execute.

Finding and fixing the errors

The worst thing about programming errors is the need to find and fix them. The Firefox and Google Chrome browsers have easy-to-use mechanisms to helpyou identify the cause of the problem so that you can fix it. Internet Explorer probably has similar capability, but so far, I haven't figured out how to accessit.

My recommendation is to simply open the files containing your JavaScript code in either Firefox or Google Chrome. Or, you can open the file in InternetExplorer, but if it doesn't do what you expect it to do, open it again in Google Chrome or Firefox for assistance in finding and fixing the problem.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask