Você está na página 1de 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

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.

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

CONTENTS
Program Flow (Control Structure) Sequential Control Structure Selection Control Structure
IfEndif IfElse Nested If For While Do While

Loop 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 structure 2. Selection / decision control structure Ifendif Ifelse Nested if 3. Looping control structure For While Dowhile

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.

Format:
Pseudocode: Start Statement A Statement B End Statement B Flowchart : START Statement A

END

Example
Problem: Mathematical operation: Get two numbers, then do adding, subtracting, multiplying and dividing operations.

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

START

Input number_1, number_2 Sum = number_1 + number_2 Subtract = number_1 number_2 Multiple = number_1 * number_2

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

Divide = number_1 / number_2

Output Sum, Subtract, Multiple, Divide

END

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.

flowchart of control structure:


Condition
False Statement that will be executed if condition is not true True

Statement that will be executed if condition is true

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.

START
Input age

If age > 21 False

True
Output Qualified

Output Not Qualified

END

3 Type of selection / decision control structure:


1. Ifendif 2. Ifelse 3. Nested if

Selection 1: IF.END IF
Rules: If (condition) Instruction (do this instruction if condition is true) Endif If condition is not true, no instruction will be executed

Pseudocode: If (condition) True statement Endif

Flowchart: START

Condition False

True Statement

END

Example IFENDIF
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

Flowchart:
START Input Shift, Basic_salary Bonus = 50 True

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

If Shift = 3 False

Output Salary END

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 False statement Endif

Flowchart: START

False

Condition

True

Statement 2

Statement 1

END

Example IFELSE
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 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 Algorithm: 1. Enter Grade 2. 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

Flowchart:
START

Pseudocode: START Input Grade If (Grade==1) Output Qualified to further study Else Output Not qualified to further study Endif END

Input Grade

False

If Grade == 1

True

Output Not qualified to further study

Output Qualified to further study

END

Example 2 - IFELSE
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 Result = num2 num1 Display Result

Flowchart:
START Input num1, num2

False

If num1 > num2

True

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

Result = num2 num1

Result = num1 num2

Output Result

END

Selection 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 condition3 is false Endif 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 1 NESTED IF
Problem: To determine whether a candidate is qualified to get a scholarship based on his / her study years, guardians salary and student CGPA. If the study year is more than 1, students CGPA is not less than 3.00 and guardians salary is below than RM1500, student will be considered for a scholarship.

Problem analysis: Input: CGPA, Year, Salary. Process: 1. Check if the student applications 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 RM1500 Output Your application is under consideration. Output: Student status Algorithm: 1. Enter CGPA, Salary and Year 2. Check if the student applications can be considered for a scholarship 2.1 If year > 1 2.1.1 If CGPA >= 3.00 2.1.1.1 If salary <= RM1500 Output Your application is under consideration 3. Display status

Pseudocode: START Input Year, CGPA, Salary If Year >1 If CGPA >= 3.00 If Salary <= RM1500 Output Your application is under consideration Endif Endif Endif END

Flowchart:
START
Input Year, CGPA, Salary

False

If Year > 1

True

False

If CGPA >= 3.00

True
Output Your application is under consideration

False

If Salary <= 1500

True

END

Example 2 NESTED IF
Problem: To determine whether a candidate is qualified or not to get a scholarship based on his / her study years, guardians salary and student CGPA. If the study year is more than 1, students CGPA is not less than 3.00 and guardians 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.

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 Salary<=1500 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 Output: Student status

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 <= RM1500 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 Display status

Pseudocode:

Input CGPA, Salary, Year If (year > 1) If (CGPA >= 3.00) If (salary <= RM1500) Output Your application is under consideration Else Output Not success Endif Else Output Not success Endif Else Output Not success Endif

Flowchart:
START Input Year, CGPA, Salary

False

If Year > 1 False

True

Output Not success

If CGPA >= 3.00 False

True

Output Not success

If Salary <= 1500

True

Output Your application is under consideration

Output Not success

END

Example 3 NESTED IF
Problem: Education status is determined based on the GPA achievement under the following scheme:
GPA 3.50-4.00 2.00-3.49 1.80-1.99 0.00-1.79 Status Dean List Pass Conditional Pass Fail

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

Algorithm: 1. Enter student GPA 2. Compare students 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

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 Endif END

oFlowchart:

START Input GPA

Flowchart:

True

If GPA < 0.00 && GPA > 4.00 True

False

Output Invalid data

If GPA >= 3.50 && GPA <= 4.00 True

False

Output Dean List

If GPA >= 2.00 && GPA < 3.50 True

False

Output Pass Output Conditional Pass

If GPA >= 1.80 && GPA < 2.00

False

Output Fail

END

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 3. Dowhile

For

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

While

Dowhile

1. 2.

Initialize value: a value to start a loop. Counter: update-to increase or decrease the initialize value. 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. 3. When the condition is false, then exit the loop.

Format for the For and While loops: START

Format for Do while loop: START

Initialize Initialize Statement A counter END True Test condition A False counter END

Test condition A True Statement A

False

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.

Problem analysis:

Algorithm: 1. Initialize Counter=1; Average = 0; Total = 0 2. Input number Input: 5 numbers 3. Add Total using formula: Process: Total = Total + number The process of adding 4. Add Counter using formula: numbers will repeat until the Counter = Counter + 1 condition to exit the loop is 5. Compare whether Counter is greater than 5 met. If yes , go to step 6 Output: Average of 5 numbers If not, go to step 2 6. Calculate Average of numbers using formula; Average = Total/5 7. Display Average

Pseudocode:

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

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

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

oFlowchart for For and While loops: START

no = 1
Total = 0 Avg = 0

For / While no <= 5

False

Avg = Total/5 Output Avg

True Input Num Total = Total + Num no = no + 1

END

oFlowchart for Dowhile loop:


START
no = 1 Total= 0

Avg = 0
Input Num

Total = Total + Num


no = no + 1 True no <= 5 False

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

THANK YOU!!

Você também pode gostar