C++ if-else

The if statement will only execute if the expression inside the parentheses is evaluated to true. In C++, this does not have to be a Boolean expression. It can be any expression that evaluates to a number, in which case zero is false and all other numbers are true. There are various types of if statements in C++.

  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder

if statement

The C++ if statement tests the condition inside the parentheses. It is executed if condition is true.

Syntax
if(condition){  
//code to be executed  
}
Cpp If else 1
Example
snippet
#include <iostream>
using namespace std;
 
int main () {
   int num = 10;  
            if (num % 2 == 0)  
            {  
                cout<<"It is even number";  
            } 
   return 0;
}
Output
It is even number

if-else statement

The C++ if-else statement also tests the condition. It executes if block when the condition is true otherwise else block is executed.

Syntax
if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}
Cpp If else 2
Example #1
snippet
#include <iostream>
using namespace std;
int main () {
   int num = 11;  
            if (num % 2 == 0)  
            {  
                cout<<"It is even number";  
            } 
            else
            {  
                cout<<"It is odd number";  
            }
   return 0;
}
Output
It is odd number
Example #2

C++ If-else Example: with input from user

snippet
#include 
using namespace std;
int main () {
    int num;
    cout<<"Enter a Number: ";
    cin>>num;
            if (num % 2 == 0)  
            {  
                cout<<"It is even number"<<endl;  
            } 
            else
            {  
                cout<<"It is odd number"<<endl;  
            }
   return 0;
}
Output
Enter a number:11 It is odd number
Output
Enter a number:12 It is even number

if-else-if ladder statement

The C++ if-else-if ladder statement executes one condition from multiple statements. The if statement can be extended by any number of else if clauses.

Syntax
if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}  
else if(condition3){  
//code to be executed if condition3 is true  
}  
...  
else{  
//code to be executed if all the conditions are false  
}
Cpp If else 3
Example #1
snippet
#include <iostream>
using namespace std;
int main () {
       int num;
       cout<<"Enter a number to check grade:";  
       cin>>num;
            if (num <0 || num >100)  
            {  
                cout<<"wrong number";  
            }  
            else if(num >= 0 && num < 50){  
                cout<<"Fail";  
            }  
            else if (num >= 50 && num < 60)  
            {  
                cout<<"D Grade";  
            }  
            else if (num >= 60 && num < 70)  
            {  
                cout<<"C Grade";  
            }  
            else if (num >= 70 && num < 80)  
            {  
                cout<<"B Grade";  
            }  
            else if (num >= 80 && num < 90)  
            {  
                cout<<"A Grade";  
            }  
            else if (num >= 90 && num <= 100)  
            {  
                cout<<"A+ Grade";
            }  
    }
Output
Enter a number to check grade:66 C Grade
Output
Enter a number to check grade:-2 wrong number
Note
The curly brackets can be left out if only a single statement needs to be executed conditionally. However, it is considered good practice to always include them since they improve readability.
Example #2

Below is the example without curly brackets.

snippet
if (x < 1)
cout << x << " < 1";
else if (x > 1)
cout << x << " > 1";
else
cout << x << " == 1";
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +