MTS 3013 Structured Programming: Chapter 4 - Looping
MTS 3013 Structured Programming: Chapter 4 - Looping
STRUCTURED
PROGRAMMING
Chapter 4 Looping
val++;
--
--val;
val--;
(Program Continues)
Introduction to Loops:
The while Loop
Loop:
expression is evaluated
expression
int number = 6;
while (number <= 5)
{
cout << "Hello\n";
number++;
}
An Infinite Loop
int number = 1;
while (number <= 5)
{
cout << "Hello\n";
}
The
Flowchart
Counters
Counter:
do
statement; // or block in { }
while (expression);
Note
do-while Example
int x = 1;
do
{
cout << x << endl;
} while(x < 0);
General Format:
for(initialization; test; update)
statement; // or block in { }
statement; // or block in { }
1)
2)
Perform initialization
Evaluate test expression
3)
A Closer Look
at the Previous
Example
Test Expression
int x, y;
for (x=1,
{
cout <<
<<
<<
}
(Program Continues)
Sentinels
sentinel:
Special
Used
(Program Continues)
Can
Nested Loops
A
Inner
Total
loop
Use
When
Use
The end