Lecture 8 Control Structures, Pseudocode and Flowcharts - 1
Lecture 8 Control Structures, Pseudocode and Flowcharts - 1
A lot of Our programs so far consist of just a list of commands to be done in order
◦ The program cannot perform the same command more than once
2. Selection structures
• eg. if, if/else, switch
3. Repetition structures
• eg. while, do/while, for
Selection Control Structures
Some types of selection statements include:
1. – if selection statement
Print “Passed”
Stop
Exercise on If Selection
Write a program using pseudocode and flow chart that
would accept the age of a person.
true false
condition?
statement-1 statement-2
The If … else selection structure
The if…else selection structure allows
the programmer to specify that different
action is to be performed when true
Studentgrade>=60
false
condition is true and when the condition
is false
Print
Print failed
Passed
◦– while
◦– do…while
◦– For
While loop
A WHILE loop is a loop that repeats while some condition is satisfied.
•The WHILE loop tests its condition at the beginning of every loop.
•If the condition is false from the start, the sequence of activities
contained in the loop never runs at all.
While Control Structure (Loop)
A while repetition structure allows the programmer to specify that an action is to
be repeated while some condition remains true.
The while loop
This is the form of the while loop:
If the condition starts out false, the statement is never executed at all
Flowchart for the while loop
true
condition? statement
false
Exercise: Write a program using a while loop
that prints “I love computers” five (5) times
Pseudocode
1. Start
2. count = 0 Exercise: Write the complete flowchart for this
algorithm
3. While count < 5
Display "I love computers!"
4. Increment count
5. Endwhile
6. Stop
Exercise
•Unlike a while loop, a do-while loop tests its condition at the end of
the loop.
•This means that its sequence of activities always runs at least once.
The Do While Control Structure
A do...while loop is similar to a while loop, except the fact that
it is guaranteed to execute at least one time.
If the condition is false, the control breaks out of the loop.
This means that the statements inside the loop are executed before
the condition is tested.
So the do while loop should be used in all scenarios where the
loop body needs to be executed at least once
Do ….While Example
An algorithm in pseudocode to print Dry run of the program
Hello World two times.
using do .. while 1. Program starts.
2. . i is initialised to 2.
3. Execution enters the do loop
1. Start a) "Hello World" gets printed 1st time.
2. i = 2 b) Update is done. (i is increased by 1)
Now i = 2.
3. Do
4. Condition is checked.
1. Print “Hello World” 2 < 2 yields false. 5.
2. Increase i by 1
4. While i<1 5. The flow goes outside the loop.
5. EndWhile
6. End The output of this program will be: Hello World
The Do While Control Structure:
Example
•The loop uses a counter to tell it how many times to run the same
sequence of activities
For Loop
The counter has the following three numeric values:
•The loop ends when the counter reaches the final counter value, or, if there is an
associated test condition, when the test condition is true.
For Loop : Flow chart
For Loop: Pseudocode Example 1
Write a program in pseudocode and flowchart to Find Sum of Natural Numbers (1 to 100).
1. BEGIN
2. counter, sum=0
3. FOR counter=1 TO 100 STEP 1
4. DO
5. sum=sum+counter Draw the flow chart
6. ENDFOR
7. OUTPUT sum
8. END
For Loop: Pseudocode Example 2
Write a program in pseudocode and flowchart to Read 50 numbers and find their sum and
average. )
(Pseudocode For Loop Example
1. BEGIN
2. NUMBER counter, sum=0, num
3. FOR counter=1 TO 50 STEP 1 DO
4. OUTPUT "Enter a Number"
5. INPUT num
6. sum=sum+num
Draw the flow chart
7. ENDFOR
8.
9. OUTPUT sum
10.END
For Loop: Pseudocode Example 3
Write a program in pseudocode and flowchart to Print Numbers from 1 to n.
2. Write a program in pseudocode and flow chart to Calculate the Sum and Average of n Number
3. Design the algorithm and flowchart that finds and display the larger of the two numbers given different from
each other.
4. The voltage (V) between the poles of a conductor is equal to the product of the current (I) passing through
the conductor and the resistance (R) present on the conductor. It’s demonstrated by the V = I * R formula.
Write an algorithm using pseudocode and flowcharts to calculate the voltage between the poles of the
conductor by using the formula according to the current and resistance values entered by the user.