• Tidak ada hasil yang ditemukan

# PROGRAM CONTROL STRUCTURES.ppt

N/A
N/A
Protected

Membagikan "PROGRAM CONTROL STRUCTURES.ppt"

Copied!
58
0
0

Teks penuh

(1)

(2)

## OBJECTIVE

### Explain the control structures in problem solving:

• Sequence • Selection

• Repetition (looping)

(3)

## CONTENTS

(4)

### is a group of statements that performs one particular task

void welcome ( ) {

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

(5)

(6)

(7)

Flowchart :

START

Statement A

END Statement B

Pseudocode:

Start

(8)

## Example

(9)

### Divide

Problem analysis: Input:

number_1, number_2

Process:

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)

(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)

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

(14)

START

Input age

END False

True

Output “Qualified”

(15)

(16)

## Seelction 1: IF…….END IF

(17)

Flowchart:

START

Conditio

n Statement

END False

True

Pseudocode:

If (condition)

(18)

### 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

### 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

### 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

### 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

consideration” If Year > 1

If CGPA >= 3.00

If Salary <= 500 False

False True

True

(32)

(33)

## Example 2 – NESTED IF

(34)

(35)

### 2.2 If not

(36)

Flowchart:

False

START

END True

Input Year, CGPA, Salary

consideration” Output “Not

success”

Output “Not success”

(37)

(38)

## Example 3 – NESTED IF

### 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

### 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:

(48)

(49)

Pseudocode:

### 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)

### 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)

### 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:

### EPF

Referensi

Dokumen terkait

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

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

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

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