C++-if-else-statement .

In this session i have discussed if-else statement in C++ .


IF-ELSE-Statement :

If-else statment is a extension of if-statement . It is allow to evaluate the specified condition if condition is true then the statement inside the if will be executed other wise control transfer to else part and the statement inside the else will be executed .

IF-Elese-statement-syntax :

mvc image


Example : Write a program check the age for voting .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int age=15;
          if(age>=18)
          {
             cout<<"Valid for vote";
          }
          else
          {
             cout<<"Not valid for vote";
          }
          return 0;
    }
    
    OUTPUT : Not valid for vote
    


Example : Write a program check the even or odd number .

    #include<iostream>
    using namespace std; 
    int main()
    {  
          int x=5;
          if(x%2==0)
          {
              cout<<"Even Number";
          }
          else
          {
              cout<<"Odd Number";
          }
          return 0;
    }
    
    OUTPUT : Odd Number
    


Example : Write a program check the userid and password for login .

    #include<iostream>
    using namespace std; 
    int main()
    { 
          int uid=10,pwd=20;
          if((uid==10) && (pwd==20))
          {
             cout<<"Login successfull";
          }
          else
          {
            cout<<"Invalid userid or password ";
          }
          return 0;
    }
    
    OUTPUT : Login successfull
    

Welcome to AndroidATPoint.

back image                     next image