Search Your Topic

config
Thursday, 08 June 2017 08:19

C++ if-else

Written by
Rate this item
(0 votes)

In C++ programming, if statement is used to test the condition. 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. }  

Cpp If else 1


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:/p>

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. }else{    
  4. //code if condition is false    
  5. }    

Cpp If else 2


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:11
It is odd number

Output:

Enter a number:12
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. }    

Cpp If else 3


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:66
C Grade

Output:

Enter a number to check grade:-2
wrong number

 

 

Read 114 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ Basic Input/Output C++ switch »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.