0% found this document useful (0 votes)
8 views28 pages

03 - Algorithms Controliteration

Uploaded by

Wong Jia Yuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views28 pages

03 - Algorithms Controliteration

Uploaded by

Wong Jia Yuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Algorithms- Control- Iteration

CT010-3-1 Python Programming


Topic & Structure of the lesson

• Problem Solving Using Programmed


Solutions
– Introduction to control structure and iteration
– Program design using pseudocodes and
simple notations of flowcharts
• control structure
– if
– case
• iteration

CT010-3-1 Fundamentals of Software Development Module Briefing Slide 2 (of 22)


Learning outcomes

• At the end of this lecture you should be


able to:
– define a problem
– outline a solution using algorithms –
• selective control structure
• repetitive iteration

CT010-3-1 Fundamentals of Software Development Module Briefing Slide 3 (of 22)


Key terms you must be able to
use
• If you have mastered this topic, you should
be able to use the following terms correctly
in your assignments and exams:

– Algorithms
• Pseudocode
• Flowchart

CT010-3-1 Fundamentals of Software Development Module Briefing Slide 4 (of 22)


Selection Control Structure

• Represents the decision making capabilities of the


computer

• Used to illustrate a choice between two or more


actions depending on whether the condition is true
or false

CT010-3-1 Fundamentals of Software Development Module Briefing


Selection Control Structure

• Condition
– based on comparison between two items
– usually expressed with one of the following relational
operators
< Less than
> Greater than
== Equal to
<= Less than or equal to
>= Greater than or equal to
!= or <> Not equal to

CT010-3-1 Fundamentals of Software Development Module Briefing


Selection Control Structure

• A choice is made between two alternative paths,


depending on the result of a condition being true or false.
• Represented using the keywords
IF ( ) THEN

ELSE

ENDIF

CT010-3-1 Fundamentals of Software Development Module Briefing


Selection Control Structure

• Simple Selection (Simple IF statement)


• Simple Selection with null false branch (null ELSE
statement)
• Combined Selection (Combined IF statement)
– AND
– OR
• Nested Selection (Nested IF statement)
– Linear Nested
– Non-linear nested

CT010-3-1 Fundamentals of Software Development Module Briefing


1. Simple Selection (simple IF statement)

• a choice is made between two alternative paths,


depending on the result of a condition being true or
false.
• is represented using the keywords
IF, THEN, ELSE and ENDIF.

CT010-3-1 Fundamentals of Software Development Module Briefing


1. Simple Selection (simple IF statement)

Pseudocode: Flowchart:

IF (condition) THEN
Action_1 (Condition)
False True
ELSE
Action_2
Action_2 Action_1
ENDIF

CT010-3-1 Fundamentals of Software Development Module Briefing


2. Simple Selection with null false branch
(null ELSE statement)

Pseudocode: Flowchart:

IF (condition) THEN
(Condition) True
Action_1
ENDIF

Action_1
False

CT010-3-1 Fundamentals of Software Development Module Briefing


3. Combined Selection
(combined IF statement)

Pseudocode: Flowchart:

IF ((condition_1) AND (condition_2)) THEN


Action_1 (Condition_1) AND
False (Condition_2)
ELSE True

Action_2
ENDIF
Action_2 Action_1

CT010-3-1 Fundamentals of Software Development Module Briefing


3. Combined Selection
(combined IF statement)
Pseudocode: Flowchart:

IF ((condition_1) OR (condition_2)) THEN


Action_1
(Condition_1) OR
ELSE False (Condition_2)
True
Action_2
ENDIF
Action_2 Action_1

CT010-3-1 Fundamentals of Software Development Module Briefing


4. Nested Selection
(nested IF statement)
Pseudocode:
IF (condition_1) THEN
Action_1
ELSE
IF (condition_2) THEN
Action_2
ELSE
IF (condition_3) THEN
Action_3
ELSE
Action_4
ENDIF
ENDIF
ENDIF Linear Nested
CT010-3-1 Fundamentals of Software Development Module Briefing
4. Nested Selection
(nested IF statement)
Linear Nested

(Condition_1) False
True

(Condition_2) False
Action_1
True

Action_2 (Condition_3)
True False

Action_3 Action_4

CT010-3-1 Fundamentals of Software Development Module Briefing


4. Nested Selection
(nested IF statement)
Pseudocode:
IF (condition_1) THEN
IF (condition_2) THEN
IF (condition_3) THEN
Action_1
ELSE
Action_2
ENDIF
ELSE
Action_3
ENDIF
ELSE
Action_4 Non-linear nested
ENDIF
CT010-3-1 Fundamentals of Software Development Module Briefing
4. Nested Selection
(nested IF statement)

Non-linear nested
(Condition_1) True
False

(Condition_2) True
Action_4
False

Action_3 (Condition_3)
False True

Action_2 Action_1

CT010-3-1 Fundamentals of Software Development Module Briefing


Selection Control Structure
CASE OF
Pseudocode
CASE OF variable
= Constant_1: Action_1
= Constant_2: Action_2
.
.
OTHERWISE
Action_for_others
ENDCASE

CT010-3-1 Fundamentals of Software Development Module Briefing


Selection Control Structure
CASE OF

CASE OF
variable

= Constant_1 = Constant_2 = Constant_3 OTHERWISE

Action_1 Action_2 Action_3 Action_4

CT010-3-1 Fundamentals of Software Development Module Briefing


Iteration Using The DOWHILE
Structure
• Most programs require same logic to be
repeated for several sets of data
• Pseudocode: (counter-controlled)
initialization
DOWHILE (condition)
statement_block
incrementation
ENDDO

CT010-3-1 Fundamentals of Software Development Module Briefing


Iteration Using The DOWHILE Structure

Flowchart
(Counter-controlled) Initialization

(Condition) True

False
Action_1

Incrementation /
Decrementation

CT010-3-1 Fundamentals of Software Development Module Briefing


Iteration Using The DOWHILE Structure

• Pseudocode: (sentinel-controlled)
Prompt user for value
Read value
DOWHILE (condition is true for value)
statement_block
Prompt user for value
Read value
ENDDO

CT010-3-1 Fundamentals of Software Development Module Briefing


Iteration Using The DOWHILE Structure

• Flowchart: Prompt user for value

(sentinel-controlled) Read value

(Condition is
True
true for value)

False
Action_1

Prompt user for value

Read value

CT010-3-1 Fundamentals of Software Development Module Briefing


Iteration Using The LOOP
Statement
• Pseudocode:
LOOP VarName FROM InitVal TO EndVal STEP increment_value
Action_1
NEXT VarName
ENDLOOP

CT010-3-1 Fundamentals of Software Development Module Briefing


Iteration Using The LOOP
Statement
• Flowchart
VN

in end

VN – variable_name incre

in – initial value
end – ending value Action_1

incre - incrementation

VN

CT010-3-1 Fundamentals of Software Development Module Briefing


Follow up Assignment

1. You are required to design a complete system which will


enable you to calculate the actual amount of salary which an
employee is to obtain from a company based on the income
tax table provided below.
Salary Range Income Tax(%)
0 to < 3000 0
3000 to < 5000 6
5000 to < 7000 8
7000 to < 8000 12
8000 to < 9000 16
>= 9000 40

CT010-3-1 Fundamentals of Software Development Module Briefing


Summary of Main Teaching
Points
• Problem Solving Using Programmed
Solutions
– Introduction to control structure and iteration
– Program design using pseudocodes and
simple notations of flowcharts
• control structure
– if
– case
• iteration

CT010-3-1 Fundamentals of Software Development Module Briefing


Next Session

• Problem Solving Using Programmed


Solutions
– Introduction to control structure and iteration
– Program design using pseudocodes and
simple notations of flowcharts
• control structure
– if
– case
• iteration

CT010-3-1 Fundamentals of Software Development Module Briefing

You might also like