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:
/* 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; }
Enter The Value of Hardness,Tensile Strength and Carbon : 50 34 4500 Steel Grade : 5To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions