You are on page 1of 20

Iterative Statements

Pseudocode and Flowcharting

LOOPING / ITERATIVE
Loops and Counter
§ Looping
§ Used when it is desired to make the same calculation of more than
one set of data. It consists of repeating a program, or a section of
program and substituting new data for each repetition.

§ Counter
§ Is a setup in a program loop to keep track of a number of times the
program segment is repeated. The program can then be terminated
after the completion of a predetermined number of passes.
Loops and Counter
§ Steps in Loop Control
§ Initialization
§ The value of counter is initially set equal to zero or one.
§ Test for limit condition
§ Before the logic flow gets out of a loop, a loop terminating condition must first
be satisfied.
§ Incrementation
§ Often each loop is executed, 1 is added to the counter. Thus counter reflects
the number of times the operation has been performed.
Looping / Iterative – for loop
§ Pseudo code
§ Method 1 Method 2
FOR i = r to s by 1 do FOR (i = r ; i<=s ; i++)
statements
statements
ENDFOR ENDFOR

Example
§ Example
FOR i =1 to 5 by 1 then FOR( i =1 ; i<=s; i++)
PRINT: i
PRINT: i
ENDFOR
ENDFOR

This will print 1 2 3 4 5


This will print 1 2 3 4 5
Looping / Iterative – for loop
START
§ Pseudo code § Flowchart

§ Method 3 (natural language)


INITIALIZE: i = r
1. START
2. Initialize i = r
3. Is i <= s
1. if yes then no
i <= s?
1. Execute Statements
2. Calculate i +1 and assign to i yes

3. Repeat step 3
Execute Statements
2. else
CALCULATE: i = i +1
1. END
END
Example for Looping
§ The initial value of the radius of a circle is equal to one unit
and each succeeding radius is one unit greater than the
values before it. Make a pseudo-code and draw a flowchart
to compute the area of the circle starting with R=1.0 up to
R=5.0, then print out each radius and corresponding area of
the circle.
Programming

LOOPING / ITERATIVE
Pretest Loops
§ In each iteration, the control
expression is tested first.
§ If it is true, the loop action(s) are
executed;

§ If it is false, the loop is


terminated
Protest Loops
§ In each iteration, the loop
actions are executed then the
control expression is tested.
§ If its is true, a new iteration is
started.

§ If false, the loop terminates

§ The action is executed at least


once
Initialization and Updating
Event-Controlled Loops
§ An event changes the loop control expression from true to
false.

§ Examples:
§ The loop will exit if the user entered a negative number.
§ The loop ends when the sum is already equal to 100.
Counter-Controlled Loops
§ If the number of repetition is known, the counter-controlled
loop is used.

§ Examples:
§ Display your name n times.
§ Display the number from 1 to n.
Loops in C

Loop Statements

for while do-while


The for Loop

§ Syntax:

for(initialization; terminating_expression; update)


{
body of the loop;
}

§ Note: braces can be omitted if there’s only


one statement
The while Loop
§ Syntax

initialization
while(terminating_expression)
{
body of the loop;
update;
}

§ Note: braces can be omitted if there’s only


one statement
The do while Loop
§ Syntax

initialization
do {
body of the loop;
update;
}while(terminating_expression);

§ Note: braces can be omitted if there’s only one statement


The for Loop
Example: Display integers from 1 to n.
#include<stdio.h>
int main(void)
{
int n, x;
printf("enter an integer: ");
scanf("%d ",&n);
printf("\n\nthe numbers are: ");
for(x=1;x<=n;x++)
{
printf("%d ",x);
}
}
The for, while, do-while Loop
§ Make a program that will determine how many odd numbers
are there from numbers 1 to n.

§ Make a program that will display the even numbers from 1 to


n.

§ Make a program that will compute the sum of n numbers


inputted by the user.
§ Create a function that would display a
menu to compute whether an area, diameter or
Circumference, and should return the value of the choice
§ What is the best type of loop (for, while, do-while) should we use?

§ What is the condition statement we are going to use?

You might also like