36
F1001 PROGRAMMING FUNDAMENTALS CONTROL STRUCTURE Sequential Control Structure Selection Control Structure Decision Control Structure 30 UNIT 3

Unit 3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

CONTROL STRUCTURE

Sequential Control Structure

Selection Control Structure

Decision Control Structure

30

UNIT

3

Page 2: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

BLOCKS OF CODE

Every programming language consists of thousand of statements.

Thus, to simplify the execution of the programming language, all the statements in the programming

language is divided into blocks of code that have specific functions.

Statement in a block of code is arranged sequentially in a program.

A block of code is a group of statements that performs one particular task.

The block is separated in several ways, like curly bracket “{“ and “}”, Begin and End statement and

others.

Function is a block of code that has been assigned a name.

Function is used as reference and execution for a block of code.

Combination of blocks of code will produce a perfect program to be executed in a computer.

Since the program comes from the different blocks of code, so, the flow needs to be controlled.

Example of blocks of code:

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.

Control structure uses single entry and single exit; meaning it has only one beginning and one ending.

There are three types of control structures:

1. Sequential control structure

2. Selection / decision control structure

a) If……endif

b) If……else

c) Nested if

3. Looping control structure

a) For

b) While

c) Do……while

31

void welcome ( ){

printf (“****************************”);printf (“********WELCOME **********”);printf (“****************************”);

}

BLOCK OF

CODE

BLOCK OF

CODEBLOCK

SEPARATOR

BLOCK

SEPARATOR

Page 3: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Sequential Control Structure

In this control, every step will be executed one by one from top to bottom.

Every box in control structure is a process. Every process is done sequentially.

Format:

Pseudocode:

Start

Statement A

Statement B

End

Example 1:

Compute the total overtime wages of an employee.

Problem analysis:

Input:

o Hours

o Basic_salary

o OT_rate

Process:

o Overtime equals OT_rate times Hours

o Salary equals Basic_salary plus Overtime

Output:

o Salary

Algorithm:

1. Enter Hours, Basic_salary, OT_rate

2. Calculate overtime using formula:

Overtime = OT_rate * Hours

3. Calculate salary using formula:

Salary = Basic_salary + Overtime

4. Output Salary

32

Flowchart

START

Statement A

END

Statement B

Page 4: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Pseudocode:

START

Input Hours, Basic_salary, OT_rate

Overtime = OT_rate * Hours

Salary = Basic_salary + Overtime

Output Salary

END

Flowchart:

Example 2:

Problem: Mathematical operation: Get two numbers, then do adding, subtracting, multiplying

and dividing operations.

Problem analysis:

Input:

o number_1, number_2

Process:

o Add 2 numbers:

Sum = number_1 + number_2

o Minus 2 numbers:

Subtract = number_1 – number_2

o Multiply 2 numbers:

33

START

Input Hours, Basic_salary, OT_rate

Overtime = OT_rate * Hours

Output Salary

END

Salary = Basic_salary + Overtime

Page 5: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Multiple = number_1 * number_2

o Division 2 numbers:

Divide = number_1 / number_2

Output:

o Sum, Subtract, Multiple and Divide

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

Flowchart:

34

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

Page 6: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Pseudocode:

START

Input number_1, number_2

Sum = number_1 + number_2

Subtract = number_1 - number_2

Multiple = = number_1 * number_2

Divide = number_1 / number_2

Output Sum, Subtract, Multiple, Divide

END

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 instruction within the FALSE loop operation.

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. If not he / she is not qualified to

have a driving license.

Below is a flowchart of control structure:

35

Page 7: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

1. IF…….ENDIF

Rules:

If (condition)

Instruction (do this instruction if condition is true)

Endif

If condition is not true, no instruction will be executed

36

START

Input age

END

False

True

Output “Qualified”

Output “Not Qualified”

If age > 21Statement that will be executed if condition

is not true

Condition

Statement that will be executed if condition

is true

True

False

Type of selection / decision control structure:

1. If……endif

2. If……else

3. Nested if

Page 8: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Pseudocode:

If (condition)

True statement

Endif

Example 1:

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

Salary= Basic_salary + Bonus

4. Display Salary

37

Flowchart:START

Condition

Statement

END

False

True

Page 9: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Flowchart:

Pseudocode:

START

Input Shift, Basic_salary

Bonus = 50

If (Shift ==3)

Salary = Basic_salary + Bonus

End if

Output Salary

END

38

START

If Shift = 3

Salary = Basic_salary + Bonus

END

False

True

Bonus = 50

Input Shift, Basic_salary

Output Salary

Page 10: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

2. IF…….ELSE type

A selection of control structure is used to select between two options

Rules:

If (condition)

True statement

Else

False statement

Endif

Flowchart:

Example 1:

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

o Problem analysis:

Input: Grade

Process: If Grade is equal to 1

Output “Qualified to further study”.

If not

Output “Not qualified to further study”.

Output: Display message qualified or not

39

Pseudocode:

If (condition)

True statement

Else

False statement

Endif

START

Condition

Statement 1

END

False True

Statement 2

Page 11: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o Algorithm:

1. Enter Grade

2. Check Grade

1.1 If Grade = 1

1.1.1 Output “Qualified to further study”

1.2 If not

1.2.1 Output “Not qualified to further study”

o Flowchart:

o Pseudocode:

START

Input Grade

If (Grade==1)

Output “Qualified to further study”

Else

Output “Not qualified to further study”

Endif

END

40

START

If Grade == 1

END

False True

Input Grade

Output “Qualified

to further study”

Output “Not qualified

to further study”

Page 12: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Example 2:

o Problem:

Prepare the problem analysis, algorithm, flowchart and pseudocode to find

subtraction between two numbers that users enter.

o Problem analysis:

Input: num1, num2

Process: If num1 greater than num2

Result = num1 – num2

If not

Result = num2 – num1

Output: Result

o Algorithm:

1. Enter num1, num2

2. Compare the 2 numbers

2.1 If num1 greater than num2

2.1.1 Result = num1 – num2

2.2 If not

2.2.1 Result = num2 – num1

3. Display Result

o Flowchart:

41

START

If num1 > num2

END

False True

Input num1, num2

Result = num1 – num2Result = num2 – num1

Output Result

Page 13: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o Pseudocode:

START

Input num1, num2

If num1 > num2

Result = num1 – num2

Else

Result = num2 – num1

Endif

Output Result

END

3. NESTED IF

There are 3 types:

1. Type 1:

If (condition1)

If (condition2)

If (condition3)

True statement

Endif

Endif

Endif

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 condition2 is false

Endif

42

Page 14: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Else

Statement that will be executed if condition1 is true but condition2

and condition3 is false

Endif

Else

Statement that will be executed if condition1 is false

Endif

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

End if

Example Type 1:

o Problem:

To determine whether a candidate is qualified or not to get a scholarship based on his /

her study years, guardian’s salary and student CGPA. If the study year is more than 1,

student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500,

student will be considered for a scholarship.

o Problem analysis:

Input: CGPA, Year, Salary.

Process:

43

Page 15: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

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

o 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

2.1.1.1 If salary <= RM500

Output “Your application is under consideration”

3. Display status

o Flowchart:

44

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

Page 16: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o 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

END

Example Type 2:

o Problem:

To determine whether a candidate is qualified or not to get a scholarship based on his /

her study years, guardian’s salary and student CGPA. If the study year is more than 1,

student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500, student

will be considered for a scholarship. If the student is not qualified the message “Not

success” will be displayed.

o Problem analysis:

Input: CGPA, Year, Salary

Process:

1. 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”

1.2 If not

Output “Not success”

Output: Student status

45

Page 17: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o 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

Output “Not success”

3. Display status

o Flowchart:

46

False

START

END

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

Output “Not success”

Output “Not success”

Output “Not success”

Page 18: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o 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

Output ”Not success”

Endif

Example Type 3:

o 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

1.80-1.99 Conditional Pass

0.00-1.79 Fail

o Problem analysis:

Input: GPAProcess:

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

2. If not2.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 not2.2.2.1 If (GPA >= 1.80 AND GPA< 2.00)

Output “Conditional Pass”

47

Page 19: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

2.2.2.2 If notOutput “Fail”

Output: Student education status

o 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.1If (GPA >= 1.80 AND GPA < 2.00)

Output “Conditional Pass”

2.2.2.2.2If not

Output “Fail”

3. Print status

48

Page 20: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o Flowchart:

o 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

49

False

START

END

FalseTrue

Input GPA

FalseTrue

True

Output “Invalid

data”

Output “Dean

List”

If GPA < 0.00 && GPA > 4.00

If GPA >= 3.50 && GPA <= 4.00

If GPA >= 2.00 && GPA < 3.50

If GPA >= 1.80 && GPA < 2.00

Output

“Conditional Pass”Output “Fail”

Output “Pass”

FalseTrue

Page 21: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

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

Endif

END

Usage of Logical Operator:

Symbol

(Pseudocode)

Symbol

(C Language)

Example Result Description

AND && (1 > 3) && (10 < 20) FALSE Both sides of the condition must

be true

OR || (1 > 3) || (10 < 20) TRUE Either one of the condition must

be true

NOT ! ! (10 < 20) FALSE Change the operation either

from true to false or vise versa

o From the above table, the logical operator are AND, OR and NOT.

o Example using of AND operator:

o If condition (a < b) is true AND condition (c > d) is true then “Print a and c” will be

displayed. If one of the condition is not true then “Print a and c” will not be displayed.

o Example usage of OR operator:

50

If ((a < b) && (c > d))

printf(“Print a and c”);

If ((sales > 5000) || (hoursworked> 81))

bonus = 500;

else

bonus = 0;

Page 22: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o If the sales are more than 5000 OR working hours are more than 81, bonus RM500 will

be given. If either condition is not fulfilled, still the amount of RM500 will still be given

as bonus.

o If both conditions are false, then bonus will not be given.

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 three types of loop:

o For

o While

o Do…while

For While Do…while

For (initialize; condition; counter)

True statement if condition is

fulfilled

Endfor

While (condition)

True statement

Endwhile

Do

True statement

While (condition)

o Initialize value: a value to start a loop.

o Counter: to increase or decrease the initialize value.

o Rules for the condition:

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

- Then, the program loops back and recheck the condition, if the condition is true /

fulfilled, repeat the process.

- When the condition is false, then exit the loop.

Format for the For and While loops:

51

Format for Do …… while loop:

False

START

Initialize

Test condition

A

Statement A

END

True

False

START

Initialize

Statement A

Test condition

A

END

True

Page 23: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Example of loops usage:

o Problem: Prepare the problem analysis, algorithm, flowchart and pseudocode for the average of 5

numbers. Data will be entered by user.

o 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

o 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

7. Display Average

o Pseudocode:

For loop While loop Do…while loop

START

no = 0

Total = 0

Avg = 0

For (no=0; no<=5; no++)

Input Num

Total = Total + Num

Endfor

Avg = Total/5

Output Avg

END

START

no = 0

Total = 0

Avg = 0

While (no <= 5)

Input Num

Total = Total + Num

no = no + 1

Endwhile

Avg = Total/5

Output Avg

END

START

no = 0

Total = 0

Avg = 0

Do {

Input Num

Total = Total + Num

no = no + 1

} While (no <= 5)

Avg = Total/5

Output Avg

END

52

Page 24: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o Flowchart for For and While loops:

53

False

True

START

no = 0

Total = 0

For / While no

<= 5

Input Num

Total = Total + Num

no = no + 1

Avg = Total/5

Output Avg

END

Avg = 0

Page 25: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o Flowchart for Do……while loop:

o Example: To compute salary of 20 employees

Algorithm for For and While loops:

54

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

FalseTrue

START

no = 0

Total= 0

no <= 5

Input Num

Total = Total + Num

no = no + 1

Avg = Total/5

Output Avg

END

Avg = 0

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

8. Back to step 2

Page 26: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Flowchart for For and While loops:

Pseudocode for For loop:

55

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

END

Salary = 0

STARTno = 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

Page 27: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Pseudocode for While loop:

Algorithm for Do……while loop:

56

STARTno = 0

Salary = 0

While no <=20

Input Basic_salary, Claim, O_time

EPF = Basic_salary * 0.09

Gaji = Basic_salary + Claim + O_time – EPF

Output Salary

no = no + 1

Endwhile

END

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

If not, go to step 2

7. Display Salary

Page 28: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

Flowchart for Do…..while loop:

Pseudocode for Do…..while loop:

57

FalseTrue

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

END

STARTno = 0

Salary = 0

Do

{

Input Basic_salary, Claim, O_time

EPF = Basic_salary * 0.09

Salary = Basic_salary + Claim + O_time – EPF

Output Salary

no = no + 1

} While (no <= 20)

END

Page 29: Unit 3

F1001 PROGRAMMING FUNDAMENTALS

o Dummy Data or Sentinel Value

- Value used to end the program. Dummy data must be different from the data to be

entered.

- Example:

START

Input nameWhile name != “XXX”

Output name

Input mark

Output mark

Input name

Endwhile

END

58

Dummy data