If/Else Statements

There are cases where we need to execute a block of statements only when a particular condition is met. This is called decision making, since we are executing a certain code after making a decision in the program logic. To make decisions through code, we use a concept called if and else statements

Syntax

They use the following syntax:

if(condition){
  Statement(s);
} 
else {
  Other_statement(s);
}

Here, if the condition is true, all of the code within the parenthesis(Statement) will run. Otherwise, the code within Other_statement will be executed.
Our if/else statements can be represented via the following flow diagram:

Sample Usage

To use an if/else statement in C++, write the following code:

#include <iostream>;
using namespace std;
int main(){
  int num=40;
    //is num greater than 40?
  if( num < 100 ){
     /* This statement will only execute,
      * if the above condition is true
      */ 
     cout<<"number is less than 100";
  }
  //otherwise, run this:
  else{
     cout<<"number is greater than 100"<<endl;
  }
  return 0;
}

Nested Ifs

When there is an if statement inside another if statement then it is called the nested if statement.
It follows the following syntax:

if(firstCondition) { //check if firstCondition is true
   Statement1(s);

   if(secondCondition) { //if firstCondition is true, now check if secondCondition is true.
      Statement2(s);
   }
}

Here, Statement1 would execute if the firstCondition is true. Furthermore, Statement2 would only execute if both the conditions(firstCondition and secondCondition ) are true.
Here’s an example of the Nested Ifs

#include <iostream>;
using namespace std;
int main(){
   int num=90;
   if( num < 100 ){ //check if num is lesser than 100
      cout<<"number is less than 100"<<endl; //now execute this code.
      if(num> 50){ //furthermore, now check if num is larger than 50.
         cout<<"number is greater than 50 AND Less than 100<<endl; //if so execute this code.
      } 
   }
   return 0;
}

This will be the output:

If/Else-If statements

if/else-if statements are used when we need to check multiple conditions. In this control structure, we have only one “if” and one “else”, however we can also have multiple “else if” blocks.
It uses this syntax:if(condition_1) {

   /*if firstCondition is true execute this*/
if(firstCondition) {
   statement(s);
}
else if(secondCondition) {
   /* execute this if firstCondition is not met but
    * secondCondition is met
    */
   statement(s);
}
else if(thirdCondition) {
   /* execute this if firstCondition & secondCondition are
    * not met but thirdCondition is true
    */
   statement(s);
}
else {
   /* if none of these conditions is true
    * then these statements will get executed
    */
   statement(s);
}

One thing to remember is that in if/else-if statements, as soon as the condition is met, the corresponding set of statements get executed, and rest gets ignored. If none of the conditions are met, then the statements inside “else” gets executed.
Here is a brief example of if/else-if statements in action:

#include <iostream>;
using namespace std;
int main(){
   int num;
   cout<<"Enter an integer number between 1 and 99999:" <<endl;
   cin>>num; //takes an input from the user and stores it in the 'num' variable.
  //is num less than 100 AND greater than 1?
   if(num <100 &&  num>=1) {
      cout<<"Its a two digit number"<<endl;
   }
  //if not, then check whether num is less than 1000 but larger than or equals to 100.
   else if(num <1000 && num >=100) {
      cout<<"Its a three digit number"<<endl;
   }
  //if the above condition was false, check if this condition is met.
   else if(num < 10000 && num>=1000) {
      cout<<"Its a four digit number"<<endl;
   }
  //if the above conditions are false, now check this statements
   else if(num < 100000 && num>10000) {
      cout<<"Its a five digit number"<<endl;
   }
//if all of the above conditions are not met, execute this code:
   else {
      cout<<"number is not between 1 & 99999"<<endl;
   }
   return 0;
}

This will be the result:

Conclusion

In this article, you learned about if and else statements in the C++ ecosystem. They are crucial not only in C++, but also in other programming languages as well. If you encountered any difficulty, I advise you to deconstruct the code so that you can fully grasp its inner workings.
Thank you so much for making it to the end! Happy coding!

Scroll to Top