Top Banner
2020 SEKOLAH PENDIDIKAN, FAKULTI SAINS SOSIAL DAN KEMUANUSIAAN, UTM The Concept of Control Structure In Programming
27

The Concept of Control Structure In Programming

Feb 14, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Concept of Control Structure In Programming

2020 SEKOLAH PENDIDIKAN,

FAKULTI SAINS SOSIAL DAN KEMUANUSIAAN, UTM

The Concept of Control Structure

In Programming

Page 2: The Concept of Control Structure In Programming

2

Learning Objectives

At the end of this lesson, students should know :

Structured Programming ;

Linear Structure.

Selection Structure :

Single selection

Double selection

Multi selection

Nested selection (pilihan bersarang)

Looping Structure

Page 3: The Concept of Control Structure In Programming

3

Structured Programming

+ Why Structured Programming?

= Make a programming easy to understand

Also known as Logic Structure in programming

Structured programming is based on 3 basic controlling :

• Linear Structure.

• Selection Structure.

• Looping Structure.

This structure is top-down design

Advantages - simple & easy to understand

Page 4: The Concept of Control Structure In Programming

4

Structured Programming

Linear Structure.

Give the situation

That based on this

flowchart

Page 5: The Concept of Control Structure In Programming

5

Structured Programming

Linear Structured

“Linear” refer to routine programming that linear (step by step programming) (A – B – C – E --- Z)

Routine = procedure : a set of instruction in logical unit

Routine example : 1. int x, y; // type of X & Y value)

2. cin>>x; // input X value)

3. cin>>y; // input Y value)

4. cout<<x+y; // display total X + Y)

Page 6: The Concept of Control Structure In Programming

6

Structured Programming

Linear Structured

Basically, linear structured used to solve

simple problem.

Eg : X + Y = 2 + 4 = 6

Page 7: The Concept of Control Structure In Programming

7

Linear Structured

Pseudo Code

1. Input X

2. Input Y

3. Calculate X + Y = Z

4. Output / Print Z

5. End

Structured Programming

Page 8: The Concept of Control Structure In Programming

8

Structured Programming

Linear Structured

Other example :

Student record application

First routine : Input : name, course, matrix

Second routine : display name, course, matrix, (sort by name)

Just input, process and display

Page 9: The Concept of Control Structure In Programming

9

Structured Programming

Selection Structure.

Give the situation

That based on this

flowchart

Page 10: The Concept of Control Structure In Programming

10

Structured Programming

Selection Structured

Next procedure or next decision depend on previous condition and input

Next routine or procedure depend previous condition ; true (1) or false (0)

Page 11: The Concept of Control Structure In Programming

11

Structured Programming

Selection Structured

Situation : All students can take SPM2102 for next semester except who gets < 2.00 in CGPA

Page 12: The Concept of Control Structure In Programming

12

Structured Programming

Selection Structured

There 4 design of selection structured :

1. Single selection

2. Double selection

3. Multi selection

4. Nested selection (pilihan bersarang)

Page 13: The Concept of Control Structure In Programming

13

Structured Programming

Single Selection Structured

If mark <39 then Print fail, else go to end

Sketch up for the flowchart

Page 14: The Concept of Control Structure In Programming

14

Structured Programming

Single selection

1. Start

2. Read the value

3. If value ≠ 0

4. Then display the value

5. End

Page 15: The Concept of Control Structure In Programming

15

Structured Programming

Read Working

Hours

Working Hours >8 Day Salary

basic + extra

hours

True

False

Display Day Salary

Single selection 1. Start 2. Read the staf working

hours 3. If working hours > 8 4. Then Day Salary =

salary + extra hours 5. Display the DaySalary 6. End

Page 16: The Concept of Control Structure In Programming

16

Structured Programming

Dual Selection Structured

If mark <39 then Print fail, then go to end

Else if >39 then Print good,

End

Sketch up for the flowchart

Page 17: The Concept of Control Structure In Programming

17

Structured Programming

Double / Dual selection

1. Start

2. Read the value

3. If value ≠ 0, then display the value, go to End

4. Else = 0, then display 0

5. End

Page 18: The Concept of Control Structure In Programming

18

Structured Programming

Multi Selection

More than two condition or selection

One input must be testing on more than two condition

If first condition are true, then will be testing on second Condition. If else, other action / routine will be taken

Page 19: The Concept of Control Structure In Programming

19

Structured Programming

Multi Selection

Page 20: The Concept of Control Structure In Programming

20

Class activity

Multi - Selection

Draw a flow chart to make a choice of 1 of 3 chocolates

namely Daim, Cadbury and Godiva. The final decision is

whether or not to buy.

Page 21: The Concept of Control Structure In Programming

21

Structured Programming

Nested Selection

Page 22: The Concept of Control Structure In Programming

22

Structured Programming

Looping Structure.

Give the situation

That based on this

flowchart

Page 23: The Concept of Control Structure In Programming

23

Structured Programming

Looping Structured

If get mark <50, then repeat for the same subject, else

register for next subject

Sketch up for the flowchart (looping structured)

Page 24: The Concept of Control Structure In Programming

24

Structured Programming

Looping Structured

Page 25: The Concept of Control Structure In Programming

25

Structured Programming

Looping Structured + Dual Selection

Page 26: The Concept of Control Structure In Programming

26

Class activity

Dual Selection + Looping

Ben has over 10 options for appointment as secretary. He

wants to make decisions based on the following conditions:

Beautiful and single

Review the 10 staff options to determine status (shortlisted)

Page 27: The Concept of Control Structure In Programming

27

Question and Answer