Tutorial 4

🟰

Logical Operators

0984 IGCSE


While by themselves, they are not overly useful, when used to execute specific code based on a condition (if statement) or to continue looping based on the value of a certain condition, then logical operators become crucial

1

All Operators

There are 6 logical operators:

  • =: equal to
  • >: greater than
  • >=: greater than or equal to
  • <: less than
  • <=: less than or equal to
  • <>: not equal to
OUTPUT 1 = 2 OUTPUT 1 <> 2 OUTPUT "" OUTPUT 2 > 2 OUTPUT 2 >= 2 OUTPUT "" OUTPUT 2 < 2 OUTPUT 2 <= 2 OUTPUT ""

2

Other Data Types

Create a program that asks a user to enter two strings and output the result of all logical comparisons, as in the previous example. Then try with other data types like CHARs and BOOLEANs

DECLARE str1, str2 : STRING OUTPUT "Enter first string" INPUT str1 OUTPUT "Enter second string" INPUT str2 OUTPUT str1 = str2 OUTPUT str1 <> str2 OUTPUT "" OUTPUT str1 > str2 OUTPUT str1 >= str2 OUTPUT "" OUTPUT str1 < str2 OUTPUT str1 <= str2

Why do you think "abc" > "ABC"?

Hint: think of the ASCII codes: "abc" = 97, 98, 99...while "ABC" = 65, 66, 67 - so a string comparison can simply compare the ASCII code of each character, one by one