• Tidak ada hasil yang ditemukan

Fundamentals of Programming

N/A
N/A
Akshay Krishna

Academic year: 2023

Membagikan "Fundamentals of Programming"

Copied!
36
0
0

Teks penuh

(1)

Fundamentals of Programming

Topic 1 : Introduction to Problem Solving Topic 2 : Program Design

(2)

Content

IPO Model

Algorithm

Pseudocode

Sequence

Selection control

Repetition control

Flowchart

Sequence

Selection control

Repetition control

Desk checking

(3)

Learning Outcomes

By the end of this session, you should be able to

Produce the IPO model defining the input, output and processes of a solution in response to problems posed.

Write the pseudocode to solve various problems posed.

Perform desk checking for the algorithm.

(4)

• Define the problem – Input / Output

• Outline the solution – Process (IPO)

• Develop the outline into an algorithm – Pseudocode & Flowchart

• Test the algorithm for correctness – Desk checking

• Code the algorithm into a specific programming language

• Run the program on the computer

• Document and maintain the program

Program

Development

Cycle

(5)

IPO model

Input Processing Output

(6)

IPO model

Sample Question 1:

Design a program that allows the user to enter values for the width and length of a room’s floor in feet. The program outputs the area of the floor in square feet. The program should also then compute and output the number of 12-inch square tiles needed to tile the floor.

(7)

Six basic computer operations

A computer can receive information (READ, GET)

A computer can put out information (DISPLAY, PRINT)

A computer can perform arithmetic (+, -, *, /, %)

A computer can assign a value to a variable or memory location (height = 150) (area = length * width)

A computer can compare two variables and select one of two alternative actions (if statement, switch statement)

A computer can repeat a group of actions (while loop, do…while loop, for loop)

(8)

Algorithm

Lists the steps involved in accomplishing a task

a set of detailed, unambiguous and ordered

instructions developed to describe the processes

necessary to produce the desired output from a given input

Is written in simple English

Can be defined in programming terms

Popular ways of representing algorithm

Pseudocode

Flowchart

(9)

Three basic control

structures

Sequence

Straight forward execution of one processing step after another

Each instruction will be executed in the order in which it appears

Selection

Presentation of a condition and the choice between two actions, the choice depending on whether the condition is true or false

Represents the decision making abilities of the computer

Repetition

Presentation of a set of instructions to be performed repeatedly as long as a condition is true (until a terminating condition occurs)

Intialising and subsequent incrementing of the variable tested in the condition is an essential feature

(10)

Pseudocode

• Is structured English

Formalised and abbreviated to look like the high-level computer languages

• There is no standard pseudocode

• Authors adopt their own special techniques and sets of rules – that resemble a particular

programming language

(11)

Selection Control

• Represents the decision making abilities of a computer

To illustrate a choice between two or more actions depending on whether a condition is true or false

• The condition in the IF statement is expressed using either relational operators or equality operators

(12)

Operators used in conditions

Relational Operator Meaning

< Less than

> Greater than

< = Less than or equal to

> = Greater than or equal to

Equality Operator Meaning

== Equals to

<> Not equals to

(13)

Simple Selection

(Simple IF statement)

(14)

Simple Selection with null false branch

(null ELSE statement)

(15)

Combined Selection

(Combined IF statement)

IF (condition_1) AND (condition_2) THEN action if both conditions are true

ELSE

action if either one or both conditions are false ENDIF

Condition_1 Condition_2 (Condition_1) AND (Condition_2)

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE

(16)

Combined Selection (Combined IF

statement)

(17)

Combined Selection

(Combined IF statement)

IF (condition_1) OR (condition_2) THEN

action if either one or both conditions are true ELSE

action if both conditions are false ENDIF

Condition_1 Condition_2 (Condition_1 )OR (Condition_2)

TRUE TRUE TRUE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE

(18)

Combined Selection (Combined IF

statement)

(19)

Combined Selection (Combined IF

statement)

IF NOT(condition) THEN

action if the NOT(condition) result is false ELSE

action if the NOT(condition) result is true ENDIF

Condition NOT (Condition)

TRUE FALSE

FALSE TRUE

(20)

Combined Selection (Combined IF

statement)

(21)

Nested Selection (nested IF

statement)

Linear nested IF statements – one variable to compare

IF (condition_1) THEN

action if condition_1 is true ELSE

IF (condition_2) THEN

action if condition_ 1 is false but condition_2 is true ELSE

IF (condition_3) THEN

action if condition_1 and condition_ 2 are false but condition_3 is true ELSE

action if condition_1, condition_2 and condition_3 are false ENDIF

ENDIF ENDIF

(22)

Nested Selection (nested IF

statement)

Non-linear nested IF statements – many variables to compare

IF (condition_1) THEN IF (condition_2) THEN IF (condition_3) THEN

action if condition_1, condition_ 2 and condition_3 are true ELSE

action if condition_1 and condition_2 are true but condition_3 is false ENDIF

ELSE

action if condition_1 is true but condition_2 is false ENDIF

ELSE

action if condition_1 is false ENDIF

(23)

CASEOF Structure

CASEOF (variable)

value_1 : statement block_1 value_2 : statement block_2

value_n : statement block_n other : statement block_other ENDCASE

(24)

Looping structure

• Will cause the processing logic to be repeated a number of times

• 3 ways the instructions can be repeated

A counted number of times (counted loop)

At the beginning of the loop (leading decision loop)

At the end of the loop (trailing decision loop)

(25)

A counter-

controlled loop

• Occurs when the exact number of loop iterations is known in advance

DOWHILE … ENDDO structure

LOOP … FROM … TO … STEP… structure

(26)

DOWHILE structure

(min to max)

INITIALISE counter as minValue DOWHILE (counter <= maxValue)

Action while the condition is true INCREMENT counter

ENDDO

Action while the condition is false

(27)

DOWHILE structure

(max to min)

INITIALISE counter as maxValue DOWHILE (counter >= minValue)

Action while the condition is true DECREMENT counter

ENDDO

Action while the condition is false

(28)

LOOP … FROM … TO

…structure (min to max)

LOOP variable FROM minValue TO maxValue STEP increment_value Action

ENDLOOP

(29)

LOOP … FROM … TO

…structure (max to min)

LOOP variable FROM maxValue TO minValue STEP decrement_value Action

ENDLOOP

(30)

A leading

decision loop

• DOWHILE … ENDDO structure

• The condition is tested before any statements are executed

Counter controlled (as seen before this)

Sentinel controlled

(31)

DOWHILE…ENDDO structure

(Sentinel controlled)

PROMPT user for initial value (provide the sentinel value) READ value

DOWHILE (value <> sentinel_value) Action while condition is true

PROMPT user for next value (provide the sentinel value) READ value

ENDDO

Action when condition above is false

(32)

DOWHILE…

ENDDO structure (Sentinel

controlled)

BEGIN

DISPLAY “Do you want to begin? (Y – to continue /N – to end)”

READ choice

DOWHILE (choice =‘Y’) OR (choice =‘y’) DISPLAY “Desmond is smart”

DISPLAY NEWLINE

DISPLAY “Do you want to continue? (Y – to continue /N – to end)”

READ choice ENDDO

DISPLAY “GoodBye”

END

(33)

A trailing decision loop

• REPEAT … UNTIL structure

• The condition is tested at the end of the loop

Statements within the loop will be executed once before the condition is tested

When the condition is false, the statements will be executed until the condition is true

Is opposite of DOWHILE…ENDDO structure

Normally has an IF statement within

(34)

A trailing decision loop

REPEAT

PROMPT user for value (provide sentinel_value) READ value

IF (value <> sentinel_value) THEN

Action is executed only if condition is true ENDIF

UNTIL (value = sentinel_value)

(35)

A trailing decision loop

BEGIN REPEAT

DISPLAY “Do you want to run program? (Y or N)”

READ choice

IF (choice<> ‘N’) OR (choice<> ’n’) THEN DISPLAY “You are all smart”

ENDIF

UNTIL (choice = ‘N’) OR (choice = ‘n’) DISPLAY “Goodbye”

END

(36)

Desk checking

• Checking the solution algorithm

• Tracing through the logic of the algorithm with some chosen data

Referensi

Dokumen terkait