Logical Operators in Programming

Click to see the rest of the programming section :

In the previous section, we looked at arithmetic operators. Another programming operator that may be familiar to you from maths topics are logical operators. These allow you to compare two values against each other and produce a boolean result (True or False).

Calculator maths

Logical operators are used in programs to make decisions in both selective and iterative statements. (For more information on these, see the programming constructs pages on selection and iteration)

Logical operators in programming include:

<less than
>more than
<=less than or equal to
>=more than or equal to
==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"
ELSE:
OUTPUT "password incorrect"

The code below implements this pseudocode in Python. click on the code & press Ctrl + Enter to run it.

Try adjusting the code so that the user is asked to enter the correct password repeatedly until it is correct.

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.

Find this page helpful? Share the love on your social media mentioning @TeachAllAboutIT and we’ll enter you in our monthly draw to win a gift voucher for any product on the site!

Share on facebook
Share on google
Share on twitter
Share on linkedin

More For Members

Lesson Plan

Coming Soon!

Presentation

Coming Soon!

Homework

Click to Download!

Students

Click To Revise!

Not a member yet? Sign Up Here

Sign Up For Membership Today

Individual Student

Individual Site License
Topic Introduction Pages
Additional Revision Resources
Revision Videos
Exam Question Walkthroughs
Discounted Group Lessons
£2.50 per Month. After your initial payment, your first payment is Free.

Whole School

Whole School Site License (teacher access)
Up to 50 student accounts (£3 per student, per annum for additional students)
Online Topic Lesson Plans
Differentiated Homework Tasks
Monthly Bundle of Downloadable Resources
Discounted Live Lessons
£12.50 per Month.
Number of courses 30
Getting Started with LESS
Improve Your CSS Workflow with SASS
Easy Zurb Foundation
GET IT NOW GET IT NOW