else if Statement in C++ Programming


The else if condition is checked only if all the conditions before it (in previous else if constructs, and the parent if constructs) have been tested to false. In this example, the else if condition will only be checked if i is greater than or equal to 2. If its result is true, its block is run, and any else if and else constructs after it will be skipped. If none of the if and else if conditions have been tested to true, the else block at the end will be run.

 Syntax :
   if ( condition 1 )
   {
       // block of statement to be executed if condition is true ;
   }
   else if ( condition 2 )
   {
       // block of statement to be executed if the condition1 is false condition2 is true ;
   }
   else
   {
       block of statement to be executed if the condition1 is false condition2 is False ;
   }

The program is written in C++ language and it determines the grade of steel based on its hardness, tensile strength, and carbon content. Here's how the program works:

  • It first declares three variables 'h', 't', and 'c' to store the input values of hardness, tensile strength, and carbon content, respectively.
  • The user is then prompted to enter the values of hardness, tensile strength, and carbon content, which are read by the 'cin' statement.
  • The program checks the input values against a series of conditions using logical AND ('&&') and OR ('||') operators to determine the grade of steel.
  • The program first checks if the hardness is greater than 50, carbon content is less than 0.7, and tensile strength is greater than 5600. If all conditions are met, the program prints "Steel Grade : 10".
  • If the above condition is not met, the program checks if the hardness is greater than 50 and carbon content is less than 0.7. If both conditions are met, the program prints "Steel Grade : 9".
  • If the above two conditions are not met, the program checks if the carbon content is less than 0.7 and tensile strength is greater than 5600. If both conditions are met, the program prints "Steel Grade : 8".
  • If the above three conditions are not met, the program checks if the hardness is greater than 50 and tensile strength is greater than 5600. If both conditions are met, the program prints "Steel Grade : 7".
  • If the above four conditions are not met, the program checks if any one of the conditions (hardness greater than 50, carbon content less than 0.7, or tensile strength greater than 5600) is met. If any one condition is met, the program prints "Steel Grade : 6".
  • If none of the above conditions are met, the program prints "Steel Grade : 5".
  • Finally, the program returns 0, indicating successful execution.

Source Code

/*
Else If Ladder in C++ :
 
A certain grade of steel is graded according to the following conditions:
 
1. Hardness must be greater than 50.
2. Carbon content must be less than 0.7
3. Tensile strength must be greater than 5600
 
The grades are as follows:
 
Grade is 10, if all three conditions are met.
Grade is 9, if conditions 1 and 2 are met.
Grade is 8, if conditions 2 and 3 are met.
Grade is 7, if conditions 1 and 3 are met.
Grade is 6, if only one condition is met.
Grade is 5, if none of the conditions are met.
*/
#include<iostream>
using namespace std;
 
int main()
{
    int h,t;
    float c;
    cout<<"Enter The Value of Hardness,Tensile Strength and Carbon :"<<endl;
    cin>>h>>t>>c;
    if(h>50 && c<0.7 && t>5600)
    {
        cout<<"Steel Grade : 10"<<endl;
    }
    else if(h>50 && c<0.7)
    {
        cout<<"Steel Grade : 9"<<endl;
    }
    else if(c<0.7 && t>5600)
    {
        cout<<"Steel Grade : 8"<<endl;
    }
    else if(h>50  && t>5600)
    {
        cout<<"Steel Grade : 7"<<endl;
    }
    else if(h>50 || c<0.7 || t>5600)
    {
        cout<<"Steel Grade : 6"<<endl;
    }
    else
    {
         cout<<"Steel Grade : 5"<<endl;
    }
    return 0;
}
 
 

Output

Enter The Value of Hardness,Tensile Strength and Carbon :
50
34
4500
Steel Grade : 5
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples