Announcements l l l Quiz1 Sat 12111430 H

  • Slides: 16
Download presentation
Announcements l l l Quiz#1: Sat 12/11/1430 H (12: 15 – 12: 45) Mid#1:

Announcements l l l Quiz#1: Sat 12/11/1430 H (12: 15 – 12: 45) Mid#1: Sat 26/11/1430 H (3: 00 – 5: 00) Quiz#2: Sat 2/1/1431 H (12: 15 – 12: 45) Mid#2: Sat 16/1/1431 H (3: 00 – 5: 00) http: //Csc 111. wordpress. com

Control Structures Selection Structure

Control Structures Selection Structure

Control Structures

Control Structures

Selection Structures • • • One way selection (single if) Two way selection (if…else)

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

One-Way Selection

One-Way Selection

Write a program that read the grade of the student and if it is

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

Write a program that divide one integer by another only if the divisor not

Write a program that divide one integer by another only if the divisor not equal to 0. l l l Start Read first number (dividend ) Read second number (divisor) If divisor not equal 0 begin l l Read D 1 Read D 2 True Result = dividend / divisor Print Result endif End Start Result= D 1 / D 2 If D 2 != 0 False Print Result End

Two-Way Selection

Two-Way Selection

Write an algorithm to read two numbers and print the largest one l l

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

Write a program that read a temperature and if it is more than 35

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

Write a program that read 3 grades for a student and find the average.

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 l l Start Read g 1, g 2, g 3 Average = (g 1+g 2+g 3)/3 If Average >= 60 l l Avg = (g 1+g 2+g 3)/3 False Print “pass” If avg >= 60 True Else l l Read g 1, g 2, g 3 Print “fail” Print “Pass” Print “Fail” End

Multiple Selection: Nested if

Multiple Selection: Nested if

Write a program that read a number then print a message saying whether the

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

Bad Solution!! Start Read N T If N>0 F Print “Positive” Why check again

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

write a pseudocode / algorithm to work out what grade a student got based

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 l l l l Start Read grade If students grade is greater than or equal to 90 l Display ‘A’ Else If students grade is greater than or equal to 80 l Display ‘B’ Else If students grade is greater than or equal to 70 l Display ‘C’ Else If students grade is greater than or equal to 60 l Display ‘D’ Else l Display ‘F’ End

Start Read G T If G >= 90 Print “A” T Print “B” F

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