C++ Tutorial

How does an if expression help in statement execution ?

In C/C++, the if expression of the if statement plays a great role in statement execution. We all know the basic syntax of the if statement:

if(expression){
    statement;
}

The if expression of the if statement can be any valid combination of constants, variables, relational operators or logical operators. If the if expression is true, the statement (or block of statements) of if will be executed. But if the if expression is false, the statement (or block of statements) will not be executed.

So, the if expression is the determining factor to execute an if statement.

A Brief Explanation

The if expression helps to make a comparison or a logical decision. The expressions are  formed of constants and variables which are connected with relational operators or logical operators or both. At first, a comparison or a logical decision is made between the constants or variables of an expression. If that particular expression is true, then the if statement is executed. But if that particular expression is false, then the if statement is not executed.

In C/C++, an expression becomes true if the outcome of that expression is 1. The expression becomes false if the outcome is 0. Again, relational operators and logical operators give the value 1 for true and 0 for false. So, it mainly depends on the calculation of the if expression formed with these operators.

Now, let’s observe the following code:

#include<iostream>
using namespace std;

int main()
{
   int a=5, b=7;
   if (a < b) {
     cout << "1. Penguins are found in Antarctica" << endl;//Statement 1
   }
   if ((a + 5) <= b) {
     cout << "2. Polar bears roam within the Arctic Circle" << endl;
     //Statement 2
   }
   if ((a + 2) >= b) {
     cout << "3. 23 is a prime number" << endl;//Statement 3
   }
   if (a == b) {
     cout << "4. The factorial of 5 is 120" << endl;//Statement 4
   }
   if (((a % 2) != 0) && (a + b == 12)) {
     cout << "5. The area of a square with length 5 is " << (5 * 5) 
              << endl;//Statement 5
   }
   if ((a > b) || (a + b == 10)) {
     cout << "6. The summation of 25+36 is " << (25 + 36) << endl;
     //Statement 6
   }
   return 0;
}

In this code, the if expressions for the statements 1, 3 and 5 are true. So these statements will be executed. But the if expressions for the statements 2, 4 and 6 are false. So these statements will not be executed.

Justifying the truth values of the expressions:

To justify that the if expressions of the statements 1, 3 and 5 are true and the if expressions of the statements 2, 4 and 6 are false, we will observe the following code:

#include<iostream>
using namespace std;

int main()
{
   int a = 5, b = 7;//Variables that were used in the previous code

   cout << "The Truth values of the if expressions used in the previous 
            code are shown below:" << endl;

     //if expression for statement 1
   cout << "1. Result: " << (a < b) << endl;

     //if expression for statement 2
   cout << "2. Result: " << ((a + 5) <= b) << endl;

     //if expression for statement 3
   cout << "3. Result: " << ((a + 2) >= b) << endl;

     //if expression for statement 4
   cout << "4. Result: " << (a == b) << endl;

     //if expression for statement 5
   cout << "5. Result: " << (((a % 2) != 0) && (a + b == 12)) << endl;

     //if expression for statement 6
   cout << "6. Result: " << ((a > b) || (a + b == 10)) << endl;

   return 0;
}

In this code, the expressions 1, 3 and 5 are termed as true because the result of these expressions is 1. And the expressions 2, 4 and 6 are termed as false because the result of these expressions is 0.

Therefore, the final truth value of an if expression determines the execution of an if statement.

An Interesting fact:

The expressions formed of the relational operators and logical operators give only two results which are either 0 or 1. But what if, the expression of the if statement is formed like a normal algebraic expression where there is no relational operators or logical operators !

In that case, the result of that expression may be 0 or any other non-zero value. If the result of that expression is any non-zero value, it will be termed as true. But if the result is 0, then it will be termed as false. The expression will be termed as true even if we use a single non-zero value as the expression. If we use 0 as the expression, it will be termed as false.

The following code will demonstrate this fact:

#include<iostream>
using namespace std;

int main()
{
   int a = 5, b = 7;

   if (5) { cout << "Statement 1 executed" << endl; }

   if (107%a) { cout << "Statement 2 executed" << endl; }

   if ((b*2)-(a+27)) { cout << "Statement 3 executed" << endl; }

   if ((135/a)+(56-b)) { cout << "Statement 4 executed" << endl; }

   if (0) { cout << "Statement 5 not executed" << endl; }

   if ((56/b)-(a+3)) { cout << "Statement 6 not executed" << endl; }

   return 0;
}

In this code, the final results of the if expressions for the statements 1, 2, 3 and 4 are non-zero values. So these statements are executed. But the final results of the if expressions for the statements 5 and 6 is 0. So these statements are not executed.

Thus we came to know that in C/C++, it is true when the result of an expression is any non-zero value and false when the result is 0. But for the expressions consisting of relational operators and logical operators, it is true when the result of that expression is 1 and false when the result is 0.

In this way, we can write different types of programs in C/C++ by applying these concepts of the if expression. We will be able to construct the conditional statements of our programs by proper implementation of the if expression.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.