There come circumstances, all things considered when we want to make some decisions and based on these choices, we conclude what should we do next. Comparative situations emerge in programming additionally where we really want to make some decisions and based on these decisions we will execute the following block of code. For example, in C++  if x happens then execute y else execute z. There can likewise be different circumstances like in C/c++ if x happens then execute p, else if condition y happens execute q, else execute r. This state of C/ C++  else-if is one of the many ways of importing different circumstances . 
 
Decision making statement

Decision-making statements in programming languages choose the direction of the flow of program execution. Decision-making statements accessible in C or C++ are: 
 

  1. if statement
  2. if..else statements
  3. nested if statements
  4. if-else-if ladder
  5. switch statements
  6. Jump Statements: 
  7. break
  8. continue
  9. goto
  10. return

Here we will discuss about if-else statements.


if statement in C/C++ language

if statement is the most simple decision-making assertion. It is utilized to conclude whether a specific statement or block of articulations will be executed or not i.e in the event that a specific condition is true then a block of statement is executed in any case not. 
Syntax


Here, the condition after assessment will be either true or false. C/C++ if statement accepts boolean worths – assuming the value is true then it will execute the block of assertion below it in any case not. In the event thatwe do not give the curly or wavy braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the first promptly beneath statement to be inside its block. 
If Statement Example

#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


As the condition present in the if statement is false. Thus, the block beneath the if statement isn't executed.


if- else statement in C/C++ programming

The if assertion alone lets us know that if a condition is true it will execute a block of statements and in the event that the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the C/C++ else statement. We can utilize the else statement with if assertion to execute a block of code when the condition is false. 
Syntax
 



If else statement example:

#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


The block of code accompanying the else assertion is executed as the condition present in the if statement is false.
 

nested-if else statement in C/C++ language

A nested if in C/C++  is an if statement that is the objective of another if statement. Nested if statements mean an if assertion inside another if statement. Indeed, both C and C++ permit us to nested if statements inside if statements, i.e, we can put an if statement inside another if articulation. 
Syntax: 


Example: 

/* C++ program to find if an 
integer is positive, negative 
or zero */
//using nested if statements

#include <iostream>
using namespace std;

int main() {

  int num;
    
  cout <<"Enter an integer: ";  
   cin >> num;    

  // outer if condition
  if (num != 0) {
        
    // inner if condition
    if (num > 0) 
    {
      cout <<"The number is positive."
      <<endl;
    }
    // inner else condition
    else {
      cout <<"The number is negative."
      <<endl;
    }  
  }
  // outer else condition
  else {
    cout <<"The number is 0 and 
    it is neither positive nor 
    negative." << endl;
  }

  cout <<"This line is always printed.
  "<<endl;

  return 0;
}


Output: 

Enter an integer: 35
The number is positive.
This line is always printed.


if-else-if ladder statement in C/C++ language

Here, a user can decide among different choices. The C/C++ if statements are executed from the top down. When one of the conditions controlling the if is true, the statement related with that if is executed, and the remainder of the C else-if ladder is avoided. If none of the conditions are true, then the last else statement will be executed. 
Syntax: 


Example:

#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


Previous                                                                                             Next