Wednesday 7 February 2018

Control statements 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<stdio.h>
    #include<conio.h>  
    void main( ){
     int x,y;
    clrscr();
    printf("Enter two number \n");
    scanf("%d%d",&x,&y);
      if (x > y )
      {
      printf ("x is greater than y \n");
        }
     getch();
        }

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<stdio.h>
   #include<conio.h>
    void main( ){
    int x,y;

    clrscr();
    printf("Enter two number \n");
    scanf("%d%d",&x,&y);
      if (x > y )
      {
      printf ("x is greater than y \n");
        }

      else{
  printf ("x is smaller than y \n");
          } 
    getch();
             }
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< stdio.h>
    #include<conio.h>
    void main( )
     {
   int x;
   clrscr();
  printf("Enter a number to check whether number is +ve,-ve or zero \n");
   scanf("%d",&x);
   if (x > 0 )
   {
    printf( "Number is Positive \n");
    }
     else if (x < 0 )
   {
    printf( "Number is Negative \n");
    }
    else{
    printf("Number is zero \n");
    }
    getch();
      }
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 <stdio.h>  
#include<conio.h>
      void main () {  
       int number;  
      printf("Enter a number \n");    
       scanf("%d",&number); 
           switch (number)    
          {    
        case 1:
         printf("It is one");
       break;    
  case 2: 
      printf("It is two"); 
        break;    
      case 3: 
      printf("It is three"); 
     break;    
     default:
   printf("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...