Monday, 5 February 2018

Control statements or decision making in C++


Control statements are used to control and execute the statements based on certain conditions or repeat a group of statements until certain specified conditions are met
There are different types of if statements in C++.
  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder
if Statement:
if statement checks the condition. It is executed if condition is true.
Syntax:
    if(condition){    
   //statement
             } 
    Example:
#  include< iostream.h>
#  include<conio.h>  
    int main( )
{   
     int x,y;
    clrscr();
    cout<<"Enter two number"<<endl;
      cin>>x>>y;
      if (x > y )
      {
      cout << "x is greater than y";
        }

     return 0;
}

if-else Statement
If-else statement checks the condition. It executes if condition is true otherwise it comes to else block and else block will be executed.
Syntax:
     if(condition){    
     //statement
               }
         else{    
        //statement
                  }    
     Example:
   #include< iostream.h>
   #include<conio.h>
    int main( ){
    int x,y;
    clrscr();
     cout<<"Enter two number"<<endl;
      cin>>x>>y;
         if (x > y )
     {
  cout << "x is greater than y";
     }
      else{
   cout << "x is smaller than y";
          } 
    return 0;
             }
If-else-if ladder Statement
if-else-if ladder statement executes one condition from multiple conditions.It will check the conditions one by one ,if the condition is true then print the output otherwise it comes to  else block and else block will be executed.
Syntax:
   if(condition1){   
     / /statement
}   
     else if(condition2){    
     //statement
}  
     else if(condition3){    
      //statement
}   
       ...........................    
     else{    
     //statement
        }
   Example:
    #include< iostream.h>
    #include<conio.h>
    int main( )
     {
   int x;
   clrscr();
   cout<<"Enter a number to check whether number is +ve,-ve or zero"<<endl;
   cin>>x;
   if (x > 0 )
   {
    cout << "Number is Positive"<<endl;
    }
     else if (x < 0 )
   {
    cout << "Number is Negative"<<endl;
    }
    else{
    cout << "Number is zero"<<endl;
    }
    return 0;
switch
switch statement executes one statement from multiple conditions. It will check the conditions one by one. If it finds true, it will print the output otherwise it comes to default and default statement will be executed. It is just like if-else-if ladder
Syntax:
switch(expression){      
case value1:      
 //statement
 break;    
case value2:      
 //statement
 break;    
......      
      
default:       
 //statement
}    

Example:
 # include <iostream.h>  
   #include<conio.h>
     int main () {  
       int number;  
       cout<<"Enter a number :";    
       cin>> number;  
           switch (number)    
          {    
        case 1:
         cout<<"It is one";
       break;    
  case 2: 
      cout<<"It is two"; 
        break;    
      case 3: 
      cout<<"It is three"; 
     break;    
             
    default:
    cout<<"Invalid"; 
          }    
    }    


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...