LAB(3)
LAB(3)
PROGRAMMING AND
SOLVING PROBLEMS
LAB(3)
Eng.Mona Ahmed Ali Abdullah 2
Looping Flowchart
• A loop represents a segment which may be repeated a
definite or indefinite number of times.
Eng.Mona Ahmed Ali Abdullah 3
Looping Flowchart
Eng.Mona Ahmed Ali Abdullah 4
Looping Flowchart
• The logical operators used in our pseudo-code are
= is equal to
> is greater than
< is less than
>= is greater than or equal
<= is less than or equal
<> is not eaqual to
Eng.Mona Ahmed Ali Abdullah 5
Looping Flowchart
• Example 1:
Flowchart for finding the sum of 1+2+3+4+5.
Solution:
Steps:
Step 1: start.
Step 2: set sum=0.
Step 3: for i= 1 to 5
Sum= sum+i
set i=i+1
End loop
Step 4: print sum.
Step 5: end
Eng.Mona Ahmed Ali Abdullah 6
Looping Flowchart
Eng.Mona Ahmed Ali Abdullah 7
Looping Flowchart
• Example 2:
Flowchart for finding the sum of 1+2+…..+n
Solution:
Steps:
Step 1: start.
Step 2: set sum=0.
Step 3: enter n
Step 4: for i= 1 to n
Sum= sum+i
Set i=i+1
End loop
Step 5: print sum.
Step 6: end
Eng.Mona Ahmed Ali Abdullah 8
Looping Flowchart
Eng.Mona Ahmed Ali Abdullah 9
Looping Flowchart
• Example 3:
Flowchart for finding the sum of 2+4+…..+n
Solution:
Steps:
Step 1: start.
Step 2: set sum=0.
Step 3: enter n
Step 4: for i= 2 to n
Sum= sum+i
Set i= i+2
End loop
Step 5: print sum.
Step 6: end
Eng.Mona Ahmed Ali Abdullah 10
Looping Flowchart
Eng.Mona Ahmed Ali Abdullah 11
Looping Flowchart
• Example 4:
Flowchart for finding the sum and the average of
15 numerical values.
Solution:
Explanation:
We need 4 variables:
X: used each time to enter one numerical value.
Sum: to find the sum of the 15 values.
Eng.Mona Ahmed Ali Abdullah 12
Looping Flowchart
• Average: to find the mean value.
Count: loop counter
Steps:
Step 1: start.
Step 2: set sum=0.
Step 3: for count= 1 to 15
Enter x
sum= sum+x
count=count+1
End loop
Step 5: set average=sum/15
Step 5: print sum, average.
Step 6: end
Eng.Mona Ahmed Ali Abdullah 13
Looping Flowchart
Eng.Mona Ahmed Ali Abdullah 14
Looping Flowchart
• Finite and Infinite Loops (Equality And In Equality
Conditions):
A lot of computer time wasted if a flowchart contains a
loop which will be worked an infinite number of times due
to certain values of variables put in logical test. This
situation is known as the infinite loop, while the programs
that include finite loops execute in normal operation and
terminate each time.
Infinite loop: common error in computer programming.
Because of a logical error a program does not terminates,
so that it is necessary to interrupt the machine in order to
stop it.
Eng.Mona Ahmed Ali Abdullah 15
Looping Flowchart
• Example 6:
Eng.Mona Ahmed Ali Abdullah 16
Looping Flowchart
• Manually Tracing Arithmetic Operations:
In the following examples, X and Y are two variables
given initial values and are updated
within the flow diagram.
Manually tracing an arithmetic means the execution of the
operations given in a flowchart
using a pencil and a paper. The result of manually tracing
a flowchart is given in a table that
contains the values of the values of variables each time
the loop is entered.
Eng.Mona Ahmed Ali Abdullah 17
Looping Flowchart
• Example 7: