Comparison operators Flashcards
Comparison operators
Used to compare different items of data
==
equal to
Checks if two values are equal
Two equal signs distinguish it from assigning a value to a variable
e.g. if length == width then
!=
Not equal to
Checks of two values are not equal to each other
e.g. if length != width then
Less than
Checks if one value is less than another
<=
Less than or equal to
Checks if one value is equal or less than another
>
Greater than
Checks if one value is greater than another
> =
Greater than or equal to
Checks if one value is equal to or greater than another
Comparing strings
These operators can also be used with strings
They are compared alphabetically
Write an algorithm in pseudocode which asks the user to enter a number between 1 and 10 and then states if it is higher, lower, or equal to 5. [4]
number = input("Enter a number between 1 and 10") if number > 5 then print("Higher than 5.") elseif number < 5 then print("Lower than 5.") else print("You entered 5") endif
Write an algorithm in pseudocode which asks the user to enter a number between 1 and 10 and then states if it is higher, lower, or equal to 5. [4]
number = input("Enter a number between 1 and 10") if number > 5 then print("Higher than 5.") elseif number < 5 then print("Lower than 5.") else print("You entered 5") endif
Write an algorithm, in pseudocode, that allows a user to enter two values as value1 and value2 and which will switch the values, if necessary, so that they are in ascending order [2]
value1 = input() value2 = input() if value1 < value2 then temp = value1 value1 = value2 value2 = temp endif