Lecture 12
Lecture 12
Lecture 12
Lecture # 12
Qurratulann
Department of Computer Science
GC University, Lahore
24
Selection Statements
Types of
Selection
IF – THEN – ENDIF(Single IF)
Single IF selection statement either performs (selects) an action if a
condition is true or skips the action if the condition is false.
IF – ELSE – ENDIF (Double IF)
The IF-ELSE selection statement performs an action if a condition is true
and performs a different action if the condition is false
IF – ELSE IF – ELSE – ENDIF (Multiple IF)
The IF – ELSE IF – ELSE selection statement performs one of many
different actions, depending on the value of an expression.
Switch (Alternate to Multiple IF)
The SWITCH selection statement performs one of many different actions,
depending on the value of an expression.
Rules for Selection statement
One Option Multiple Conditions
IF (condition) then IF (condition) then
steps… steps…
Endif Else if(condition) then
Two Options steps…
IF(condition) then Else if (condition) then
steps… steps…
Else Else
steps… steps…
Endif Endif
Algorithm with Selection
Problem: Write the appropriate dress for a given temperature.
Write "Enter temperature"
Read temperature
Determine Dress
Algorithm with Selection
Determine Dress
IF (temperature > 90)
Write “Texas weather: wear shorts”
ELSE IF (temperature > 70)
Write “Ideal weather: short sleeves are fine”
ELSE IF (temperature > 50)
Write “A little chilly: wear a light jacket”
ELSE IF (temperature > 32)
Write “Philadelphia weather: wear a heavy coat”
ELSE
Write “Stay inside”
Repetition Statements
Step 3: Print A
A LxW
Output
W, L
STOP
Example
START
Input
M1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
N Y
IS
GRADE<50
Print “PASS”
Print “FAIL”
STOP
Trace Table
Trace Tables & Dry Run
Algorithm
A sequence of steps designed to perform a particular task
Dry run
Working through a section of a program manually
Trace table
A table constructed with a column to identify the instruction executed and
columns for the contents of each variable
Variable
The identifier associated with a particular memory location used to store data
Constant
A data item with a fixed value
Example 1
– Trace table
Algorithm
1. Start – Let user Input for y is 5
2. Set x := 0 Step Algorithm Lines X Y Output
3. input y
4. x := y * 2 1 Start - - -
5. Output x 2 Set x := 0 0 - -
6. End 3 input y 0 5 -
4 x := y * 2 10 5 -
5 Output x 10 5 10
6 End - - -
Example 2
Algorithm : Take five inputs from user, calculate and
display their sum and average (Without using loop)
1. Start
2. input : num1, num2, num3, num4 and num5
3. Set sum := 0 , average := 0 , totalNumbers :=5
4. input in num1, num2, num3, num4 and num5
5. sum := num1 + num2 + num3 + num4 + num5
6. average := sum / totalNumbers
7. Print “Sum is ” sum
8. Print “Average is ” average
9. End
Example 2
Assume input: 25, 17, 34, 9, 75
# Algorithm Lines num1 num2 num3 num4 num5 sum average totalNu Output
mbers
1 Start - - - - - - - - -
9 End - - - - - - - - -
Example 3
Algorithm : Take five inputs from user, calculate and display
their sum and average (Without using loop)
1. Start 10. Input num
1 Start - - - - -
2 num=0 0 - - - -
4 Input num 25 0 0 5 -
6 input num 17 25 0 5 -
8 Input num 34 42 0 5 -
10 input num 9 76 0 5 -
12 Input num 75 85 0 5 -
17 End - - - - -
Example 4
Algorithm : Take five inputs from user using only 1 variable for input,
calculate and display their totalSum, sumOfEven and average (Without using
loop)
1. Start 13. If (num3 mod 2 = 0 )
2. Set num1=0, num2=0, num3=0, num4=0, 14. Then sumOfEven := sumOfEven +num3
num5=0
15. End if
3. Set totalSum := 0 , sumOfEven := 0,
average := 0 , totalNumbers := 5 16. If (num4 mod 2 = 0 )
4. input num1, num2, num3, num4 and num5 17. Then sumOfEven := sumOfEven +num4
1 Start - - - - - - - - -
9 End if 24 19 34 18 75 170 24 34 5
Example 4
Assume input: 24, 19, 34, 18, 75
# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNumb
n ers
10 If (num2 mod 2 = 0 ) 24 19 34 18 75 170 24 34 5
11 Then sumOfEven := sumOfEven 24 19 34 18 75 170 24 34 5
+num2
12 End if 24 19 34 18 75 170 24 34 5
13 If (num3 mod 2 = 0 ) 24 19 34 18 75 170 24 34 5
14 Then sumOfEven := sumOfEven 24 19 34 18 75 170 58 34 5
+num3
15 End if 24 19 34 18 75 170 58 34 5
16 If (num4 mod 2 = 0 ) 24 19 34 18 75 170 58 34 5
17 Then sumOfEven := sumOfEven 24 19 34 18 75 170 76 34 5
+num4
18 End if 24 19 34 18 75 170 76 34 5
19 If (num5 mod 2 = 0 ) 24 19 34 18 75 170 76 34 5
20 Then sumOfEven := sumOfEven 24 19 34 18 75 170 76 34 5
+num5
21 End if 24 19 34 18 75 170 76 34 5
Example 4
Assume input: 24, 19, 34, 18, 75
# Algorithm Lines num1 num2 num3 num4 num5 sum sumOfEve average totalNumb
n ers
25 End - - - - - - - - -
Output
22 Total Sum is 170
23 Total Sum is 170
Average is 34
24 Total Sum is 170
Average is 34
Sum of evens is 76
Example 5
Take five inputs from user, calculate and display their sum and average (With
using loop)
Algorithm:
1. Start
2. Num=0
3. Set sum := 0 , average := 0 , totalNumbers := 5, loopCounter := 1
4. Repeat while (loopCounter <= totalNumbers)
5. Begin
6. input num
7. display “Enter input ” loop counter “:”
8. sum := sum + num
9. loopCounter = loopCounter + 1
10. End while
11. average := sum / totalNumbers
12. Print “Total Sum is ” sum
13. Print “Average is ” average
13. End
Example 5
Assume input: 25, 17, 34, 9, 75
# Algorithm Loop num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Pass / r
Iteration
1 1 - - - - - - -
2 2 - 0 - - - - -
3 3 - - 0 0 5 1 -
4 4 - - 0 0 5 1 True
5 5 1 - 0 0 5 1 True
6 6 1 25 0 0 5 1 True
7 7 1 25 25 0 5 1 True
8 8 1 25 25 0 5 2 True
9 4 2 25 25 0 5 2 True
10 5 2 25 25 0 5 2 True
Example 5
Assume input: 25, 17, 34, 9, 75
# Algorithm Loop num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Pass / r
Iteration
11 6 2 17 25 0 5 2 True
12 7 2 17 42 0 5 2 True
13 8 2 17 42 0 5 3 True
9 Repeat ----- go to step 4
14 4 3 17 42 0 5 3 True
15 5 3 17 42 0 5 3 True
16 6 3 34 42 0 5 3 True
17 7 3 34 76 0 5 3 True
18 8 2 34 76 0 5 4 True
9 Repeat ----- go to step 4
19 4 4 34 76 0 5 4 True
20 5 4 34 76 0 5 4 True
21 6 4 9 76 0 5 4 True
22 7 4 9 85 0 5 4 True
23 8 4 9 85 0 5 5 True
Example 5
Assume input: 25, 17, 34, 9, 75
# Algorithm Loop num sum average totalNumbers loopCounte loopCounter <= totalNumbers
Step Pass / r
Iteration
9 Repeat ----- go to step 4
24 4 - 9 85 0 5 5 True
25 5 5 9 85 0 5 5 True
26 6 5 75 85 0 5 5 True
27 7 5 75 160 0 5 5 True
28 8 5 75 160 0 5 6 True
29 4 - 75 160 0 5 6 False
30 10 - 75 160 32 5 6 -
31 11 - 75 160 32 5 6 -
32 12 - 75 160 32 5 6 -
33 13 - - - - - - -
Example 5
Assume input: 25, 17, 34, 9, 75
6 Enter input 2: 17
6 Enter input 3: 34
6 Enter input 4: 9
6 Enter input 5: 75