0% found this document useful (0 votes)
6 views

02_Problem Solving Program Design Tools 2_REDUCED (1)

Done if my organizational behavior lecturer notes

Uploaded by

kasun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

02_Problem Solving Program Design Tools 2_REDUCED (1)

Done if my organizational behavior lecturer notes

Uploaded by

kasun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

BBM2304-Programming Fundamentals with

Python

Problem Solving Program Design Tools -2

1
The Structured Programming Theory
Lecture objective
▪ Selection
▪ Multiple IF condition
▪ Case option
▪ Exercises
▪ Repetition(Iteration)
▪ Repeat Until
▪ While
▪ For
▪ Exercises
2
Multiple IF Condition
Nested If-Else selection structure test for multiple cases by placing
If-Else selection structures inside If-Else structures.
IF condition Ex:
1. Multiple IF condition (Nested if)
IF marks>80 THEN
Format: Grade=‘A’
IF<condition1> THEN ELSE IF marks>70 THEN
Statement-1
Grade=‘B’
Statement-2
ELSE IF marks>60 THEN
ELSE IF<condition2> THEN
Statement-3 Grade=‘C’
Statement-4 ELSE
ELSE Grade=‘R’
Statement-5 ENDIF
Statement-6 ENDIF
ENDIF
END IF
ENDIF
3
Exercise
5. Input student number and marks through the key board. If
marks are more than 80 then the grade is D, if marks are
more than 70 then the grade is C, if marks are more than
60 then the grade is S else the grade is F. Finally display
the student number , grade on the screen.

4
Solution - 5
BEGIN
Use variables: stud_no, grade OF TYPE String
marks OF TYPE Integer
ACCEPT stud_no, marks
IF marks>80 THEN
grade:= “D”
ELSE IF marks>70 THEN
grade = “C”
ELSE IF marks>60 THEN
grade = “S”
ELSE
grade = “F”
ENDIF
ENDIF
ENDIF
DISPLAY stud_no, grade
END PROGRAM
5
Nested IF condition scenario
Some computations have multiple levels of decision making. You first make
one decision, and each of the outcomes leads to another decision. Here is a
typical example.

In the United States, taxpayers pay income tax at different rates depending on
their incomes and marital status. There are two main tax schedules: one for
single taxpayers and one for married taxpayers “filing jointly”, meaning that the
married taxpayers add their incomes together and pay taxes on the total.

CDP 2014/2015 6
Nested IF condition scenario
Flow chart representation for the income tax computation

Exercise:
➢ Identify the possible variables and write Psedocode for the above problem. 7
CASE OPTIONS
Case option is an alternative to If-else-if for selectively executing a
single block of statements among multiple block of statements
Case Options

Format:
DO CASE of<variable name>
CASE 1<codition1>
Statement-1
Statement-2
CASE 2<condition2>
Statement-3
Statement-4
CASE 3<codition3>
Statement-5
Statement-6
OTHERWISE Statement-7
ENDCASE 8
Exercises

7. Write a psedocode to present choices which allow the user to


choose between 3 houses which are available for rent. By
entering a choice details of house, should display message.

House Categories are,


1. House 1-Cost £200 per month.
One bedroom with car parking area.
2. House 2-Cost £400 per month
Two bedrooms and a garage.
3. House 3-Cost £800 per month
Three bedrooms with air condition and one double garage.

9
Solution - 7
BEGIN
Use variables: opt OF TYPE Integer
DISPLAY “Menu Option”
DISPLAY ”Option 1-Cost £200”
DISPLAY ”Option 2-Cost £400”
DISPLAY ”Option 3-Cost £800”
DISPLAY ”Please enter Your Option:”
ACCEPT opt
DO CASE of opt
CASE1 opt=1
DISPLAY “Cost £200 for month, one bedroom and car parking area”
CASE2 opt=2
DISPLAY “Cost £400 for month, two bedrooms and garage”
CASE3 opt=3
DISPLAY “Cost £800 for month, three bedrooms air condition and double
garage”
OTHERWISE
DISPLAY “Invalid Input”
END CASE
END PROGRAM 10
CASE-OPTION Exercise
Menu

1.) Pizza
2.) Burger
3.) Pasta and Rice

Choose option and press OK: ………


ok

Display on the screen, when


Option 1 is selected – “Piza - Chicken/ Vegetable”
Option 2 is selected – “Burger -Chicken/ Vegetable”
Option 3 is selected – “Pasta and Rice Fish/ Chicken/ Vegetable”
Any other option should return – “Invalid input”

11
Solution
BEGIN
Use variables: opt OF TYPE Integer
DISPLAY “Menu Option”
DISPLAY ”Option 1-Pizza”
DISPLAY ”Option 2-Burger”
DISPLAY ”Option 3-Pasta and Rice”
DISPLAY ”Please enter Your Option:”
ACCEPT opt
DO CASE of opt
CASE1 opt=1
DISPLAY “Piza - Chicken/ Vegetable”
CASE2 opt=2
DISPLAY “Burger -Chicken/ Vegetable”
CASE3 opt=3
DISPLAY “Fish/ Chicken/ Vegetable”
OTHERWISE
DISPLAY “Invalid Input”
END CASE
END PROGRAM
12
REPETITION

There are mainly three repetition groups

1. Repeat Until loop

2. While loop

3. For loop

13
Repetition Repeat Until
1. Repeat Until loop
Repeating an action or block of actions until a true
condition occurs.

Format: Ex:
x=1
REPEAT REPEAT
Statement-1
DISPLAY x
Statement-2
UNTIL <Condition> x=x+1
UNTIL x>5

Output – 1, 2, 3, 4, 5
14
Exercises

8. Write a psedocode to display the numbers from 10 to 20


on the screen.

9. Display the word ‘My Computer’ for 10 times on the


screen.

10. Write a psedocode to display the following output


2 4 6 8 10

15
Solution

8. Write a psedocode to display the numbers from 10 to 20 on


the screen.

BEGIN
Use variables: x OF TYPE Integer
x:=10
REPEAT
DISPLAY x
x=x+1
UNTIL x>20
END PROGRAM

16
Solution
9. Display the word ‘My Computer’ for 10 times on the
screen.

BEGIN
Use variables: x OF TYPE Integer
label OF TYPE String
x=1
label= “My Computer”
REPEAT
DISPLAY label
x=x+1
UNTIL x>10
END PROGRAM This can be done without declaring a variable call lable

17
Solution

10. Write a psedocode to display the following output


2 4 6 8 10

BEGIN
Use variables: x OF TYPE Integer
x=2
REPEAT
DISPLAY x
x=x+2
UNTIL x>10
END PROGRAM

18
Repetition WHILE loop
2. While loop
Repeat a set of statements as long as the given condition is
true. The logic test checking is done at the beginning point
of the loop. Therefore no action will be performed if the
condition is false.

Format: Ex:
WHILE <Condition> x=1
Statement-1
WHILE x<=5
Statement-2
DISPLAY “Hello World”
END WHILE
x=x+1
END WHILE

19
Exercises
11. Write a psedocode to display your name for 5 times on
the screen.

12. Write a psedocode to display 10 to 1 on the screen.

20
Solution 11
Write a psedocode to display the word ‘My Computer’ for 5
times on the screen.

BEGIN
Use variables: x OF TYPE Integer
label OF TYPE string
x=1
label = “Dave ”
WHILE x<=5
DISPLAY label
x=x+1
END WHILE
END PROGRAM

21
Solution 12

Write a psedocode to display 10 to 1 on the screen.

BEGIN
Use variables: x OF TYPE Integer
x=10
WHILE x>=1
DISPLAY x
x=x-1
END WHILE
END PROGRAM

22
Repetition FOR loop
1. For loop
The for loop is controlled by a variable which is a simple
count which is assigned a starting value, It will compare the
current value of the variable with the final state. If the
current variable has passed the final state, the loop will be
terminated.
Format:

FOR (Starting value, final value, Increment value)


Statement-1
Statement-2
ENDFOR

23
Exercise
13. Write a psedocode to find the sum and average of
a series of numbers. The user can decide the
numbers to be entered. Use a FOR Loop. Look at
the Ex:

Enter number of Elements/Numbers 5


Enter Number of values 1.) 10
2.) 5
3.) 8
4.) 15
5.) 6
Sum: ………..
Average: …………

24
Solution - 13
13. Write a psedocode to find the sum and average of a series
of numbers. The user can decide the numbers to be
entered. Use a FOR Loop

BEGIN
Use variables: num, sum, count, x, avg OF TYPE real

ACCEPT count
sum=0
FOR (x=1,x<=count, x=x+1)
ACCEPT num
sum=sum+num
ENDFOR
avg=sum/count
DISPLAY sum, avg
END PROGRAM
25
Thank you

26

You might also like