+ All Categories
Home > Documents > Chapter 3.3 - Control Structure

Chapter 3.3 - Control Structure

Date post: 26-May-2017
Category:
Upload: yeshwini-ramasamy
View: 224 times
Download: 5 times
Share this document with a friend
51
Programming Principles Chapter 3: Fundamentals of Programming Language This topic introduces the basic concept of programming. The topic includes identifier, data types, operators, expressions and program controls By: Miss Nur Azhani binti Rosly JTMK,PSIS
Transcript
Page 1: Chapter 3.3 - Control Structure

Programming PrinciplesChapter 3:

Fundamentals of Programming Language

This topic introduces the basic concept of programming. The topic includes identifier, data types, operators, expressions and program controls

By: Miss Nur Azhani binti RoslyJTMK,PSIS

Page 2: Chapter 3.3 - Control Structure

Learning Outcome• Upon completion of this course, students should be able to:

• Explain the basic computer and programming fundamentals with appropriate examples of language and technology.

• Apply the different types of algorithm to solve problem efficiently.

• Solve problem effectively by applying related theories of the basic programming language to a given particular scenario using programming life cycle.

Page 3: Chapter 3.3 - Control Structure

Apply Program Control Structures• Explain the control structures in problem solving:

• Sequence• Selection• Repetition (looping)

• Illustrate the flow of control structures using pseudo code and flowchart

• Write pseudo code and flowchart using control structures in solving given problems.

• Analyze the problem and design the algorithm for a given case study using problem solving tools

Page 4: Chapter 3.3 - Control Structure

CONTENTS• Program Flow (Control Structure)• Sequential Control Structure• Selection Control Structure

• If……Endif• If……Else• Nested If

• Loop Control Structure• For• While• Do …… While

Page 5: Chapter 3.3 - Control Structure

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 structure2. Selection / decision control structure

• If……endif• If……else • Nested if

3. Looping control structure• For• While• Do……while

Page 6: Chapter 3.3 - Control Structure

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

Page 7: Chapter 3.3 - Control Structure

• Format:Flowchart :

START

Statement A

END

Statement B

Pseudocode:

StartStatement AStatement B

End

Page 8: Chapter 3.3 - Control Structure

ExampleProblem: • Mathematical operation:

Get two numbers, then do adding, subtracting, multiplying and dividing operations.

Page 9: Chapter 3.3 - Control Structure

Algorithm:

1. Enter 2 numbers2. Add 2 numbers

Sum = number_1 + number_23. Minus 2 numbers

Subtract = number_1 – number_24. Multiply 2 numbers

Multiple = number_1 * number_25. Division of 2 numbers

Divide = number_1 / number_26. Display Sum, Subtract, Multiple and

Divide

Problem analysis:Input:number_1, number_2Process:Add 2 numbers:

Sum = number_1 + number_2Minus 2 numbers:

Subtract = number_1 – number_2 Multiply 2 numbers:

Multiple = number_1 * number_2Division 2 numbers:

Divide = number_1 / number_2Output:Sum, Subtract, Multiple and Divide

Page 10: Chapter 3.3 - Control Structure

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

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_2Output Sum, Subtract, Multiple, DivideEND

Page 11: Chapter 3.3 - Control Structure

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

instruction within the FALSE loop operation.

Page 12: Chapter 3.3 - Control Structure

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

False

Page 13: Chapter 3.3 - Control Structure

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.

Page 14: Chapter 3.3 - Control Structure

START

Input age

END

False

True

Output “Qualified”

Output “Not Qualified”

If age > 21

Page 15: Chapter 3.3 - Control Structure

3 Type of selection / decision control structure:

1. If……endif2. If……else 3. Nested if

Page 16: Chapter 3.3 - Control Structure

Selection 1: IF…….END IF• Rules:

If (condition)Instruction (do this instruction if condition is true)EndifIf condition is not true, no instruction will be executed

Page 17: Chapter 3.3 - Control Structure

Flowchart:

START

Condition Statement

END

False

True

Pseudocode:

If (condition) True statementEndif

Page 18: Chapter 3.3 - Control Structure

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_salaryProcess: Bonus equals RM 50

If Shift equals to 3:Salary equals Bonus plus Basic_salary

Output: Salary

Algorithm:1. Enter Basic_salary, Shift2. Bonus equals to RM 503. Check workers Shift

3.1 If Shift equals to 3 Salary= Basic_salary + Bonus4. Display Salary

Page 19: Chapter 3.3 - Control Structure

START

If Shift = 3 Salary = Basic_salary + Bonus

END

False

True

Bonus = 50

Input Shift, Basic_salary

Output Salary

Pseudocode:START Input Shift, Basic_salary Bonus = 50 If (Shift ==3) Salary = Basic_salary + Bonus End if Output SalaryEND

Flowchart:

Page 20: Chapter 3.3 - Control Structure

Selection 2: IF…….ELSE• A selection of control structure is used to

select between two options

Rules:If (condition)

True statementElse

False statementEndif

Pseudocode:If (condition)

True statementElse

False statementEndif

Page 21: Chapter 3.3 - Control Structure

Flowchart:

START

Condition

Statement 1

END

False True

Statement 2

Page 22: Chapter 3.3 - Control Structure

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

Algorithm:1. Enter Grade2. Check Grade 2.1 If Grade = 1

2.1.1 Output “Qualified to further study”

2.2 If not 2.2.1 Output “Not qualified to

further study”

Page 23: Chapter 3.3 - Control Structure

Flowchart:

START

If Grade == 1

END

False True

Input Grade

Output “Qualified to further study”

Output “Not qualified to further study”

Pseudocode:

START Input Grade If (Grade==1) Output “Qualified to further study” Else Output “Not qualified to further study” EndifEND

Page 24: Chapter 3.3 - Control Structure

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, num2Process: If num1 greater than num2

Result = num1 – num2If notResult = num2 – num1

Output: Result

Algorithm:Enter num1, num2Compare the 2 numbers

If num1 greater than num2 Result = num1 – num2

If not Result = num2 – num1

Display Result

Page 25: Chapter 3.3 - Control Structure

Flowchart:START

If num1 > num2

END

False True

Input num1, num2

Result = num1 – num2Result = num2 – num1

Output Result

Pseudocode:START Input num1, num2 If num1 > num2 Result = num1 – num2 Else Result = num2 – num1 Endif Output ResultEND

Page 26: Chapter 3.3 - Control Structure

Selection 3: NESTED IF• There are 3 types:1. Type 1:

If (condition1) If (condition2) If (condition3) True statement Endif EndifEndif

Page 27: Chapter 3.3 - Control Structure

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

EndifElse

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

Else Statement that will be executed if condition1 is falseEndif

Page 28: Chapter 3.3 - Control Structure

3. Type 3

If (condition1)Statement that will be executed if condition 1 is trueElse

If (condition 2) Statement that will be executed if condition2 is true but condition1 is falseElse

If (condition3) Statement that will be executed if condition3 is true but

condition1 and condition2 are falseElse Statement that will be executed if condition1, condition2 and

condition3 are falseEndif

EndifEnd if

Page 29: Chapter 3.3 - Control Structure

Example 1 – NESTED IFProblem:• To determine whether a candidate is qualified 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 RM1500, student will be considered for a scholarship.

Page 30: Chapter 3.3 - Control Structure

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 11.1.1 If CGPA greater than or equal to 3.00

1.1.1.1 If Salary is less than or equal to RM1500

Output “Your application is under consideration”.Output: Student status

Algorithm:1. Enter CGPA, Salary and Year2. Check if the student application’s can be considered for a scholarship

2.1 If year > 12.1.1 If CGPA >= 3.00

2.1.1.1 If salary <= RM1500Output “Your application is under consideration”3. Display status

Page 31: Chapter 3.3 - Control Structure

Pseudocode:START

Input Year, CGPA, SalaryIf Year >1

If CGPA >= 3.00If Salary <= RM1500Output “Your application is under consideration”Endif

EndifEndif

END

Page 32: Chapter 3.3 - Control Structure

START

END

False True

Input Year, CGPA, Salary

Output “Your application is under

consideration”

If Year > 1

If CGPA >= 3.00

If Salary <= 1500

False

False True

True

Flowchart:

Page 33: Chapter 3.3 - Control Structure

Example 2 – NESTED IFProblem:• 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 RM1500, student will be considered for a scholarship. If the student is not qualified, the message “Not success” will be displayed.

Page 34: Chapter 3.3 - Control Structure

Problem analysis:Input: CGPA, Year, Salary

Process: Check if a student can be considered for a scholarship

1.1 If Year > 11.1.1 If CGPA >= 3.00

1.1.1.1 If Salary<=1500Output “You application is under consideration”1.1.1.2 If not Output “Not success”1.1.2 If notOutput “Not success”

If notOutput “Not success”Output: Student status

Page 35: Chapter 3.3 - Control Structure

Algorithm:1. Enter CGPA, Year, Salary2. Check if a student can be considered for a scholarship

2.1 If Year > 12.1.1 If CGPA >= 3.00

2.1.1.1 If Salary <= RM1500Output “Your application is under consideration”.

2.1.1.2 If notOutput “Not success”

2.1.2 If notOutput “Not success”2.2 If notOutput “Not success”

Display status

Page 36: Chapter 3.3 - Control Structure

Pseudocode:

Input CGPA, Salary, YearIf (year > 1)

If (CGPA >= 3.00)If (salary <= RM1500)

Output “Your application is under consideration”Else

Output ”Not success”Endif

Else Output ”Not success”

EndifElse

Output ”Not success”Endif

Page 37: Chapter 3.3 - Control Structure

Flowchart:

False

START

END

True

Input Year, CGPA, Salary

Output “Your application is under

consideration”

If Year > 1

If CGPA >= 3.00

If Salary <= 1500

False

False True

True

Output “Not success”

Output “Not success”

Output “Not success”

Page 38: Chapter 3.3 - Control Structure

Example 3 – NESTED IFProblem: • 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

Page 39: Chapter 3.3 - Control Structure

Problem analysis:Input: GPAProcess: 1. If (GPA < 0.00 AND GPA > 4.00)

Output “Invalid data”2. If not

2.1 If (GPA >=3.50 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”

Output: Student education status

Page 40: Chapter 3.3 - Control Structure

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”2.2.2.2.2 If not

Output “Fail”3. Print status

Page 41: Chapter 3.3 - Control Structure

Pseudocode:STARTInput GPA

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

ElseIf ((GPA >= 3.50) AND (GPA <= 4.00))

Output "Dean List"Else

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

ElseIf ((GPA >= 1.80) AND (GPA < 2.00))

Output "Conditional Pass”Else

Output "Fail" Endif

EndifEndif

EndifEND

Page 42: Chapter 3.3 - Control Structure

oFlowchart:

False

START

END

FalseTrue

Input GPA

FalseTrue

TrueOutput “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

Flowchart:

Page 43: Chapter 3.3 - Control Structure

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. For2. While3. Do…while

Page 44: Chapter 3.3 - Control Structure

For For (initialize; condition; counter)True statement if condition is fulfilledEndfor

While While (condition) True statementEndwhile

Do…while Do True statementWhile (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.3. When the condition is false, then exit the loop.

Page 45: Chapter 3.3 - Control Structure

False

START

Initialize

Test condition A

Statement A

END

True

False

START

Initialize

Statement A

Test condition A

END

True

Format for the For and While loops: Format for Do …… while loop:

counter

counter

Page 46: Chapter 3.3 - Control Structure

Example of loops usage:Problem: • Prepare the problem analysis, algorithm,

flowchart and pseudocode for the average of 5 numbers. Data will be entered by user.

Page 47: Chapter 3.3 - Control Structure

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=1; Average = 0; Total = 02. Input number3. Add Total using formula:

Total = Total + number4. Add Counter using formula:

Counter = Counter + 15. Compare whether Counter is greater than 5

If yes , go to step 6If not, go to step 2

6. Calculate Average of numbers using formula;Average = Total/5

7. Display Average

Page 48: Chapter 3.3 - Control Structure

Pseudocode:

For loop While loop Do…while loop

START no = 1 Total = 0 Avg = 0For (no=1; no<=5; no++){ Input Num Total = Total + Num}Endfor Avg = Total/5 Output AvgEND

START no = 1 Total = 0 Avg = 0While (no <= 5){ Input Num Total = Total + Num no = no + 1}Endwhile Avg = Total/5 Output AvgEND

START no = 1 Total = 0 Avg = 0Do { Input Num Total = Total + Num no = no + 1} While (no <= 5) Avg = Total/5 Output AvgEND

Page 49: Chapter 3.3 - Control Structure

oFlowchart for For and While loops:

False

True

START

no = 1

Total = 0

For / While no <= 5

Input Num

Total = Total + Num

no = no + 1

Avg = Total/5

Output Avg

END

Avg = 0

Page 50: Chapter 3.3 - Control Structure

Note:no: a variable to control loop structure whether to continue or exit from the loopno + 1: a counter and it is very important. Without a counter, the loop will be infiniteTotal = Total + Num: a variable to compute the value

FalseTrue

START

no = 1

Total= 0

no <= 5

Input Num

Total = Total + Num

no = no + 1

Avg = Total/5

Output Avg

END

Avg = 0

oFlowchart for Do……while loop:

Page 51: Chapter 3.3 - Control Structure

THANK YOU!!


Recommended