if...else statement in C++

In this post, we will learn about C++ if...else conditional statement which is used for decision-making purposes in programming. In a decision-making process, we have to choose one out of many available options and our decision is based upon certain conditions. 


In programming, let's say you have two blocks of code and you want to execute the first block of code only if a certain condition is true else you will execute the second block of code. In these kinds of cases, you require a condition statement like if...else


There are three kinds of if...else statement available in C++.

  • if statement.
  • if...else statement.
  • if...else if...else statement.


if statement in C++.

The if statement evaluates the given boolean condition and if the condition returns true then the code present inside the body ({ }) of if gets executed and if the condition returns false then the code present inside the body of if gets skipped and the program continues to execute the remaining statements. 

Syntax of if statement:
if(condition){
  //statement(s) to execute if condition is true
}
if statement flowchart
Flowchart of if statement

C++ Example of if statement.
//C++ if statement Example 
#include<iostream>
using namespace std;

int main(){
    
    int num;

    cout<<"Enter number:";
    cin>>num;

    if(num % 2 == 0){
        cout<<"The Entered number is an even number.";
    }
    cout<<"\nThe value of num: "<<num;

    return 0;
}
Output 1:
Enter number:24
The entered number is an even number.
The value of num: 24
The user entered 24 which is an even number and it is satisfying our if condition and returns true so the statement inside the if body gets executed. 

Output 2:
Enter number:21
The value of num: 21
The user entered 21 which is an odd number and it is not satisfying our if condition and returns false so the statement inside the if body will get not executed.

if...else statement in C++.

In if...else statement the given boolean condition is evaluated and if it return true then the code present inside the body of if get executed and if the condition return false then the code present inside the body of else get executed.

Syntax of if...else statement: 
if(condition){
  //statement(s) to execute if condition is true
}
else{
  //statement(s) to execute if condition is false
}
if else statement flowchart
Flowchart of if...else statement

C++ Example of if...else statement.
//C++ if..else statement Example 
#include<iostream>
using namespace std;

int main(){
    
    int num;

    cout<<"Enter number:";
    cin>>num;

    if(num % 2 == 0){
        cout<<"The Entered number is even number.";
    }
    else{
        cout<<"The Entered number is an odd number.";
    }
    cout<<"\nThe value of num: "<<num;

    return 0;
}
Output 1:
Enter number:33
The entered number is an odd number.
The value of num: 33
The user have entered 33 which is an odd number so condition return false and code inside else block get executed.

Output 2:
Enter number:36
The entered number is even number.
The value of num: 36
The user has entered 36 which is an even number so condition return true and code inside if block get executed and skipped the else part.

if...else if...else statement in C++.

The if...else if...else statement is used when we have more than two block of code to choose from. In this case, we have multiple conditions to evaluate and whichever condition return true that block of code get executed and if non of the condition is true then the last else block get executed. 

Syntax of if...else if...else statement: 
if(condition 1){
  //statement(s) to execute if condition 1 is true
}
else if(condition 2){
  //statement(s) to execute if condition 2 is true
}
else if(condition 3){
  //statement(s) to execute if condition 3 is true
}
else{
  //statement(s) to execute if all conditions false
}
if else if else flowchart
Flowchart of if...else if...else statement

C++ Example of if...else if...else statement.
//C++ if..else if else statement Example 
#include<iostream>
using namespace std;

int main(){
    
    int mark;

    cout<<"Enter the mark of the student: ";
    cin>>mark;

    if(mark >= 90){
        cout<<"Student got Grade A";
    }
    else if(mark >= 70 && mark < 90){
        cout<<"Student got Grade B";
    }
    else if(mark > 50 && mark < 70){
        cout<<"Student got Grade C";
    }
    else{
        cout<<"Student got Grade D";
    }

    cout<<"\nTotal mark of the student: "<<mark;

    return 0;
}
Output 1:
Enter the mark of the student: 70
Student got Grade B
Total mark of the student: 70
When the user enter a value 70 then our second condition got satisfied so compiler execute second block of code and print "Student got Grade B".

Output 2:
Enter the mark of the student: 40
Student got Grade D
Total mark of the student: 40
When the user enter a value 40 then none of the condition got satisfied so compiler execute else block of code and print "Student got Grade D".
Note: There can be more than one else if statement in our code but only one if and else statement is allowed. (alert-success)


Nested if...else statement.

Nested if...else is a condition in which we use if..else statement inside another if...else statement. 

Syntax of Nested if...else statement: 
if(condition 1){
  //statement(s) to execute if condition 1 is true
  if(condition 1A){
    //statement(s) to execute if condition 1A is true
  }
  else{
    //statement(s) to execute if condition 1A is false
  }
}
else if(condition 2){
  //statement(s) to execute if condition 2 is true
}  
else{
  //statement(s) to execute if all conditions false
}

C++ Example of if...else if...else statement.
//C++ Nested if..else if else statement Example 
#include<iostream>
using namespace std;

int main(){

    int num;

    cout<<"Enter a number: ";
    cin>>num;

    if(num % 2 == 0){
        if(num % 5 == 0){
            cout<<"Number is Even and Divisible by 5";
        }
        else{
            cout<<"Number is Even and not Divisible by 5";
        }
    }
    else{
        if(num % 5 == 0){
            cout<<"Number is Odd and Divisible by 5";
        }
        else{
            cout<<"Number is Odd and not Divisible by 5";
        }
    }

    return 0;
}
Output:
Enter a number: 25
Number is Odd and Divisible by 5

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS