if-else statement in C++ | Simple Explanation

Hello Folks, Today, we’ll be going to discuss the if-else statement in C++. In the previous tutorial, we’ve discussed the operators in C++. This tutorial will be about decision-making, an essential and important concept in programming.

In programming, oftentimes, we want to execute a particular code only when a certain condition is met. This is called decision making, in which some sort of decision is involved. Either this or that.

For decision making, we have 4 types of control structures (or statements):

  1. If statement
  2. Nested if statement
  3. If-else statement
  4. If else-if else statement

Let’s study all of them one-by-one.

If statement

Imagine a situation if want to execute a particular piece of code, only when a certain condition is met. Or we can also say that we want to execute a particular piece of code only ‘if’ a condition becomes true.

Here, 'if' statement comes into the picture.

if(condition){

    // code to be executed

}

Now, let’s see the control diagram to understand the 'if‘ statement in detail.

Flow Diagram

flow chart of if-statement
  • Save
Flow Diagram of if-statement

In the above code, the code inside the 'if‘ statement executes only when the condition becomes true.

Related: Do you want to learn the Basics of C++?

Nested if statment

Now, let’s come to nested if statement. When there is an if statement inside another if statement, then it is called a nested-if statement.

Below is the structure of the nested if statement.

if(condition_1){
    
    statement1(s);

    if(condition_2){
        
        statement2(s);

    }
}

In the above nested-if statement, one should note that statement1 would execute only if condition1 becomes true. Whereas, statement2 would execute only if condition2 becomes true.

Related: Here’s the list of the 6 best IDE for C++ programming

If-else statement

Till now, we have studied only ‘if‘ statements. ‘if‘ statement is used if the condition is true. Similarly, 'else‘ statement is used when we want to execute a code only if the condition is false.

Below is the structure of the if-else statement.

if(condition){

    statement1(s);

}
else{

    statement2(s);

}

Here, statement1 executes only when the condition is true. Statement2 is executed only when the condition is false.

Related: Do you know, Sophis Robot is going for mass production. Just check out this link

If-else-if statement in C++

If-else-if statement is used when we have multiple conditions.  In this control statement, we have multiple ‘if‘, ‘else‘ & we have an additional ‘else if‘ block.

The below structure will make it clear.

if(condition_1){

    /* If condition_1 is true execute this */
    statement(s);

}
else if(condition_2){

    /* execute this if condition_1 is not met
    *  condition_2 is met
    */
    statement(s);

}
else if(condition_3){

    /* execute this if condition_1 and condition_2 is not met and
    *  condition_3 is met
    */
    statement(s);

}
.
.
.
else{

    /* if none of the condition is true
    *  then these statements gets executed
    */
    statement(s);

}

One can easily understand the above code when read carefully. The statement in ‘else if‘ executes only if the conditions of the ‘if‘ & ‘else if‘ is false. If the condition inside the ‘if‘ or ‘else if‘ is true, then the program doesn’t check in the below, ‘if‘, ‘else if‘ and ‘else‘.

                                                                                                                                In the above scenario, if the above code of none of the conditions inside ‘if‘ & ‘else if‘ is true, then the statement inside the ‘else‘ works.

This is all about the control statements in C++. If you have any doubts or suggestions, do let me know in the comment section. If you enjoy reading this article and find this informative, do share it with your friends.

In the next tutorial, we’ll discuss the switch statement in C++. Till then, stay tuned.

Share via
Copy link
Powered by Social Snap