Logical operators in C++ can instruct the program to perform mathematical or logical operations. With the help of logical operators, we can use them to operate variables we have defined.

Arithmetic Operators

What is the % operator?

This is called the modulus which is represented by a percentage symbol. It stands for modulus division which finds the remainder of a calculated division in a program. Here’s an example.


#include <iostream>
#include <math.h>

using namespace std;

int main()
{
// Result variable of the modulus division
int x;

x = 11 % 3;

// Display the remainder value from the modulus division
cout << x;

return 0;
}

Result of the modulus division of 11 divided by 3

 

What is increment and decrement?

The operators ++ and — are the increment and decrement operators. The most simple way to explain them is they are shortcuts for addition and subtraction in programming.

For example

x = x + 1;

is actually the same as x++

and x = x – 1;

is the same as –x;

Does it matter where I put the ++ and — when I am implementing increment or decrement?

++x is known as a pre increment

x++ is known as a post increment

In this simple example, there is no difference where you place the ++ or — in your code. But if you are dealing with a large expression, it can become a important difference. Remember to always test your code when you are implementing increment or decrement operations as the complier cannot pick up logical errors.


#include <iostream>
#include <math.h>

using namespace std;

int main()
{
// Variables for pre and post increment example
int x;
int y;

x = 5;
y = 7;

// Pre increment value of x
++x;

// Post increment value y
y++;

cout << "x value pre increment value" << "\n" << x << "\n";
cout << "y value post increment value" << "\n" << y << "\n";

return 0;
}

In this sample program of pre and post increment, the values of x and y have been incremented by one with the x value starting with 5 and y value starting with 7. With the results shown below, it is showing the end result operation of pre and post increment.

 

Logical operators

In C++, logical operators deal with true and false statements acting together. In logic, this can be AND, OR, NOT. The symbols used for these operations are in the following table.


#include <iostream>

using namespace std;

int main()
{
// Values used for logical operators
bool value1;
bool value2;

// Set values to true and false
value1 = true;
value2 = false;

// AND Operation
if (value1 && value2)
// This will not be displayed because this logical operator
// Will not work
cout << "This won't work\n";

// NOT AND Operation
if(!(value1 && value2))
cout << "!(value1 && value2) works\n";

// OR Operation
if (value1 || value2)
cout << "value1 || value2 is true\n";

return 0;
}

Let’s look at what is going on with the code. This may be tricky to understand at first but let’s go through line by line at what’s happening. Don’t worry about the “if” keyword just yet. They will be taught in the chapter “program control statements”.

Line 11 and 12: We are just declaring two boolean values to be used for the logical operations. We cannot use int or double because they deal with numbers.

Line 16 and 17: The two boolean values are now being initialised with a value. Value1 is being set to true and Value2 is set to false.

Lines 19 – 23: The first logical operation being used is an AND operation. As you can see in the simulation, cout << “This won’t work\n”; is not being displayed in the simulation. The reason why it is not showing is because value1 && value 2 are not both true or false. One is different from the other, thus line 23 will not be outputted in the simulation.

AND Operation diagram

Lines 25 – 27: This is very similar to lines 19 – 23 but if you look closely, there is a “!” before the expression (value1 && value2). “!” represents NOT, so since value1 and value2 are the not the same, it will display in the simulation “!(value1 && value2) works”.

Lines 29 – 31: OR operation being implemented here. This is the opposite of the AND operation. If value1 and value2 have different values so it will display “value1 || value2 is true” in the simulation.

OR Operation diagram

 

Relational operators

These operators deal with logic operations such as less than, equal or greater than. These are not as difficult as logical operators and can easily be mastered.

In the code example, the value of i is 5 and value of j is set to 6. In the code, I am programming the computer to question the following

– Is i less than j

-Is i greater than l

-Is i not equal to j

-Is i equal to j

-Is i greater than or equal to j

-Is i less than or equal to j


#include <iostream>

using namespace std;

int main()
{
// Integer variables
int i, j;

// Set variables
i = 5;
j = 6;

// Relational operators
if (i < j)
cout << "i is less than j\n";
if(i > j)
cout << "i is greater than j\n";
if(i != j)
cout << "i is not equal to j\n";
if(i == j)
cout << "i equal to j\n";
if (i >= j)
cout << "This won't work\n";
if(i <= j)
cout << "i is less than or equal to j\n";

return 0;
}

The lines which are true would be displayed in the output simulation.

Share this post

FAQ's

What are logical operators in c++?

Logical operators in C++ can instruct the program to perform mathematical or logical operations.

What are Arithmetic operators?

Addition, Subtraction, Multiplication, Division, Increment, Decrement

What are Relational operators?

< less than, <= equal than or greater, > greater than, >= equal or greater, == equal, != not equal

How do we use logical operators in c++?

To use logical operators in C++, you will need to determine whether your application is required to do a arithmetic or relational operation.