<meta http-equiv="refresh" content="1; url=/nojavascript/"> The While Loop | CK-12 Foundation
Dismiss
Skip Navigation
You are reading an older version of this FlexBook® textbook: Engineering: An Introduction to Solving Engineering Problems with Matlab Go to the latest version.

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
end

where

expression

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
end

This code creates the following output:

n =
1
n =
2
n =
3

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.

Image Attributions

Description

Authors:

Categories:

Grades:

Date Created:

Feb 23, 2012

Last Modified:

Dec 12, 2013
You can only attach files to None which belong to you
If you would like to associate files with this None, please make a copy first.

Reviews

Please wait...
You need to be signed in to perform this action. Please sign-in and try again.
Please wait...
Image Detail
Sizes: Medium | Original
 
CK.ENG.ENG.SE.1.Engineering-Matlab.8.1

Original text