<img src="https://d5nxst8fruw4z.cloudfront.net/atrk.gif?account=iA1Pi1a8Dy00ym" style="display:none" height="1" width="1" alt="" />
Skip Navigation

8.1: The While Loop

Difficulty Level: At Grade Created by: CK-12
Turn In

The while loop is similar to the for loop in that it allows the repeated execution of statements. Unlike the for loop, the number of times that the statements in the body of the loop are executed can depend on variable values that are computed in the loop. The syntax of the while loop has the following form:

While expression
% Command 1
% Command 2
% More commands to execute repeatedly until expression is not true



is a logical expression that is either true or false. (Information about logical expressions is available in the Logical Expressions subsection of the Conditionals section.) For example, consider the following while loop:

n = 1
while n < 3
n = n + 1

This code creates the following output:

n =
n =
n =

Note that in all of this example, the commands inside the while loop are indented relative to the while and end statements. This is not required, but is common practice and makes the code much more readable.

Notes/Highlights Having trouble? Report an issue.

Color Highlighted Text Notes
Please to create your own Highlights / Notes
Show More

Image Attributions

Show Hide Details
Date Created:
Feb 23, 2012
Last Modified:
Apr 13, 2016
Save or share your relevant files like activites, homework and worksheet.
To add resources, you must be the owner of the section. Click Customize to make your own copy.
Please wait...
Please wait...
Image Detail
Sizes: Medium | Original