Professional Documents
Culture Documents
PROGRAM CONTROL
STRUCTURE (Part 3)
ITERATIONAL
Used to repeat a
block of codeDFC2073 Programming Fundamentals
Astri
Iteration structures
Iteration structures
while statement
do-while statement
for statement
Repetition Description
Type
while loop Repeats a statement or group of statements while a
given condition is true.
It tests the condition before executing the loop body
do...while Like a while statement, except that it tests the
loop condition at the end of the loop body
for loop Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable
2 Loop condition
3 Loop body
1 2 3
Initialization Evaluation Update
ITERATION STRUCTURES
Syntax
Output ?
ITERATION STRUCTURES
Syntax
Output ?
ITERATION STRUCTURES
Syntax
2 Expression / Condition
Increment/decrement
3
•typically increments or decrements the loop index.
•this performed every time through the loop iteration.
•This part is used to increment any variable(s) that may need to be
incremented/decremented
DFC2073 Programming Fundamentals
Astri
‘for’ loops component & structure
initialization
Termination-
condition
Incrementation
‘for’ Statement : Example
Output ?
In For loops
More readable ; Coding just in one line;
More efficient; Easy to understand
DFC2073 Programming Fundamentals
Astri
Iteration Example
Let us view more example of iteration loop
application below
int main ()
{
unsigned long n;
do
{
cout << "Enter number (0 to end): ";
cin >> n;
cout << "You entered: " << n << "\n";
}
while (n != 0);
cout<<"THE END"<<endl;
return 0;
}
DFC2073 Programming Fundamentals
Astri
The WHILE loop
The process:
1. User assigns a value to n
2. The input receive is call upon and display
3. The while condition is checked (n!=0). At this point there
are two posibilities:
* condition is TRUE: statement is repeated (to step 1)
* condition is FALSE: ignore statement and continue the
program right after the block: print THE END and end
program.
continue
break statement goto statement
statement
• Terminates the • Causes the loop • control flow
loop or switch to skip the statement that
statement and remainder of its causes the CPU
transfers body and to jump to
execution to immediately another spot in
the statement retest its the code.
immediately condition prior • It is identified
following the to reiterating. through use of
loop or switch. a statement
label.
‘break’ Statement
When the break statement is encountered inside a loop,
the loop is immediately terminated and program control
resumes at the next statement following the loop.
Syntax
break;
Key point :
forcing
termination
Flow Diagram
‘break’ Statement : Example (i)
Output ?
We can leave a
loop even if the
condition for its to
end is not fulfilled
‘break’ Statement : Example (ii)
int main()
{
int sum = 0;
Program to type up to 10
// Allow the user to enter up to 10 numbers
for (int count=0; count < 10; ++count) numbers, and displays the sum
{ of all the numbers entered at
cout << "Enter a number to add, or 0 to exit: ";
int num;
the end
cin >> num;
return 0;
} before 10
numbers have
been entered!!
DFC2073 Programming Fundamentals
Astri
‘continue’ Statement
Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
as if the end of the statement block had been reached, causing it to jump to
the start of the following iteration
Syntax
continue;
Key point :
Forces next iteration
of loop take place &
skip any code in
between
Flow Diagram
‘continue’ Statement : Example (i)
int count(0);
while (count < 10) Be careful:
These loops typically
{ increment the loop
if (count == 5) variables in the body of
continue; // jump to end of loop body the loop, using continue
can cause the loop to
cout << count << " "; become infinite!
++count;
An example with a
do-while loop using
• This prints: continue correctly
0 1 2 3 4 6 7 8 9
‘continue’ Statement : Example (iii)
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
if( a == 15) {
// skip the iteration.
a = a + 1; continue;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
‘continue’ Statement : Example (iv)
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a(10);
// do loop execution
do {
if( a <= 15) {
// skip the iteration.
a = a + 1;
continue;
}
cout << "value of a: " << a << endl;
a = a + 2;
}while( a < 25 );
return 0;
}
‘goto’ statement
You should use this feature with caution since its
execution causes an unconditional jump ignoring any
type of nesting limitations
goto allows to make an absolute jump to another
point in the program.
The destination point is identified by a label, which is then
used as an argument for the goto statement.
A label is made of a valid identifier followed by a colon (:)
The goto statement and its corresponding
statement label must appear in the same function.
DFC2073 Programming Fundamentals
Astri
‘goto’ Statement : Example (i)
#include <iostream> If the input entered is :
#include <cmath> // for sqrt() function
using namespace std;
First Input x -> - 8
int main()
{
Second Input x -> - 5
double x; Last Input x -> 9
tryAgain: // this is a statement label
cout << "Enter a non-negative number:";
cin >> x;
if (x < 0.0)
{ If the input entered is :
cout<<"Invalid input"<<endl;
goto tryAgain; // this is the goto statement First Input x -> - 9
} Last Input x -> 22.5
cout << "The sqrt of "<< x << " is " << sqrt(x);
return 0;
}
‘goto’ Statement : Example (ii)
int main ()
{
int n=10;
next:
cout << n <<",";
n--;
if (n>0)
{
goto next;
}
cout << "FIRE!\n";
What is the output if
return 0; the statement are
change into the
} program code shown
‘goto’ Statement : Example (iii)
int main ()
{
int n=10;
Why don’t
you give it a
try ?
Class Activity
In group of TWO :
1. Write a program
to solve the
given problem
using
appropriate
iteration control
structure.
Activity 1
Activity 1
Iteration structure
Write a program using appropriate iteration
control structure:
1. To countdown from the value 10.
2. To display the number of countdown each
time.
3. Terminate the program as the value
reach 3 with an output statement display
as “countdown aborted!”.
Example output:
Exercise
Reference :
• http://www.tutorialspoint.com
• http://
www.learncpp.com/cpp-tutorial
Next Class
TOPIC 4 :
Array, Structures and
Pointer (Part 1)