Pseudo-code Guide for 2210 Computer Science O Levels, Slides of Programming Languages

A proper guide of how to write pseudo code in o levels exams.

Typology: Slides

2018/2019

Uploaded on 08/23/2019

syed-saad-hassan-74627-tchr-bhlp
syed-saad-hassan-74627-tchr-bhlp šŸ‡µšŸ‡°

3.5

(2)

1 document

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PSEUDOCODECONSTRUCTGUIDE
0

PSEUDOCODE
CONSTRUCT
GUIDE
FORCIECOMPUTERSCIENCE0478/2210
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Pseudo-code Guide for 2210 Computer Science O Levels and more Slides Programming Languages in PDF only on Docsity!

PSEUDOCODE

CONSTRUCT

GUIDE

FOR CIE COMPUTER S CIENCE 0478 / 2210

TABLE OF CONTENTS

  • Introduction
    • What does this guide contains?
  • Pseudocode Constructs
    • Overview of pseudocode constructs..............................................................................................
      1. Datatypes
      1. Declaration
      1. Assignment
      1. Input / Output
      1. Decision Making
      • IF‐THEN
      • CASE‐OF
      1. Looping
      • FOR‐NEXT
      • WHILE
      • REPEAT‐UNTIL ...................................................................................................................
  • Appendix

PSEUDOCODE

CONSTRUCT

GUIDE

Pseudocode

Storage

VariableConstant

Array

I/O

INPUT PRINT

Selection

IF

‐THEN CASE

‐OF

Repetition

Counter

FOR

‐NEXT

Conditional

WHILE

‐

END

WHILE REPEAT

‐

UNTIL

OVERVIEW

OF

PSEUDOCODE

CONSTRUCTS

  1. DATA TYPES

Following the common data types that are used in pseudocode when declaring variables, constants and arrays. Some data types can be referred with more than one name; alternative names are also listed.

INTEGER Data type to hold whole numbers i.e. numbers without decimal value. It contains both positive and negative numbers ļ„ Example: 56 , 27 , ‐  Use: Mainly used for counting, whole number addition/subtraction etc.

FLOAT / REAL

Data type to hold floating point numbers i.e. numbers with decimal value. It contains both positive and negative decimal numbers ļ„ Example: 72.84 , 0.014 , ‐12.  Use: Mainly used for money, weight, height, temperature etc.

CHAR

Data type to hold a ā€œsingleā€ alphanumeric character. ļ„ Example: R , K , s  Use: Mainly used for codes like T or F (True/False), M or F(male/female) etc.

TEXT / STRING

Data type to hold alphanumeric characters. It includes all printable characters including alphabets, numbers, special symbols and punctuation marks. ļ„ Example: Google , CIE2016 , R@N#  Use: Mainly used for names, address etc.

ARRAYS

SET array_name [1:max] AS datatype SET array_name [max] AS datatype Where: array_name is the name of array. It can be anything but should be meaningful and signify the purpose. Array names should start with an alphabet. 4num is not a valid array name since it starts with a number. datatype is the data type of array. It could be integer, float, char or string. max is the size of an array. It is the number of elements in an array. ļ„ Example ļ‚§ SET marks [1:10] AS Integer ļ‚§ SET father names [50] AS String  Beware ļ‚§ An array’s index number cannot be a floating point value nor it can be negative value. It can only be a positive integer value.

  1. ASSIGNMENT

Values can be assigned to variables, constants and arrays at either declaration or afterwards in

pseudocode.  sign is used to assign values. An important rule to remember is that the values are

always assigned to the variable that is on the left hand side, hence the direction of the arrow will ALWAYS be towards the left hand side.

VARIABLES variable_name  Value variable_name  another_variable

Where: variable_name is the name of variable that has been already declared. Value is the data to assigned to variable. It should match with the data type of variable. String / Text values are always enclosed in double quotes ( ā€œ ā€œ ) and Char values are enclosed in single quotes ( ā€˜ ā€˜ ). Integer & float values are directly assigned. another_variable is some other variable containing a value.

ļ„ Example

ļ‚§ name  ā€œKokoā€

ļ‚§ gender  ā€˜F’

ļ‚§ height  5.

ļ‚§ age  20

 Beware ļ‚§ Always assign values to variables according to its data type. Assigning text data to a variable that is declared as Integer is wrong and may lose marks in exams.

  1. INPUT / OUTPUT

Taking user input is done using INPUT keyword and output is printed using PRINT keyword. Input can only be stored in either variable or array. Output can be displayed from variables, arrays, constants and fixed values (string literals).

INPUT INPUT variable_name INPUT array_name [index]

Where: variable_name is the name of variable in which to store the input. array_name is the name of array in which to store the input. Index is the particular element/location in an array

ļ„ Example

ļ‚§ INPUT name

ļ‚§ INPUT class [6]

ļ‚§ INPUT name, father_name, age

 Important

ļ‚§ Multiple inputs can be taken using single INPUT command by separating them

with comma_. (see above example; last one)_

 Beware

ļ‚§ Input cannot be taken in constants. Their value is assigned during declaration

and cannot be changed anywhere in pseudocode.

ļ‚§ An array cannot be filled directly by using single INPUT command. You have to

use loop to fill array since it is a collection of variables.

OUTPUT

PRINT variable_name PRINT array_name [index] PRINT ā€œstring_literalā€ Where: variable_name is the name of variable to be printed. array_name is the name of an array to be printed. Index is the particular element/location in an array string_literal is a fixed text value to be printed. For example a message like ā€œThe answer isā€.

ļ„ Example

ļ‚§ PRINT name

ļ‚§ PRINT class [6]

ļ‚§ PRINT ā€œYour name is ā€œ

ļ‚§ PRINT name, father_name, age

 Important

ļ‚§ Multiple values can be printed using single OUTPUT command by separating

them with comma_. (see above example; last one)_

 Beware

ļ‚§ String literal values should always be in double quotes as they are treated as text

ļ‚§ An array cannot be printed directly by using single OUTPUT command. You have

to use loop to print array since it is a collection of variables

IF – THEN ‐ ELSE (Multiple conditions)

IF condition THEN Instructions ELSE IF condition THEN Instructions END IF

Where: condition is the condition to evaluate. Its answer will always be in the form of True or False. instructions are the pseudocode statements that will only be executed if the condition evaluates to true. In simple words, these lines of code will only run if the condition’s answer is True otherwise they will not run and computer will directly jump to line after END IF statement

ļ„ Example

ļ‚§ IF weight < 20 THEN

PRINT ā€œYou are underweightā€ ELSE IF weight < 35 THEN PRINT ā€œYour weight is normal END IF

ļ‚§ IF chocolate = ā€œBabliā€ THEN

count  count + 1

ELSE IF chocolate = ā€œkokoā€ THEN

count2  count + 2

END IF

CASE‐OF does the same thing as IF‐THEN but it is a bit more concise and clearer to read.

CASE‐OF CASE variable OF Compare1: Instructions Compare2: Instructions END CASE Where: Variable is the variable which contains the value that you want to check for. Compare1-2 are the values which you compare with variable (defined with CASE command). There can be as many compare statements as you need to test. There is no limit. instructions are the pseudocode statements that will only be executed if the compare evaluates to true. In simple words, these lines of code will only run if the condition’s answer is True otherwise they will not run and computer will directly jump to line after END CASE statement

ļ„ Example

ļ‚§ CASE weight OF

PRINT ā€œYou are underweightā€ < 35: PRINT ā€œYour weight is normal END CASE

ļ‚§ CASE chocolate OF

ā€œBabliā€:

count  count + 1

ā€œkokoā€:

count2  count + 2

END CASE

ļ‚§  Beware

ļ‚§ Counting can only be done using integer numbers. Floating point counting is not

possible in FOR loop nor does it make any sense.

WHILE – END WHILE

WHILE condition DO Instructions END WHILE Where: condition is the condition to check. If the result of comparison returns TRUE , only then the statements inside DO will be executed. instructions are the pseudocode statements that will only be executed if the comparison evaluates to true. In simple words, these lines of code will only run if the condition’s answer is True otherwise they will not run and computer will directly jump to line after END WHILE statement

ļ„ Example

ļ‚§ WHILE weight > 100

DO

PRINT ā€œYou are overweightā€ END WHILE

 Important

ļ‚§ WHILE is a ā€œtrue‐basedā€ loop. It will only repeat the instructions if the result of

checking condition is TRUE.

REPEAT – UNTIL

REPEAT

Instructions UNTIL condition Where: condition is the condition to check. If the result of comparison returns FALSE , only then the statements inside REPEAT will be executed. instructions are the pseudocode statements that will only be executed if the comparison evaluates to false. In simple words, these lines of code will only run if the condition’s answer is False otherwise they will not run and computer will directly jump to line after UNTIL statement

ļ„ Example

ļ‚§ REPEAT

PRINT ā€œYou are overweightā€ UNTIL weight < 120

 Important

ļ‚§ REPEAT is a ā€œfalse‐basedā€ loop. It will only repeat the instructions if the result of

checking condition is FALSE.

DIFFERENCES BETWEEN FOR‐NEXT, WHILE & REPEAT‐UNTIL

FOR‐NEXT WHILE REPEAT‐UNTIL

ļ‚· Repeat statements fixed number of times

ļ‚· Repeats statements when condition is true

ļ‚· Repeats statements when condition if false

ļ‚· No condition checking ļ‚·^ Checks start of^ conditionloop^ at^ the^ ļ‚·^ Checks of loop^ condition^ at^ the^ end

APPENDIX – B

MATHEMATICAL OPERATORS

Following are the basic arithmetic operators that allow us to perform different types of mathematical calculations in our flowchart/pseudocode.

OPERATOR MEANING + Addition ‐ Subtraction ***** Multiplication / Division mod Remainder ← Assignment^ operator.^ Used^ to^ assign^ values^ to variables/constant/array.

COMPARISION OPERATORS

Comparison operators allows us to compare different values.

**OPERATOR MEANING

** Greater^ than < Less^ than > = Greater^ than^ or^ equal^ to < = Less^ than^ or^ equal^ to < > Not^ equal^ to = Equal^ to^ (In^ sense^ of^ comparison)

Two values can be compared using the above operators and the answer would be in the form of YES/TRUE or NO/FALSE. For example; if we say 5 > 7, then the answer would be NO/FALSE since 5 is not greater than 7.