Programming Concepts
Programming Concepts
Programming Concepts
Data Types
A data type is a classification of data into groups according to the kind of data they
represent
Computers use different data types to represent different types of data in a program
The basic data types include:
o Integer: used to represent whole numbers, either positive or negative
Examples: 10, -5, 0
o Real: used to represent numbers with a fractional part, either positive or negative
Examples: 3.14, -2.5, 0.0
o Char: used to represent a single character such as a letter, digit or symbol
Examples: 'a', 'B', '5', '$'
o String: used to represent a sequence of characters
Examples: "Hello World", "1234", "@#$%
o Boolean: used to represent true or false values
Examples: True, False
We can declare variables as follows:
It is important to choose the correct data type for a given situation to ensure accuracy and
efficiency in the program.
Computer Science 2210 By Sajid Ali Imam
Worked example
Name and describe the most appropriate programming data type for each of the examples
of data given. Each data type must be different. [6]
Data: 83
Data type name: Integer [1]
Data type description: The number is a whole number [1]
Data: [email protected]
Data type name: String [1]
Data type description: It is a group of characters [1]
Data: True
Data type name: Boolean [1]
Data type description: The value is True (or could be False) [1]
Variables & Constants
Variables and constants are used to store a single item of data in a program. This can be
accessed through the identifier. Variables can be changed during program execution while
constants remain the same.
Declaring Variables and Constants
Variables are declared using a data type, a name and a value (optional)
Constants are declared using the 'const' keyword, a name and a value
In all programming languages, variable names should follow certain rules, such as:
o Starting with a letter
o Not containing spaces
o Can contain letters, numbers, _ or $
o Not using reserved words (like if, while, for etc.)
Examples of data types include integer, float, boolean, and string
Computer Science 2210 By Sajid Ali Imam
Examples in Pseudocode:
Declare a variable called 'score' with a value of 10
score ← 10
Declare a constant called 'PI' with a value of 3.14
const PI ← 3.14
Examples in Python:
Declare a variable called 'score' with a value of 10
score = 10
Declare a constant called 'PI' with a value of 3.14
PI = 3.14
Examples in Java:
Declare a variable called 'score' with a value of 10
Exam Tip
Exam questions will ask you to write pseudocode statements, rather than in a specific language
Computer Science 2210 By Sajid Ali Imam
Worked example
The variables 'name' and 'age' are used to store data in a program:
name stores the user’s name
age stores the user’s age
Write pseudocode statements to declare the variables name and age. [2]
DECLARE name : STRING [1]
DECLARE age : INTEGER [1]
Output
Output refers to the process of displaying or saving the results of a program to the user.
Pseudocode example:
Python example:
Java example:
Input
'User Input' is data or information entered by the user during program execution.
Pseudocode example:
Python example:
Java example:
Exam Tip
Remember to prompt the user for input clearly, and format the output to make it readable
and understandable
Computer Science 2210 By Sajid Ali Imam
Sequence
What is Sequence?
Pseudocode example:
Python example:
print("Hello, World!")
x=5
y = 10
z=x+y
print(z)
Java example:
System.out.println("Hello, World!");
int x = 5;
int y = 10;
int z = x + y;
System.out.println(z);
Computer Science 2210 By Sajid Ali Imam
Exam Tip
Always remember to write your instructions in the order in which you want them to be
executed
Selection
What is selection?
Selection is a programming concept that allows you to execute different sets of instructions
based on certain conditions. There are two main types of selection statements: IF statements
and CASE statements.
If Statements
IF statements allow you to execute a set of instructions if a condition is true. They have the
following syntax:
IF condition
THEN
instructions
ENDIF
Computer Science 2210 By Sajid Ali Imam
Pseudocode example:
x← 5
IF x > 0
THEN
PRINT "x is positive"
ENDIF
Python example:
x=5
if x > 0:
print("x is positive")
Java example:
int x = 5;
if (x > 0) {
System.out.println("x is positive");
}
Dim x As Integer = 5
If x > 0 Then
Console.WriteLine("x is positive")
End If
Computer Science 2210 By Sajid Ali Imam
IF ELSE Statements
If else statements are used to execute one set of statements if a condition is true and a
different set of statements if the condition is false. They have the following syntax:
IF condition
THEN
Instructions
ELSE
Instructions
ENDIF
Pseudocode example:
x←5
IF x > 0
THEN
PRINT "x is positive"
ELSE
PRINT “x is negative”
ENDIF
Python example:
x=5
if x > 0:
print("x is positive")
else:
print("x is negative")
Computer Science 2210 By Sajid Ali Imam
Java example:
int x = 5;
if (x > 0) {
System.out.println("x is positive");
} else {
System.out.println("x is negative");
}
Dim x As Integer = 5
If x > 0 Then
Console.WriteLine("x is positive")
Else
Console.WriteLine("x is negative")
End If
IF ELSE IF Statements
If else if statements are used to test multiple conditions and execute different statements for
each condition. They have the following syntax:
IF condition
THEN
Instructions
ELSE IF condition
THEN
Instructions
ELSE
Instructions
Computer Science 2210 By Sajid Ali Imam
ENDIF
Pseudocode example:
x←5
IF x > 0
THEN
PRINT "x is positive"
ELSE IF x < 0
THEN
PRINT “x is negative”
ELSE
PRINT “x is 0”
ENDIF
Python example:
x=5
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is 0")
Java example:
int x = 5;
if (x > 0) {
System.out.println("x is positive");
} else if (x < 0) {
System.out.println("x is negative");
} else {
System.out.println("x is 0");
}
Worked example
Write an algorithm using pseudocode that:
Inputs 3 numbers
Outputs the largest of the three numbers [3]
INPUT a, b, c
[1
IF a > b AND a > c THEN PRINT a
]
[1
ELSE IF b > c THEN PRINT b
]
[1
ELSE PRINT c
]
Case Statements
CASE statements allow you to execute different sets of instructions based on the value of a
variable. They have the following syntax:
CASE OF variable
value1: instructions
value2: instructions
...
OTHERWISE instructions
END CASE
Computer Science 2210 By Sajid Ali Imam
Pseudocode example:
CASE OF number
1: PRINT "Monday"
2: PRINT "Tuesday"
3: PRINT “Wednesday”
4: PRINT “Thursday”
5: PRINT “Friday”
6: PRINT “Saturday”
7: PRINT “Sunday”
OTHERWISE PRINT "Invalid number"
END CASE
Python example:
match (number)
case 1:
print "Monday";
case 2:
print "Tuesday";
case 3:
print "Wednesday";
case 4:
print "Thursday";
case 5:
print "Friday";
case 6:
print "Saturday";
case 7:
print "Sunday";
case _:
print "Invalid number";
Computer Science 2210 By Sajid Ali Imam
Java example:
switch (number) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
default:
return "Invalid number";
}
Exam Tip
Make sure to include all necessary components in the selection statement: the condition,
the statement(s) to execute when the condition is true, and any optional statements to
execute when the condition is false
Use proper indentation to make the code easier to read and understand
Be careful with the syntax of the programming language being used, as it may differ slightly
between languages
Make sure to test the selection statement with various input values to ensure that it works
as expected