34
Pseudocode Pseudocode Skill Area 312 Part C

Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

Embed Size (px)

Citation preview

Page 1: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

PseudocodePseudocodeSkill Area 312 Part C

Page 2: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Variable Types• Key Terms• Pseudocode• Pseudocode (Sequence)• Pseudocode (Selection)• Pseudocode (Iteration)• Examples

Page 3: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples
Page 4: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

Reserved Words

• Words that has specific roles in the context in which it occurs, which cannot be used for other purposes.

Example:Example: IF – THEN – ELSEIF – THEN – ELSE DO – WHILE - DO – WHILE -

LOOPLOOP

Example:Example: PRINT, “”, :=PRINT, “”, :=

KeywordKeyword A A symbolsymbol in in

programming programming language that has a language that has a special meaningspecial meaning for for the compiler or the compiler or interpreter.interpreter.

Page 5: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Pseudocode consists of short, English phrases used to explain specific tasks within a program’s algorithm.

Page 6: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Used to provide an outline description of the specification for the software module

• Contain a natural language of expressions embedded in syntactic structures taken from programming language– IF…THEN…ELSE– REPEAT..UNTIL

• Not intended to be executed by a computer, must be interpreted by people.

Page 7: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

PSEUDOCODEPSEUDOCODESEQUENCESEQUENCE

Page 8: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end.

• This is a basic assumption of all algorithm design.

• We call this SEQUENCE.

Page 9: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• In pseudo-code it looks like this:

Page 10: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• For example, for making a cup of tea:

Page 11: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Or as a program:

Page 12: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

PSEUDOCODEPSEUDOCODESELECTIONSELECTION

Page 13: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• What if we want to make a choice– example, do we want to add sugar or not to

the tea?

• We call this SELECTION.

Page 14: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So, we could state this as:

IF (sugar is required) THEN add sugar; ELSE don’t add sugar;ENDIF;

IF (sugar is required) THEN add sugar; ELSE don’t add sugar;ENDIF;

Page 15: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Or, in general:

IF (<CONDITION>) THEN <Statements>; ELSE <Statements>;ENDIF;

IF (<CONDITION>) THEN <Statements>; ELSE <Statements>;ENDIF;

Page 16: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Or to check which number is biggest:

IF (A > B) THEN Print A + “is bigger”; ELSE Print B + “is bigger”;ENDIF;

IF (A > B) THEN Print A + “is bigger”; ELSE Print B + “is bigger”;ENDIF;

Page 17: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Adding a selection statement in the program:BEGIN

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END.

BEGINPROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END.

Page 18: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

PSEUDOCODEPSEUDOCODEITERATIONITERATION

Page 19: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• What if we need to tell the computer to keep doing something until some condition occurs?

• Let’s say we wish to indicate that the you need to keep filling the kettle with water until it is full.

• We need a LOOP, or ITERATION.

Page 20: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

WHILE Loop

REPEAT UNTIL Loop

FOR Loop

Page 21: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So, we could state this as:

• Or to print out the numbers 1 to 5:

A = 1;WHILE(A > 5) DO Print A; A = A + 1;ENDWHILE;

A = 1;WHILE(A > 5) DO Print A; A = A + 1;ENDWHILE;

Page 22: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

Get first entryIf this is the required entry

Then write down phone numberElse get next entryIf this is the correct entry

then write done entryelse get next entry

if this is the correct entry

Get first entryIf this is the required entry

Then write down phone numberElse get next entryIf this is the correct entry

then write done entryelse get next entry

if this is the correct entry

We may rewrite this as follows:

Get first entry;Call this entry N;WHILE N is NOT the required entryDO Get next entry;

Call this entry N;ENDWHILE;

Get first entry;Call this entry N;WHILE N is NOT the required entryDO Get next entry;

Call this entry N;ENDWHILE;

Consider the problem of searching for an entry in a phone book with only condition:

Page 23: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• Or as a program:

BEGINPROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END

BEGINPROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END

Page 24: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

PSEUDOCODEPSEUDOCODEEXAMPLESEXAMPLES

Page 25: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So let’s say we want to express the following algorithm:– Read in a number and print it out.

BEGINPROGRAM PrintNumber:Use variables: A OF TYPE Integer Read A; Print A;END

BEGINPROGRAM PrintNumber:Use variables: A OF TYPE Integer Read A; Print A;END

Page 26: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So let’s say we want to express the following algorithm:– Read in a number and print it out double the number

BEGINPROGRAM PrintDoubleNumber:Use variables: A,B OF TYPE Integer Read A; B = A*2; Print B;END

BEGINPROGRAM PrintDoubleNumber:Use variables: A,B OF TYPE Integer Read A; B = A*2; Print B;END

Page 27: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So let’s say we want to express the following algorithm to print out the bigger of two numbers:– Read in two numbers, call them A and B. Is A is bigger than B, print out A, otherwise print out

B.

BEGINPROGRAM PrintBiggerOfTwo:Use variables: A,B OF TYPE Integer Read A; Read B; IF (A>B) THEN Print A; ELSE Print B; ENDIF;END

BEGINPROGRAM PrintBiggerOfTwo:Use variables: A,B OF TYPE Integer Read A; Read B; IF (A>B) THEN Print A; ELSE Print B; ENDIF;END

Page 28: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So let’s say we want to express the following algorithm to print out the bigger of three numbers:– Read in three numbers, call them A, B and C.

• If A is bigger than B, then if A is bigger than C, print out A, otherwise print out C. • If B is bigger than A, then if B is bigger than C, print out B, otherwise print out C.

Page 29: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

BEGINPROGRAM BiggerOfThree:Use variables: A,B,C OF TYPE Integer Read A; Read B; Read C; IF (A>B) THEN IF (A>C) THEN Print A; ELSE Print C; END IF; ELSE IF (B>C) THEN Print B; ELSE Print C; END IF; END IF;END

BEGINPROGRAM BiggerOfThree:Use variables: A,B,C OF TYPE Integer Read A; Read B; Read C; IF (A>B) THEN IF (A>C) THEN Print A; ELSE Print C; END IF; ELSE IF (B>C) THEN Print B; ELSE Print C; END IF; END IF;END

Page 30: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

• So let’s say we want to express the following algorithm:– Print out the numbers from 1 to 5

BEGINPROGRAM Print1to5:Use variables: A OF TYPE Integer A = 1; WHILE (A != 6) DO Print A; A = A + 1; ENDWHILE;END

BEGINPROGRAM Print1to5:Use variables: A OF TYPE Integer A = 1; WHILE (A != 6) DO Print A; A = A + 1; ENDWHILE;END

Page 31: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

BEGINPROGRAM ComputeSum:Use variables: number1, number2, sum

OF TYPE IntegerDISPLAY “Enter the numbers”ACCEPT number1, number2Sum:=number1 + number2DISPLAY “SUM”END

BEGINPROGRAM ComputeSum:Use variables: number1, number2, sum

OF TYPE IntegerDISPLAY “Enter the numbers”ACCEPT number1, number2Sum:=number1 + number2DISPLAY “SUM”END

Page 32: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

BEGINPROGRAM CategoryOfInsurance:Use variables: category OF TYPE character

insurance OF TYPE stringACCEPT categoryDO CASE of category

CASE category = UDISPLAY Insurance := “not available”

CASE category = ADISPLAY Insurance := “double”

CASE category = BDISPLAY Insurance := “normal”

CASE category = MDISPLAY Insurance := “medically dependent”

OTHERWISE DISPLAY “entry is invalid”ENDCASEEND

BEGINPROGRAM CategoryOfInsurance:Use variables: category OF TYPE character

insurance OF TYPE stringACCEPT categoryDO CASE of category

CASE category = UDISPLAY Insurance := “not available”

CASE category = ADISPLAY Insurance := “double”

CASE category = BDISPLAY Insurance := “normal”

CASE category = MDISPLAY Insurance := “medically dependent”

OTHERWISE DISPLAY “entry is invalid”ENDCASEEND

Page 33: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

BEGINPROGRAM NumberRange0-100:Use variables: number

OF TYPE IntegerREPEAT

DISPLAY “Enter a numberbetween 0and 100”

ACCEPT numberUNTIL number <0 or number >100END

BEGINPROGRAM NumberRange0-100:Use variables: number

OF TYPE IntegerREPEAT

DISPLAY “Enter a numberbetween 0and 100”

ACCEPT numberUNTIL number <0 or number >100END

Page 34: Pseudocode Skill Area 312 Part C. Variable Types Key Terms Pseudocode Pseudocode (Sequence) Pseudocode (Selection) Pseudocode (Iteration) Examples

Create a flowchart for inputting your employee’s name. If the first letter starts from A to J then display “ First Category” , if K to T then display “ Second Category” otherwise display Third Category

Create a flowchart for inputting your employee’s name. If the first letter starts from A to J then display “ First Category” , if K to T then display “ Second Category” otherwise display Third Category

If student's grade is greater than or equal to 60Print "passed“ else Print "failed“If student's grade is greater than or equal to 60Print "passed“ else Print "failed“

Set the counter to 0. If the counter is more than 10 then customer receives free value meal otherwise no free value meal.

Set the counter to 0. If the counter is more than 10 then customer receives free value meal otherwise no free value meal.