Paper 2: Pseudocode | O Level Computer Science 2210 & IGCSE Computer Science 0478 | Detailed Free Notes To Score An A Star (A*)
- Basically pseudocode is not a programming language in the true sense.
- Instead, it allows you to present a complex program that you have in your mind and want to build in the future in a non-language intensive and pure logic form.
- As such, anyone can later understand that program in exactly what it has to do
- Some of the common functions in pseudocode are as follows:
- Variable
- It is a value that can change with time or iterations
- A constant can be considered something that does not change its data
- Identifier can be considered a name given to a variable, constant, subroutine or data structure.
- He won’t ask for definitions in exams
- We use indentation in pseudocode based on what is a sub part of something
- Declaration of variatble:
- Always use small letters for variables
- When you declare variables, it must be with either = to sign or a reverse arrow.
- I personally prefer equal to sign
- Declaration is the initial or original value of the variable
- For example a=0 means that initially, a has a value of zero.
- Remember, in the name of the variable, we don’t use spaces. The better way is to start with capital letters. For example say StudentName.
- We need to total at times. It means adding the inputs of numbers.
- For this, we use total=Â total + number
- Remember, we can use any variable for the totaling. For example, we can say a= a + nunber
- Similarly, we can use any variable for the number as well
- For example, we can say a = a +x
- The main thing is the addition here.
- Next, we need to count at times.
- It means with every step, we want to increase the count value by 1.
- For example, we can say count = count +1
- Remember, we can use any variable for counting
- For example a= a+1
- Just make sure that the variable you use is unique in the pseudocode and not being used to represent anything else.
- Data Types
- String contains a mix of texts, characters and numbers.
- It must always be inside “” otherwise it won’t display
- Integer
- Whole numbers only
- Real, Single, Double
- Decimal Numbers
- Boolean
- Either true or false, yes or no, or any two choices
- Char
- One character, number of symbol
- It must always be either in “” or ”.
- Arithmetic Operations
- +: Addition
- -: Subtraction
- *: Multiplication
- /: Division
- DIVL provides the while number that comes after the first number is divided by second. Ignores the remainder
- MOD: Ignores the whole number after the division, provides only the remainder.
- ^: Exponent or power of something.
- Remember, programs are run in a sequence
- They are run from top to down, each statement is run after the other step by step.
- Selection Rules
- We use = for equal to
- We use == for equal to as well
- We use <> for not equal to
- We use < for less than
- We use <= less than or equal to
- We use > for greater to
- We use >= for greater than equal to
- Decision making
- Sometimes, we need to decide something in the statement.
- We use IF statement for that purpose.
- Remember, the syntax is IF _____ THEN _____ ELSE____ ENDIF. Don’t miss the ENDIF part it is extremely important part of the syntax
- Here, we must remember, that after IF we provide the condition, then on the same line we write THEN
- Remember, for all parts of syntax we use capital letters
- Then, on the next line we write what has to happen, in an indent.
- Then, we write the ELSE part if an ELSE exists.
- Otherwise, we write ENDIF directly
- We write ELSE on the next line, then we write what to happen if the IF statement was not true on the next line, and then we write END IF.
- Nested IFs
- Sometimes, we need to write multiple IFs because there are multiple decisions involved.
- Basically, a nested if is an IF inside a IF.
- For example, if I input a number, and then I first need to decide if it is greater than 100, if it is not greater than hundred than I need to decide if it is less than 10. In such cases, I would use nested IF.
- Here, we will do as follows
- IF x>100 THEN
- a=a+x
- ELSE IF x< 10 THEN
- b=b+x
- END IF
- END IF
- IF x>100 THEN
- Remember, we need as many endifs as the total ifs we have used.
- We can have many different ifs inside the same statement as well.
- SELECT CASE
- Here, if we have a lot of options we need to decide, we can instead use SELECT CASE syntax instead of using a lot of nested IFs
- For example
- I need to check if the input country name is Pakistan, India, Bangladesh or any other, and based on this I need to increase the count of p, i, b or o.
- Therefore, I will use the following syntax.
- CASE OF x
- “Pakistan”
- p=p+1
- “India”
- i=i+1
- “Bangladesh”
- b=b+1
- OTHERWISE
- o=o+1
- “Pakistan”
- ENDCASE
- CASE OF x
- Remember, otherwise is a part of the syntax for all the options not mentioned directly.
- BOOLEAN OPEARTORS
- Basically, it comes under the logic gates section
- AND
- Both conditions are true then the result is true
- Even if one of the two conditions or both conditions false, the answer is false.
- OR
- Even if one of the conditions or both conditions true, then it will give true
- If both conditions false, it will give false.
- NOT
- It will reverse the condition
- If input true, then output false
- If input false then output true
- It will reverse the condition
- AND
- Basically, it comes under the logic gates section
- Iteration
- It means when the same activity has to be repeated constantly for a set number of times.
- It has three different types, we also call them different types of loops
- First, we use count-controlled iteration or what we call as the FOR TO NEXT Loop
- Remember, the FOR loop is used where we have an exact number of iterations to perform. For example, 30 times, 50 times etc.
- The syntax is as follows:
- FOR x = 1 TO 50
- Write the code part that we need to repeat
- NEXT x
- FOR x = 1 TO 50
- For example, if we need to take 30 inputs and then total all of them
- Therefore
- FOR x= 1 TO 30
- Input number
- a=a+number
- NEXT x
- FOR x= 1 TO 30
- It will take 30 inputs and then the loop will stop
- A second form of loop is called the pre-condition loop where the condition is checked at the start of the loop
- If the condition is not satisfied, the loop will never even run once. This is one amazing quality of the WHILE loop
- The structure is as follows
- WHILE condition DO
- Code that has to be repeated
- ENDWHILE
- WHILE condition DO
- For example, if we want a program that will output the first 10 numbers one by one, the following can be performed
- x=1
- WHILE x<11 DO
- OUTPUT(x)
- x=x+1
- ENDWHILE
- Here, once the number reaches more than 10, or becomes 11, the loop will stop
- Notice how we used the initial value 1
- Here, we must always think about the logic being applied
- Remember, the pseudocode system is flexible to help create the logic we want.
- Just ensure that the right logic is being applied
- Post condition Loop
- A loop where the condition is checked at the end. It means that the loop will run at least once.
- Here, the REPEAT UNTIL syntax is used
- REPEAT
- The code that is to be repeated
- UNTIL Condition
- We can convert the loops between each other as well with changes in the syntax.
- Some other commands are as follows
- Length of a written string
- We use LENGTH (“string”)
- It will return the number of characters.
- Remember, space is counted as a character as well.
- SUBSTRING (String, start character, number of characters)
- Here, the string is the actual sentence etc.
- The start character is the letter, starting the count from the left side, from where the new string will be generated
- The number of characters is the number of letters to be extracted.
- For example SUBSTRING (“I am Hunain”, 6, 4) will return “Huna”, as the fifth character, counting the spaces is H, and the 4 characters including it are Huna.
- Length of a written string
- Upper and Lower case
- UPPER (string) will convert the complete string to upper case letters
- LOWER (string) will convert the entire string to lower case letters
- Subroutine
- A complete self-contained code that has its own identifier.
- The benefit is that we write such a code once, and then we can just use its identifier to reference wherever it will be used directly without writing it again.
- Two types
- Procedures
- It does not return a value, just runs the code
- PROCEDURE identifier ()
- Code to run
- ENDPROCEDURE
- For example,
- PROECDURE Add5Inputs()
- FOR Count = 1 TO 5
- Input Number
- a=a+number
- NEXT Count
- FOR Count = 1 TO 5
- ENDPROCEDURE
- Now, wherever I need to perform the same action again, I will just write Add5Inputs() and it will do the entire code written above inside the procedure
- Procedures
- A function does a different jo, it will return an answer based on the input we provide
- FUNCTION identifier()
- Code that we perform
- RETURN value
- ENDFUNCTION
- FUNCTION identifier()
- For example a function that will first ask the user to enter two values, then add them together and then return the value that is the total will be as follows
- FUNCTION Add()
- OUTPUT (“Enter a number”)
- Input Num1
- OUTPUT (“Enter another number”)
- Input Num2
- Return Num1+Num2
- ENDFUNCTION
- FUNCTION Add()
- Now, the function will provide the totaled value.
- Scope
- An area of the program where it can be accessed
- Global is what can be used by any part of the program
- Local variable is one that can be used inside a specific area of the program
- Not tested in our exams.
- Parameters
- Â A value that a main program sends to the subroutine
- It is declared within the brackets of the subroutine
- Rarely tested in exams
- Variable
- Library Routines
- ROUND limits the number of decimal places to a certain extent
- For example, ROUND (Number, the number of decimal places)
- For example ROUND (5.1512512, 2) will give 5.15
- RANDOM
- It generates a random number between 2 values
- RANDOM(1,100).
- Arrays
- An array is a data structure where we can store multiple pieces of information in the same structure.
- For example, storing 30 values separately.
- 1-dimensional array has one row of data
- Each space on the array is called an index
- Arrays can have 0 or 1 as the first space, in pseudocode it is usually decided in advance.
- If I want to store the number 25 in the first location of the array, I can write
- NumberStore(0)= 25
- We can also output the data as well or take it out as per requirement.
- We can put it in a loop as well
- FOR i = 1 TO 10
- OUTPUT (Colors(i))
- NEXT i
- Here, the 10 outputs will be displayed from the items stored in the array one by one.
- 2-dimensional arrays
- Two indices are used, for example Color(0,0)
- Reading from a file rarely tested in exams
- Writing on a file is also rarely tested.
