Tuesday, October 1, 2019

if else ladder statement in c++, If Else Statement in C++,Ladder in c++.

                                      

                                        If  Else Ladder Statement in C++

            
                             Syntax:-
                             

                               if()
                              {
                                statement 1;
                              }
                              else if()
                              {
                                statement 2;
                               }
                              else if()
                              {
                                statement 3;
                              }
                              else
                              {
                                statement 4;
                              }


1. It is a part of conditional statement that executes on;y one condition at a time.
2. If all conditions are false then else part executes.
3. It executes that condition that becomes first true from the top.
4. In the case of if in the place of condition always zero and non-zero value is checked in which zero means condition false and non-zero means condition true

Example:-

#include<iostream.h>
int main()
{
//Assigning value to the variable
int x=10;

if(x>5)//checking the condition
{
cout<<"x is greater than 5";
}
else if(x<8)//checking the condition
{
cout<<"x is less than 8";
}
else if(x==10)//checking the condition
{
cout<<"x is equal to 10";
}
else
{
cout<<"No one condition is true";
}
}

                                  ### OUTPUT###
                         x is greater than 5

As we can see from the above program there are three conditions in which first and third condition are true but the it executes only one condition that becomes first true from the above so the output is x is greater than 5.





  

No comments:

Post a Comment