(^222) | File Objects and Looping Statements
The body of a loop can consist of a block of
statements, which allows us to execute any group
of statements repeatedly. Typically, we use while
loops in the following form:
while(Expression)
{
.
.
.
}
In this structure, if the expression is true, the
entire sequence of statements in the block is exe-
cuted, and then the expression is checked again. If
it is still true, the statements are executed again. The cycle continues until the expression
becomes false.
Although in some ways the ifand whilestatements are alike, there are fundamental dif-
ferences between them (see Figure 5.5). In the ifstructure, Statement1 is either skipped or
executed exactly once. In the whilestructure, Statement1 can be skipped, executed once, or
executed over and over. The ifis used to choosea course of action; the whileis used to repeat
a course of action.
Statement 1
Statement 2
while( Expression )
Figure 5.4 whileStatement Flow of Control
Statement 1
Statement 2
Statement 1
Statement 2
if( Expression ) while( Expression )
ifff((thenn)) Statement while Statement
Figure 5.5 A comparison of ifand while
やまだぃちぅ
(やまだぃちぅ)
#1