Logical operators in programming include:
|<=||less than or equal to|
|>=||more than or equal to|
|!=||not equal to (aka <>)|
Selective statements like IF statements make use of logical operators to compare two values against each other, producing a boolean result. For example, if we wrote a program to check a password was correct, we would need to compare the string that the user entered agains the string saved for the correct password. In pseudocode this would look like:
password = "teachallaboutit"
OUTPUT "Please enter your password:"
userPassword = INPUT
IF password == userPassword THEN
OUTPUT "password accepted"
OUTPUT "password incorrect"
The code below implements this pseudocode in Python.
Try adjusting the code so that the user is asked to enter the correct password repeatedly until it is correct.
You can edit and run the code directly in your browser:
Why these symbols?
The logical operators that we use are very similar to those used in maths, with the exceptions of the “or equal to” where maths uses a combined symbol of ≤ or ≥, and the not equal to symbol of ≠.
The reason for the difference in these symbols is that programming source code is all written in plain text which uses the ASCII character set. So, because ASCII does not contain the mathematical symbols alternatives had to be found.
Found this page helpful? Please consider sharing this page!
Looking for More?
Sign in to access the following extras on this page for members:
Looking for something else? Check back soon as new resources are added every week!