C++

C++ If-Else Statement – Details From [A-Z]

When a certain condition is met or not, we might need to only execute a block of statements in that particular case. As we are executing a specific code after making a decision in the program logic, this is referred to as decision making. The four different forms of control statements (or control structures) available for C++ decision-making are as follows:

a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

If statement in C++

If a condition is met, a statement or group of statements will follow as illustrated below:

if(condition){
  Statement(s);
}

Only when the provided condition is true are the statements enclosed in the if parenthesis (sometimes referred to as the if body) performed. The assertions within the body of the if statement are entirely disregarded if the condition is untrue.

Example of if statement

#include <iostream>
using namespace std;
int main(){
  int num=70;
  if( num < 100 ){
     /* This cout statement will only execute,
      * if the above condition is true
      */ 
     cout<<"number is less than 100";
  }

  if(num > 100){
     /* This cout statement will only execute,
      * if the above condition is true
      */ 
     cout<<"number is greater than 100";
  }
  return 0;
}

Output:

number is less than 100

C++’s nested if statement

The term “nested if statement” refers to an if statement that is included within another if statement.
The nested if structure is as follows:

if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}

If condition 1 is true, statement 1 would be carried out. Statement 2 would only be put into action if conditions 1 and 2 were met.

Nested if statement illustration

#include <iostream>
using namespace std;
int main(){
   int num=90;
   /* Nested if statement. An if statement
    * inside another if body
    */
   if( num < 100 ){
      cout<<"number is less than 100"<<endl;
      if(num > 50){
         cout<<"number is greater than 50";
      } 
   }
   return 0;
}

Output:

number is less than 100
number is greater than 50

C++’s if/else expression

When a condition is true, you could want to run a block of code; if the condition is false, you might want to run another block of code. The if-else statement in C++ can be used to accomplish this.

An if-else sentence appears like follows:

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

If the condition is true, the statements inside “if” will be carried out, and if it is false, the statements inside “otherwise” will be carried out.

Example of an if-else clause

#include <iostream>
using namespace std;
int main(){
   int num=66;
   if( num < 50 ){
      //This would run if above condition is true
      cout<<"num is less than 50";
   }
   else {
      //This would run if above condition is false
      cout<<"num is greater than or equal 50";
   }
   return 0;
}

Output:

num is greater than or equal 50

if-else-if C++ statement

When we need to check multiple conditions, we utilize the if-else-if statement. There is only one “if” and one “else” in this control structure, however there may be more than one “else if” block. It appears as follows:

if(condition_1) {
   /*if condition_1 is true execute this*/
   statement(s);
}
else if(condition_2) {
   /* execute this if condition_1 is not met and
    * condition_2 is met
    */
   statement(s);
}
else if(condition_3) {
   /* execute this if condition_1 & condition_2 are
    * not met and condition_3 is met
    */
   statement(s);
}
.
.
.
else {
   /* if none of the condition is true
    * then these statements gets executed
    */
   statement(s);
}

The most crucial thing to keep in mind is that in an if-else-if statement, the corresponding set of statements is always performed and the remainder is disregarded. The statements contained in “otherwise” are carried out if none of the conditions are satisfied.

A good if-else-if example

#include <iostream>
using namespace std;
int main(){
   int num;
   cout<<"Enter an integer number between 1 & 99999: ";
   cin>>num;
   if(num <100 && num>=1) {
      cout<<"Its a two digit number";
   }
   else if(num <1000 && num>=100) {
      cout<<"Its a three digit number";
   }
   else if(num <10000 && num>=1000) {
      cout<<"Its a four digit number";
   }
   else if(num <100000 && num>=10000) {
      cout<<"Its a five digit number";
   }
   else {
      cout<<"number is not between 1 & 99999";
   }
   return 0;
}

Output:

Enter an integer number between 1 & 99999: 8976
Its a four digit number

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button