PSEUDOCODE CONSTRUCT GUIDE PSEUDOCODE CONSTRUCT GUIDE FOR CIE COMPUTER SCIENCE 0478 / 2210 0 PSEUDOCODE CONSTRUCT GUIDE TABLE OF CONTENTS Introduction ..................................................................................................................................... 2 What does this guide contains? ..................................................................................................... 2 Pseudocode Constructs ................................................................................................................... 3 Overview of pseudocode constructs.............................................................................................. 3 1. Datatypes ............................................................................................................................... 4 2. Declaration ............................................................................................................................. 5 3. Assignment .............................................................................................................................. 7 4. Input / Output ......................................................................................................................... 9 5. Decision Making .................................................................................................................... 11 IF‐THEN .............................................................................................................................. 11 CASE‐OF ............................................................................................................................. 13 6. Looping .................................................................................................................................14 FOR‐NEXT ...........................................................................................................................14 WHILE ................................................................................................................................. 15 REPEAT‐UNTIL ...................................................................................................................16 Appendix ......................................................................................................................................... 17 1 PSEUDOCODE CONSTRUCT GUIDE INTRODUCTION WHAT DOES THIS GUIDE CONTAINS? This guide contains essential material and syntax rules for writing pseudocode. Example resources at the end of document also explains the common type of questions that appears in CIE. However, it should be remembered that this is not an extensive guide for learning pseudocode and should only be used as a helper reference guide. Also, please remember that there is no set standard of writing pseudocode and each teacher/institute uses their own rules to develop pseudocode. For example, some teachers might use the keyword SET or DECLARE for variable declaration, others might not even use it. Both the approaches are correct as CIE syllabus (year 2016) does not define any particular method of doing so. This guide only covers a specific set of commands & methodology as taught by the authors in their own schools and does not cover all possible alternatives. Students should not get confuse if any particular teacher does not use the same syntax rules as defined in this guide. At the end, remember that pseudocode is all about logic building and not syntax writing! However, these rules will help you writing good code when you move on to formal programming languages in ‘A levels’. Go ahead and get started! 2 PSEUDOCODE CONSTRUCT GUIDE OVERVIEW OF PSEUDOCODE CONSTRUCTS Pseudocode Storage I/O Selection Variable INPUT IF‐THEN Constant PRINT CASE‐OF Repetition Counter FOR‐NEXT Conditional WHILE‐ END WHILE REPEAT‐ UNTIL Array 3 PSEUDOCODE CONSTRUCT GUIDE 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 , ‐25 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.85 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. 4 PSEUDOCODE CONSTRUCT GUIDE 2. DECLARATION Declaration is a way to tell the computer that you are going to use a particular variable so it should reserve specific memory to it as well as label it with the name that you give in the declaration. It is required for variables, constants and arrays and is mostly done at the start of pseudocode, however it is not compulsory and we can declare them anywhere in our pseudocode but beware that declaration should always be before the use of variable, constant or array. VARIABLES SET variable_name AS data_type Where: Variable_name is the name of variable. It can be anything but should be meaningful and signify the purpose. Variable names should start with an alphabet. 4num is not a valid variable name since it starts with a number. data_type is the data type of variable. It could be integer, float, char or string. Example SET student name AS String SET height AS Float CONSTANTS constant_name Value CONST AS datatype Where: constant_name is the name of constant. It can be anything but should be meaningful and signify the purpose. Constant names should start with an alphabet. 4num is not a valid variable name since it starts with a number. data_type is the data type of variable. It could be integer, float, char or string. Value is the fixed value assigned to variable which will not be changed afterwards in pseudocode or flowchart Example CONST pi 3.141 AS Float CONST color “Red” AS String Beware Constant’s value is assigned directly in the declaration. Its value can be read but cannot be changed later on. 5 PSEUDOCODE CONSTRUCT GUIDE ARRAYS SET array_name [1:max] SET array_name [max] AS AS datatype 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. 6 PSEUDOCODE CONSTRUCT GUIDE 3. 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.4 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. 7 PSEUDOCODE CONSTRUCT GUIDE ARRAYS array_name [ index ] value array_name [ index ] another_variable Where: array_name is the name of array that has been already declared. Value is the data to be assigned to array. It should match with the data type of array. String / Text values are always enclosed in double quotes ( “ “ ) and Char values are enclosed in single quotes ( ‘ ‘ ). Integer & float values are directly assigned. Index is the particular element/location in an array Example Company [3] “Blitz Computing” Active [6] ‘Y’ price [43] 3454.35 floors [5] 5 Beware Always assign values to array according to its data type. Assigning text data to an array that is declared as Integer is wrong and may lose marks in exams. 8 PSEUDOCODE CONSTRUCT GUIDE 4. 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. 9 PSEUDOCODE CONSTRUCT GUIDE 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 10 PSEUDOCODE CONSTRUCT GUIDE 5. DECISION MAKING Decision making or branching is a way to direct the flow of pseudocode depending upon a particular condition. We can use these constructs to check for a condition and depending upon the result either do something or not. There are 2 constructs for decision making in pseudocode; CASE‐OF and IF‐THEN. IF‐THEN have 2 variations; one which checks single condition and the other which can check against a series of conditions. Both have slight differences in their syntax and are explained below. IF – THEN (Single condition) 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” END IF IF chocolate = “Babli” count count + 1 END IF 11 THEN PSEUDOCODE CONSTRUCT GUIDE 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 12 PSEUDOCODE CONSTRUCT GUIDE 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 > 20: 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 13 PSEUDOCODE CONSTRUCT GUIDE 6. LOOPING Looping or repetition is a way to repeat certain pseudocode instructions either a set number of times (counting based) or depending upon a particular condition (condition based). Hence there are 2 constructs for looping; FOR‐NEXT for counting based looping and WHILE & REPEAT‐UNTIL for condition based looping. WHILE loop and REPEAT‐UNTIL loop have slight difference as how they work. It is explained in their examples below: FOR‐NEXT FOR counter_variable start TO end Instructions NEXT variable Where: counter_variable is the variable which does the counting in the loop. Its value will “start” from the number that you define and will go all the way up to “end” value. By default, the counting is always done in the increments of +1 but you can change its stepping by using STEP keyword (see second example below). Also counting can be done in reverse as well (see last example). instructions are the pseudocode statements that will be repeated Example FOR counting 1 to 75 PRINT “This will be printed 75 times” NEXT counting FOR counting 1 to 40 STEP 2 PRINT “This will be printed 20 times” NEXT counting FOR counting 20 to 1 PRINT “This will be printed 20 times” NEXT counting 14 PSEUDOCODE CONSTRUCT GUIDE 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. 15 PSEUDOCODE CONSTRUCT GUIDE 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 Repeat statements fixed number of times No condition checking WHILE REPEAT‐UNTIL Repeats statements when condition is true Repeats statements when condition if false Checks condition at the start of loop Checks condition at the end of loop 16 PSEUDOCODE CONSTRUCT GUIDE APPENDIX – A LIST OF ALL PSEUDOCODE KEYWORDS SET IF AS THEN CONST ELSE REAL END IF FLOAT WHILE INTEGER DO CHAR END WHILE STRING REPEAT TEXT UNTIL INPUT FOR READ TO OUTPUT NEXT PRINT CALL The above words should not be used as variable, constant or array names as they represent commands which have special functionality. 17 PSEUDOCODE CONSTRUCT GUIDE 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. 18