If else in C plus plus

If else in C plus plus programming, if statement is used to test the condition. And if the condition is false then the else block is used. There are various types of if statements in C++.

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

C++ IF Statement

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

 
  1. if(condition){    
  2. //code to be executed    
  3. }  

 


C++ If Example

 
  1. #include <iostream>  
  2. using namespace std;  
  3.    
  4. int main () {  
  5.    int num = 10;    
  6.             if (num % 2 == 0)    
  7.             {    
  8.                 cout<<“It is even number”;    
  9.             }   
  10.    return 0;  
  11. }  

Output: 👇👇

It is even number

 

C++ IF-else Statement

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

 
  1. if(condition)    {    
  2. //code if condition is true    
  3. }
  4. else  {    
  5. //code if condition is false    
  6. }    

 


C++ If-else Example

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main () {  
  4.    int num = 11;    
  5.             if (num % 2 == 0)    
  6.             {    
  7.                 cout<<“It is even number”;    
  8.             }   
  9.             else  
  10.             {    
  11.                 cout<<“It is odd number”;    
  12.             }  
  13.    return 0;  
  14. }  

Output:

It is odd number

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

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main () {  
  4.     int num;  
  5.     cout<<“Enter a Number: “;  
  6.     cin>>num;  
  7.             if (num % 2 == 0)    
  8.             {    
  9.                 cout<<“It is even number”<<endl;    
  10.             }   
  11.             else  
  12.             {    
  13.                 cout<<“It is odd number”<<endl;    
  14.             }  
  15.    return 0;  
  16. }  

Output:

Enter a number:19
It is odd number

Output:

Enter a number:26
It is even number

C++ IF-else-if ladder Statement

The C++ if-else-if ladder statement executes one condition from multiple statements.

 
  1. if(condition1){    
  2. //code to be executed if condition1 is true    
  3. }else if(condition2){    
  4. //code to be executed if condition2 is true    
  5. }    
  6. else if(condition3){    
  7. //code to be executed if condition3 is true    
  8. }    
  9. …    
  10. else{    
  11. //code to be executed if all the conditions are false    
  12. }    

 


C++ If else-if Example

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main () {  
  4.        int num;  
  5.        cout<<“Enter a number to check grade:”;    
  6.        cin>>num;  
  7.             if (num <0 || num >100)    
  8.             {    
  9.                 cout<<“wrong number”;    
  10.             }    
  11.             else if(num >= 0 && num < 50){    
  12.                 cout<<“Fail”;    
  13.             }    
  14.             else if (num >= 50 && num < 60)    
  15.             {    
  16.                 cout<<“D Grade”;    
  17.             }    
  18.             else if (num >= 60 && num < 70)    
  19.             {    
  20.                 cout<<“C Grade”;    
  21.             }    
  22.             else if (num >= 70 && num < 80)    
  23.             {    
  24.                 cout<<“B Grade”;    
  25.             }    
  26.             else if (num >= 80 && num < 90)    
  27.             {    
  28.                 cout<<“A Grade”;    
  29.             }    
  30.             else if (num >= 90 && num <= 100)    
  31.             {    
  32.                 cout<<“A+ Grade”;  
  33.             }    
  34.     }    

Output:

Enter a number to check grade:65
C Grade

Output:

Enter a number to check grade:-3
wrong number

 

Leave a Reply