Quick Index
What Is A Loop Statement?
Algorithmic Perspective
Everyday Loops
While True
What Is A Loop Statement?
Recall the definition of
condition
.
A
loop statement
is a conditional expression followed by a code block that executes
while
the conditional expression is true.
The next examples will explain.
Algorithmic Perspective
General
While the condition (expression) is true code "A" repeatedly evaluates (runs).
While (CONDITION)
{
//CODE "A"
}
📋
Specific Example
The condition is "a > b".
Thus, "while" the element "a" is greater than the element "b", then code "A" repeatedly evaluates (runs).
While (a > b) { //CODE "A" }
Everyday Loops
While
the
condition
is true we continue with the action.
After the action, the incrementor moves to the next.
The loop terminates when the condition goes false.
Condition
Incrementor
Action (Code Block
While True)
Any problems remaining?
move to next problem
work on problem
Are we there yet? (biking)
move to next mile
peddle peddle
Semester over?
move to next day
work work
Any pizza left?
next slice
eat slice
While True
The condition is either true
else
it is false.
Purpose
Initialization
Condition
Incrementor
Run
Code Block
While True
Print Numbers
initialize n = 1
n <= 100
increment n
print n
Sum Numbers
"initialize n = 1
initialize sum = 0"
n <= 10
increment n
sum = sum + n
Coding Fundamentals (Chapter 105 - Loop Statements)
CHAP104 <
105 - 105 - Loop Statements
> CHAP106
Chapter
Top
Search
TOC