Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various...

19
Computer Science Departme nt FTSM FTSM Control Structure: Control Structure: Selection Selection (Part 1) (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure

Transcript of Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various...

Page 1: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

Computer Science Department FTSMFTSM

Control Structure: Control Structure: SelectionSelection(Part 1)(Part 1)

Knowledge:Understand various concepts of selection control structure

Skill:Be able to develop a program containing selection control structure

Page 2: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 22

Selection StructureSelection Structure

Single Single SelectionSelection

Double SelectionDouble Selection

Multiple Multiple SelectionSelection

Page 3: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 33

Single SelectionSingle Selection

Pseudocode StructurePseudocode Structure

step a

if <condition is true> startstep mstep n

end_ifstep x

Pseudocode StructurePseudocode Structure

step a

if <condition is true> startstep mstep n

end_ifstep x

Page 4: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 44

Single SelectionSingle Selection

Step aStep a

conditioncondition Step mStep m

Step nStep n

Step xStep x

truetrue

falsefalse

Step aStep a

conditioncondition Step mStep m

Step nStep n

Step xStep x

truetrue

falsefalse

Page 5: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 55

ExerciseExerciseDevelop 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.

Page 6: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 66

Exercise - Answer Exercise - Answer

truetrue

falsefalse

““You are You are eligible to vote”eligible to vote”

ageage

startstart

endend

““Your age is <age> Your age is <age> years old”years old”

age > 20age > 20truetrue

falsefalse

““You are You are eligible to vote”eligible to vote”

ageage

startstart

endend

““Your age is <age> Your age is <age> years old”years old”

age > 20age > 20

Assume the age is Assume the age is 12 years old12 years old

12 > 20 ? 12 > 20 ? FALSE !FALSE !

Your age is 12 Your age is 12 years oldyears old

Page 7: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 77

Something to ponder …Something to ponder …

the age is 37 years old?What if …..

What about 20 years old?

Page 8: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 88

Let’s recap …Let’s recap …

Pseudocode StructurePseudocode Structure

step a

if <condition is true> startstep mstep n

end_ifstep x

Pseudocode StructurePseudocode Structure

step a

if <condition is true> startstep mstep n

end_ifstep x

Single Single SelectionSelection

Double selection has similar Double selection has similar structure except that …structure except that …

Page 9: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 99

Double SelectionDouble Selection

Pseudocode StructurePseudocode Structure

step aif <condition is true> start

step mstep n

end_ifelse start

step xstep y

end_elsestep z

Pseudocode StructurePseudocode Structure

step aif <condition is true> start

step mstep n

end_ifelse start

step xstep y

end_elsestep z

… … additional additional steps at the steps at the bottom …bottom …

Page 10: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1010

Double SelectionDouble Selection

Step aStep a

conditioncondition Step mStep m

Step nStep n

Step zStep z

truetrue

falsefalse

Step xStep x

Step yStep y

Step aStep a

conditioncondition Step mStep m

Step nStep n

Step zStep z

truetrue

falsefalse

Step xStep x

Step yStep y

Page 11: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1111

ExerciseExerciseDevelop 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.

Page 12: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1212

Exercise - Answer Exercise - Answer

truetrue

falsefalse

““You are You are eligible to vote”eligible to vote”

ageage

startstart

endend

““Your age is <age> Your age is <age> years old”years old”

age > 20age > 20

Verify this Verify this flowchart !flowchart !

““You are not You are not eligible to vote”eligible to vote”

Page 13: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1313

Let’s recap …Let’s recap …

Pseudocode StructurePseudocode Structure

step a

if <condition is true> startstep mstep n

end_ifstep x

Pseudocode StructurePseudocode Structure

step a

if <condition is true> startstep mstep n

end_ifstep x

Single Single SelectionSelection

Pseudocode StructurePseudocode Structure

step aif <condition is true> start

step mstep n

end_ifelse start

step xstep y

end_elsestep z

Pseudocode StructurePseudocode Structure

step aif <condition is true> start

step mstep n

end_ifelse start

step xstep y

end_elsestep z

Double Double SelectionSelection

Guess…how does multiple Guess…how does multiple selection look like ?selection look like ?

Page 14: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1414

Multiple SelectionMultiple Selection

Pseudocode Structure (Multi-way if)Pseudocode Structure (Multi-way if)

step aif <condition_1 is true> start

step mend_ifif <condition_2 is true> start

step nend_if

step z

Pseudocode Structure (Multi-way if)Pseudocode Structure (Multi-way if)

step aif <condition_1 is true> start

step mend_ifif <condition_2 is true> start

step nend_if

step z

Page 15: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1515

Multiple SelectionMultiple Selection

Step aStep a

Condition1Condition1 Step mStep m

Step nStep n

Step zStep z

truetrue

falsefalse

Condition2Condition2truetrue

falsefalse

Step aStep a

Condition1Condition1 Step mStep m

Step nStep n

Step zStep z

truetrue

falsefalse

Condition2Condition2truetrue

falsefalse

Page 16: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1616

Multiple SelectionMultiple Selection

Pseudocode Structure (Cascaded if)Pseudocode Structure (Cascaded if)step aif <condition_1 is true> start

step mend_if

if <condition_2 is true> startstep n

end_ifelse start

step xend_elsestep z

Pseudocode Structure (Cascaded if)Pseudocode Structure (Cascaded if)step aif <condition_1 is true> start

step mend_if

if <condition_2 is true> startstep n

end_ifelse start

step xend_elsestep z

Page 17: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1717

Multiple SelectionMultiple Selection

Step aStep a

Condition1Condition1 Step mStep m

Step nStep n

Step zStep z

truetrue

falsefalse

Condition2Condition2truetrue

falsefalse

Step xStep x

Step aStep a

Condition1Condition1 Step mStep m

Step nStep n

Step zStep z

truetrue

falsefalse

Condition2Condition2truetrue

falsefalse

Step xStep x

Page 18: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1818

ExerciseExerciseDevelop a flowchart for the following problem.

Given a mark, determine its grade based on the table below:

74 < mark < 100 grade = A64 < mark < 75 grade = B54 < mark < 65 grade = C39 < mark < 55 grade = D 0 < mark < 40 grade = E others error message

Page 19: Computer Science Department FTSM Control Structure: Selection (Part 1) Knowledge: Understand various concepts of selection control structure Skill: Be.

TK1913-C ProgrammingTK1913-C Programming 1919

End of Lecture 7 (Part 1)End of Lecture 7 (Part 1)

Yes !! That’s all? Yes !! That’s all? What’s next???What’s next???

PART 2PART 2 on the way… on the way… relax !relax !