190 likes | 211 Views
FTSM. Control Structure: Selection (Part 1). Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure. Single Selection. Multiple Selection. Double Selection. Selection Structure. Single Selection.
E N D
FTSM Control Structure: Selection(Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure Computer Science Department
Single Selection Multiple Selection Double Selection Selection Structure TK1913-C Programming2
Single Selection • Pseudocode Structure • step a • if <condition is true> start • step m • step n • end_if • step x TK1913-C Programming3
Step a true condition Step m Step n false Step x Single Selection Step a true condition Step m Step n false Step x TK1913-C Programming4
Exercise Develop an algorithm for the following problem: A system to check/verify the eligibility of a person to vote during the election is going to be developed. The input for the system is the person’s age. If the age is greater than 20 years old, then display “You are eligible to vote” and “Your age is <age> years old” messages. If the age is less than 20 years old, display “Your age is <age> years old” message only. TK1913-C Programming5
start age “You are eligible to vote” true age > 20 FALSE ! false false “Your age is <age> years old” end Exercise - Answer start Assume the age is 12 years old age true “You are eligible to vote” 12 > 20 ? age > 20 “Your age is <age> years old” Your age is 12 years old end TK1913-C Programming6
Something to ponder … What if ….. the age is 37 years old? What about 20 years old? TK1913-C Programming7
Single Selection • Pseudocode Structure • step a • if <condition is true> start • step m • step n • end_if • step x Let’s recap … Double selection has similar structure except that … TK1913-C Programming8
… additional steps at the bottom … Double Selection • Pseudocode Structure • step a • if <condition is true> start • step m • step n • end_if • else start • step x • step y • end_else • step z TK1913-C Programming9
Step a condition true Step m false Step n Step x Step y Step z Double Selection Step a condition true Step m false Step n Step x Step y Step z TK1913-C Programming10
Exercise Develop an algorithm for the following problem: A system to check/verify the eligibility of a person to vote during the election is going to be developed. The input for the system is the person’s age. If the age is greater than 20 years old, then display “You are eligible to vote” and “Your age is <age> years old” messages. If the age is less than 20 years old, display “You are not eligible to vote” and “Your age is <age> years old” messages. TK1913-C Programming11
start age “You are eligible to vote” “You are not eligible to vote” age > 20 “Your age is <age> years old” end Exercise - Answer Verify this flowchart ! true false TK1913-C Programming12
Double Selection Single Selection • Pseudocode Structure • step a • if <condition is true> start • step m • step n • end_if • else start • step x • step y • end_else • step z • Pseudocode Structure • step a • if <condition is true> start • step m • step n • end_if • step x Let’s recap … Guess…how does multiple selection look like ? TK1913-C Programming13
Multiple Selection • Pseudocode Structure (Multi-way if) • step a • if <condition_1 is true> start • step m • end_if • if <condition_2 is true> start • step n • end_if • step z TK1913-C Programming14
Step a Condition1 true Step m false Condition2 true Step n false Step z Multiple Selection Step a Condition1 true Step m false Condition2 true Step n false Step z TK1913-C Programming15
Multiple Selection • Pseudocode Structure (Cascaded if) • step a • if <condition_1 is true> start • step m • end_if • if <condition_2 is true> start • step n • end_if • else start • step x • end_else • step z TK1913-C Programming16
Step a Condition1 true Step m false true Condition2 Step n false Step x Step z Multiple Selection Step a Condition1 true Step m false true Condition2 Step n false Step x Step z TK1913-C Programming17
Exercise Develop a flowchart for the following problem. Given a mark, determine its grade based on the table below: 74 < mark < 100 grade = A 64 < mark < 75 grade = B 54 < mark < 65 grade = C 39 < mark < 55 grade = D 0 < mark < 40 grade = E others error message TK1913-C Programming18
Yes !! That’s all? What’s next??? PART 2 on the way… relax ! End of Lecture 7 (Part 1) TK1913-C Programming19