• Tidak ada hasil yang ditemukan

PROGRAM CONTROL STRUCTURES.ppt

N/A
N/A
Protected

Academic year: 2019

Membagikan "PROGRAM CONTROL STRUCTURES.ppt"

Copied!
58
0
0

Teks penuh

(1)

PROGRAM CONTROL

STRUCTURES

Sequence

Selection

(2)

OBJECTIVE

At the end of this topic, the students should be able to:

3.1 Understand Program Control Structures

Explain the control structures in problem solving:

• Sequence • Selection

• Repetition (looping)

Use flowcharts (graphic algorithm) to show the flow of control

using these structures.

(3)

CONTENTS

Blocks of Code

Program Flow (Control Structure)

Sequential Control Structure

Selection Control Structure

If……Endif

If……Else

Nested If

Loop Control Structure

For

While

(4)

BLOCK OF CODE

is a group of statements that performs one particular task

void welcome ( ) {

printf (“****************************”); printf (“********WELCOME **********”); printf (“****************************”); reference and execution a program

(5)

PROGRAM FLOW

Program flow in computer is controlled by the control structure.

Control structure is a logical structure that controls the flow of

instruction to be executed by computer.

There are 3 types of control structures:

1. Sequential control structure

2. Selection / decision control structure

• If……endif

• If……else

• Nested if

3. Looping control structure

• For

• While

(6)

1. Sequential control structure

• In this control, every step will be executed

one by one from top to bottom.

(7)

• Format:

Flowchart :

START

Statement A

END Statement B

Pseudocode:

Start

(8)

Example

Problem:

Mathematical operation:

(9)

Algorithm:

1. Enter 2 numbers

2. Add 2 numbers

Sum = number_1 + number_2

3. Minus 2 numbers

Subtract = number_1 – number_2

4. Multiply 2 numbers

Multiple = number_1 * number_2

5. Division of 2 numbers

Divide = number_1 / number_2

6. Display Sum, Subtract, Multiple and

Divide

Problem analysis: Input:

number_1, number_2

Process:

Add 2 numbers:

Sum = number_1 + number_2 Minus 2 numbers:

Subtract = number_1 – number_2 Multiply 2 numbers:

Multiple = number_1 * number_2 Division 2 numbers:

Divide = number_1 / number_2

Output:

(10)

Flowchart:

START

Input number_1, number_2

Sum = number_1 + number_2

Output Sum, Subtract, Multiple, Divide

END

Subtract = number_1 – number_2

Multiple = number_1 * number_2

Divide = number_1 / number_2

(11)

2. Selection / Decision Control

Structure

• This type of control structure is usually used in structured

programming

• This control structure will execute an instruction

based on

result of a condition or comparison

.

• A condition will result either

TRUE or FALSE

.

• If the condition result is true, the control program will

execute the instruction within the TRUE loop operation.

• Otherwise, it will execute the next instruction or the

(12)

flowchart of control structure:

Statement that will be executed if condition is

not true Condition

Statement that will be executed if condition is

true True

(13)

Example 1:

Condition:

A person can obtain a license

when he/

she is above 21 years old

Decision:

If true then she / he is qualified to have driving license.

(14)

START

Input age

END False

True

Output “Qualified”

(15)

3 Type of selection / decision

control structure:

(16)

Seelction 1: IF…….END IF

Rules:

If (condition)

Instruction (do this instruction if condition is true)

Endif

(17)

Flowchart:

START

Conditio

n Statement

END False

True

Pseudocode:

If (condition)

(18)

Example IF…ENDIF

• Workers who work on shift 3 will receive

additional Bonus RM50, where basic salary is

entered by workers.

Problem analysis: Input: 1. Shift

2. Basic_salary

Process: Bonus equals RM 50 If Shift equals to 3:

Salary equals Bonus plus Basic_salary

Output: Salary

Algorithm:

1. Enter Basic_salary, Shift 2. Bonus equals to RM 50 3. Check workers Shift

3.1 If Shift equals to 3

(19)

START

If Shift = 3 Salary = Basic_salary + Bonus

END False

True Bonus = 50

Input Shift, Basic_salary

Output Salary

Pseudocode:

START

(20)

Selection 2: IF…….ELSE

• A selection of control structure is used to

select between two options

Rules:

If (condition)

True statement Else

False statement Endif

Pseudocode:

If (condition)

True statement Else

(21)

Flowchart:

START

Conditio n

Statement 1

END

False True

(22)

Example IF…ELSE

• Prepare a problem analysis, algorithm, flowchart

and pseudocode to identify whether a student is

qualified to further her / his studies in any local

university using his / her SPM grade equal to 1.

Problem analysis:

Input: Grade Output: Display message qualified or not

Algorithm:

(23)

Flowchart: “Qualified to further study” Output “Not

(24)

Example 2 - IF…ELSE

Problem:

• Prepare the problem analysis, algorithm, flowchart and

pseudocode to find subtraction between two numbers that

users enter.

Problem analysis:

Input: num1, num2

Process: If num1 greater than num2 Result = num1 – num2 If not

Result = num2 – num1 Output: Result

Algorithm:

Enter num1, num2

Compare the 2 numbers

If num1 greater than num2 Result = num1 – num2 If not

(25)

Flowchart:

Input num1, num2

(26)

Selection 3: NESTED IF

• There are 3 types:

1. Type 1:

If (condition1)

If (condition2)

If (condition3) True statement Endif

Endif

(27)

2. Type 2:

If (condition1)

If (condition2) If (condition3)

Statement that will be executed if condition1, condition2 and condition3 are true Else

Statement that will be executed if condition1, and condition2 are true but condition3 is false

Endif

Else

Statement that will be executed if condition1 is true but condition2 and condition3 is false

Endif

Else

(28)

3. Type 3

If (condition1)

Statement that will be executed if condition 1 is true Else

If (condition 2)

Statement that will be executed if condition2 is true but condition1 is false Else

If (condition3)

Statement that will be executed if condition3 is true but condition1 and condition2 are false

Else

Statement that will be executed if condition1, condition2 and condition3 are false

Endif

Endif

(29)

Example 1 – NESTED IF

Problem:

(30)

Problem analysis:

Input: CGPA, Year, Salary. Process:

1. Check if the student application’s can be considered or not for a scholarship. 1.1 If Year greater than 1

1.1.1 If CGPA greater than or equal to 3.00

1.1.1.1 If Salary is less than or equal to RM500 Output “Your application is under consideration”.

Output: Student status

Algorithm:

1. Enter CGPA, Salary and Year

2. Check if the student application’s can be considered for a scholarship 2.1 If year > 1

2.1.1 If CGPA >= 3.00

(31)

START

END False True

Input Year, CGPA, Salary

Output “Your application is under

consideration” If Year > 1

If CGPA >= 3.00

If Salary <= 500 False

False True

True

(32)

Pseudocode:

START

Input Year, CGPA, Salary

If Year >1

If CGPA >= 3.00

If Salary <= RM500

Output “Your application is under consideration”

Endif

Endif

Endif

(33)

Example 2 – NESTED IF

Problem:

(34)

Problem analysis:

Input:

CGPA, Year, Salary

Process:

Check if a student can be considered for a scholarship

1.1 If Year > 1

1.1.1

If CGPA >= 3.00

1.1.1.1 If Gaji <= 500

Output “You application is under consideration”

1.1.1.2 If not

Output “Not success”

1.1.2

If not

Output “Not success”

If not

Output “Not success”

(35)

Algorithm:

1. Enter CGPA, Year, Salary

2. Check if a student can be considered for a scholarship

2.1 If Year > 1

2.1.1

If CGPA >= 3.00

2.1.1.1 If Salary <= RM500

Output “Your application is under consideration”.

2.1.1.2 If not

Output “Not success”

2.1.2

If not

Output “Not success”

2.2 If not

(36)

Flowchart:

False

START

END True

Input Year, CGPA, Salary

Output “Your application is under

consideration” Output “Not

success”

Output “Not success”

(37)

Pseudocode:

Input CGPA, Salary, Year

If (year > 1)

If (CGPA >= 3.00)

If (salary <= RM500)

Output “Your application is under consideration”

Else

Output ”Not success”

Endif

Else

Output ”Not success”

Endif

Else

(38)

Example 3 – NESTED IF

Problem:

• Education status is

determined based on

the GPA achievement

under the following

scheme:

GPA

Status

3.50-4.00

Dean List

2.00-3.49

Pass

(39)

Problem analysis: Input: GPA

Process:

1. If (GPA < 0.00 AND GPA > 4.00) Output “Invalid data”

2. If not

2.1 If (GPA >=3.5 AND GPA <= 4.00) Output “Dean List”

2.2 If not

2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass”

2.2.2 If not

2.2.2.1 If (GPA >= 1.80 AND GPA< 2.00) Output “Conditional Pass” 2.2.2.2 If not

Output “Fail”

(40)

Algorithm:

1. Enter student GPA

2. Compare student’s GPA to determine his/ her education status. 2.1 If (GPA < 0.00 AND GPA > 4.00)

Output “Invalid data” 2.2 If not

2.2.1 If (GPA >=3.50 AND GPA <= 4.00) Output “Dean List”

2.2.2 If not

2.2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass”

2.2.2.2 If not

2.2.2.2.1 If (GPA >= 1.80 AND GPA < 2.00) Output “Conditional Pass”

(41)

oFlowchart: Output “Invalid

data”

Output “Dean List” If GPA < 0.00 &&

“Conditional Pass” Output “Fail”

Output “Pass”

False True

(42)

Pseudocode:

START Input GPA

If ((GPA < 0.00) AND (GPA > 4.00)) Output "Invalid Data"

Else

If ((GPA >= 3.50) AND (GPA <= 4.00)) Output "Dean List"

Else

If ((GPA >=2.00) AND (GPA < 3.50)) Output "Pass"

Else

If ((GPA >= 1.80) AND (GPA < 2.00)) Output "Conditional Pass” Else

Output "Fail" Endif

Endif Endif

(43)

Symbol

Pseudo code

Symbol

C Language Example Result Description

AND && (1 > 3) && (10 < 20) If ((a < b) && (c > d)) printf(“Print a and c”);

FALSE Both sides of the condition must be true

OR || (1 > 3) || (10 < 20) must be true

(44)

3. Looping Control Structure

A programming control structure that

allows a

series of instructions

to be

executed more than

once

. The similar statement is repeated several

times

until the conditions are fulfilled

.

Three are 3 types of loop:

1. For

2. While

(45)

For For (initialize; condition; counter) True statement if condition is fulfilled Endfor

While While (condition) True statement Endwhile

Do…while Do

True statement While (condition)

Initialize value: a value to start a loop.

Counter: update-to increase or decrease the initialize value. • Rules for the condition:

1. If the condition is true / fulfilled, the process will be performed.

2. Then, the program loops back and recheck the condition, if the condition is true / fulfilled, repeat the process.

(46)

False

(47)

Example of loops usage:

Problem:

• Prepare the problem analysis, algorithm,

(48)

Problem analysis:

• Input: 5 numbers

• Process:

The process of adding

numbers will repeat until the

condition to exit the loop is

met.

• Output: Average of 5 numbers

Algorithm:

1. Initialize Counter=0; Average = 0; Total = 0

2. Input number

3. Add Total using formula:

Total = Total + number

4. Add Counter using formula:

Counter = Counter + 1

5. Compare whether Counter is greater than 5

If yes , go to step 6

If not, go to step 2

6. Calculate Average of numbers using formula;

Average = Total/5

(49)

Pseudocode:

For loop

While loop

Do…while loop

(50)

oFlowchart for For and While loops:

False

True

START

no = 0

Total = 0

For / While no <= 5

Input Num

Total = Total + Num

no = no + 1

Avg = Total/5

Output Avg

(51)

Note:

no: a variable to control loop structure whether to continue or exit from the loop

no + 1: a counter and it is very important. Without a counter, the loop will be infinite

Total = Total + Num: a variable to compute the value

Note:

no: a variable to control loop structure whether to continue or exit from the loop

no + 1: a counter and it is very important. Without a counter, the loop will be infinite

Total = Total + Num: a variable to compute the value

Avg = Total/5

Output Avg

END Avg = 0

(52)

Example

(53)

* Algorithm for For and While loops:

1.Initialize Counter = 0, Salary = 0;

2.Compare whether Counter is greater than 20 or not

If yes , out from the loop

If not , go to step 3

3. Enter Basic_salary, Claim, O_time

4. Calculate EPF:

EPF = Basic_salary * 0.09

5. Calculate Salary using this formula:

Salary = Basic_salary + Claim + O_time – EPF

6. Display Salary

7. Add Counter using the formula:

Counter = Counter + 1

(54)

Flowchart for For and While loops:

False True

START no = 0

For / While no <= 20

Input Basic_salary, Claim, O_time

Salary = Basic_salary + Claim + O_time – EPF

no = no + 1

EPF = Basic_salary * 0.09

Output Salary

(55)

Pseudocode for For loop:

START

no = 0 Salary = 0

For (no = 0, no <=20, no ++)

Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09

Salary = Basic_salary + Claim + O_time – EPF Output Salary

Endfor END

Pseudocode for While loop:

START

no = 0 Salary = 0 While no <=20

(56)

* Algorithm for Do……while loop:

1. Initialize Counter = 0, Salary = 0;

2. Input Basic_salary, Claim, O_time

3. Calculate EPF

EPF = Basic_salary * 0.09

4. Calculate Salary using this formula:

Salary = Basic_salary + Claim + O_time – EPF

5. Add Counter using this formula:

Counter = Counter + 1

6. Compare whether the Counter is greater than 20 or not

If yes , out of loop

(57)

Flowchart for Do…..while loop:

False True

START

no = 0

Salary = 0

no <= 5

Input Basic_salary, Claim, O_time

EPF = Basic_salary * 0.09

no = no + 1

Salary = Basic_salary + Claim + O_time – EPF

Output Salary

(58)

Pseudocode for Do…..while loop:

START

no = 0

Salary = 0

Do

{

Input Basic_salary, Claim, O_time

EPF = Basic_salary * 0.09

Salary = Basic_salary + Claim + O_time –

EPF

Referensi

Dokumen terkait

Himpunan Peraturan Gubernur Tahun 2014 1... Himpunan Peraturan Gubernur Tahun 2014

Banyak waktu yang terbuang dalam penenetuan arah pengambilan barang dan lamanya proses pelatihan kerja untuk pegawai tentang tata letak barang dalam proses order

Deskripsi Singkat : Pencetakan Pita Cukai adalah kegiatan yang meliputi penyediaan bahan baku berupa kertas sekuriti, pelekatan hologram

Qur’an di MTs Al Huda Bandung. Untuk mendeskripsikan strategi guru Al- Qur’ an hadits dalam meningkatkan kemampuan kefasihan siswa dalam membaca Al- Qur’an di MTs Al

tidak diterbitkan Dokumen V-Legal dan LVLK membuat Laporan Ketidaksesuaian untuk disampaikan kepada ETPIK atau ETPIK Non-Produsen dan Direktur Jenderal Bina Usaha

Berdasarkan hasil angket yang telah diisi oleh siswa, dapat ditarik kesimpulan bahwa respon siswa pada pernyataan angket berjenis positif memiliki respon

Selama periode penelitian data di ambil dari Sub divisi Bedah Onkologi dan departemen Patologi Anatomi dengan mengumpulkan blok paraffin jaringan penderita kanker

Therefore, the results obtained as regards an improved understanding of players' absolute and actual game time provide valuable information for planning specific