Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Control Structures

Similar presentations


Presentation on theme: "Chapter 4: Control Structures"— Presentation transcript:

1 Chapter 4: Control Structures
Selection Statement Selection Structure

2 Control Structures

3 Selection Structures One way selection (single if)
Two way selection (if…else) Multiple Selection (Nested if…else)

4 One-Way Selection

5 If G more than or equal to 60 End
Write a program that read the grade of the student and if it is more than or equal 60; print “Pass” Start Start Read the grade G If G more than or equal to 60 Print “Pass” End Read G If G >= 60 False True Print “pass” End

6 Write a program that divide one integer by another only if the divisor not equal to 0.
Start Read first number (dividend ) Read second number (divisor) If divisor not equal 0 Result = dividend / divisor Print Result End Start Read D1 Read D2 If D2 != 0 True False Result= D1 / D2 Print Result End

7 Two-Way Selection

8 Write an algorithm to read two numbers and print the largest one
Start Read first number (num1) Read second number (num2) If num1 is larger than num2 Print num1 else Print num2 Start Read num1 Read num2 If num1 > num2 False True Print num1 Print num2 End

9 Write a program that read a temperature and if it is more than 35 print “Summer”, otherwise print “winter” Start Read D If D>35 Print “Summer” Else Print “winter” End

10 Start Read g1,g2,g3 Average = (g1+g2+g3)/3 If Average >= 60 Else
Write a program that read 3 grades for a student and find the average. If the average more than or equal 60; print “pass”, otherwise print “fail” Start Read g1,g2,g3 Start Read g1,g2,g3 Average = (g1+g2+g3)/3 If Average >= 60 Print “pass” Else Print “fail” End Avg = (g1+g2+g3)/3 If avg >= 60 False True Print “Pass” Print “Fail” End

11 Multiple Selection: Nested if

12 Start Read a number (N) If N greater than 0 Else if N less than 0 Else
Write a program that read a number then print a message saying whether the number is positive, negative or zero. Start Start Read a number (N) If N greater than 0 Print “Positive” Else if N less than 0 Print “Negative” Else Print “Zero” Read N If N > 0 T Print “Positive” F If N < 0 T F Print “Negative” Print “Zero” End

13 Bad Solution!! Start Read N T If N > 0 F Print “Positive”
Why check again ?!! If N < 0 T F Print “Negative” Print “Zero”

14 If students grade is greater than or equal to 90 Display ‘A’
write a pseudocode / algorithm to work out what grade a student got based on the following information :- if student (above or equal) to 90 -> A , 80 –> B, 70 –> C, 60 –> D, less then or equal 60 –> F Start Read grade If students grade is greater than or equal to 90 Display ‘A’ Else If students grade is greater than or equal to 80 Display ‘B’ Else If students grade is greater than or equal to 70 Display ‘C’ Else If students grade is greater than or equal to 60 Display ‘D’ Else Display ‘F’ End

15 Start Read G If G >= 90 T F Print “A” If G >= 80 T F If G >= 70 F T Print “B” If G >= 60 T F Print “C” Print “D” Print “F” End


Download ppt "Chapter 4: Control Structures"

Similar presentations


Ads by Google