Travel the world

Climb the mountains

Post Page Advertisement [Top]


Conditional statement

                                    


  •       When the program is run the statements are executed sequentially by the compiler. The statements which are used to control the programs are called as control statements and the control statements controls the flow of programs execution. c language has the capacity to perform decision making ability. this execution flow is of two types sequential execution and random execution.

  • Sequential execution : in this execution statements executed one by one in sequential manner is called sequential execution .
  •  
  •  Random execution : in this execution statements are executed randomly and repeatedly it is called random execution. It is useful for writing the complex and better programs.

  •  Control statements :
  • 1)                        If…else statements:  this statements can be applicable for checking the single condition if condition is true the statements 1 will executed otherwise statement 2

  •   Syntax:
  •          If (condition)

                   {     statement 1 : 

                  }

           else

                 {      statements 2: 

                }

            }

     

     


  • Ex
  •      #include< stdio.h>
  •      #include<conio.h>
  •      #include<math.h>
  •    Void main ( )
  •   {              
  •         Int num;
  •         Clrscr( );
  •         Printf(“\nEnter the number=> ”);
  •         Scanf (“%d”,&num);
  •         If (num<0)
  •         {
  •                 Printf(“\n %d ”,abs(num) );
  •         }
  •   else
  •    {
  •         Printf(“\n%d”,num);
  •    }
  •    getch( );
  • }

  • flowchart


  • Nested if :  it is used when multiples condition are introduced in particular sequence . in below syntax condition -1 is check and if it is true then it enter in the inner loop check the condition-2 again it is true then display statement-1 otherwise display statement-2 ,if the condition -1is not true then it will execute the statement-3 directly and cannot enter in the inner loop and the program is exit.
  • Syntax
  •       If (condition1)
  •       {
  •       If (condition 2)
  •      {             Statement-1;
  •     }
  •      else
  •      {            Statement-2;
  •     } 
  •  }
  •     else
  •     {           Statement-3;
  •   }

  • Ex
  •       #include<stdio.h>
  •       #include<conio.h>
  •       Void main( )
  •  {
  •       Int a=0,b=0,c=0;
  •       Clrscr();
  •       Printf(“enter the number a,b,c\n”);
  •       Scanf (“%d%d%d,&a,&b,&c”);
  •       If (a>b)
  •      {
  •        If(a>c)
  •       {
  •            Printf(“the number %d is largest”,a);
  •       }
  •      else
  •      {
  •             Printf(“the number%d is largest”,c);
  •      } 
  •    }
  •       else
  •     {
  •              Printf(“the number %d is largest”,b);
  •     }
  •        getch( );
  •   }
  •      Out put
  •       Enter the number a,b,c
  •       80 90 95
  •       The number 95 is largest
  • Flowchart :

  •  
  • If else ladder :  is used in the situation where there are multiple conditions to be checked so you can put  multiple if ‘s with else so that checking of conditions will be in the sequence . in below form considering only two conditions is to be checked , if condition -1 is true then display the statement-1,  if false then check the condition 2
  • If it is true then display statements 2 otherwise else part is display 
  • Syntax
  •       If (condition 1)
  •      {      
  •            Statement-1;
  •      }
  •       else if (condition 2)
  •    {
  •            Statement-2;
  •     }
  •      else
  •     {
  •                 Statement-3 ;
  •    }
  • Ex
  • #include<stdio.h>
  • #include<conio.h>
  • Void main ( )
  • {
  •         Int per ;
  •         Clrscr();
  •         Printf(“\n enter per=”);
  •         Scanf(“%d”,&per);
  •         If (per>=75)
  • {
  •         Printf(“\n distinction”);
  • }
  •   else if (per >= 60 && per < 75)     
  • {
  •         Printf(“\n 1st  class”);
  • }
  •   else if (per >= 50 && per <60)
  • {
  •         Printf(“\n 2nd  class”);
  • }
  •   else if (per >= 40 && per < 50)
  • {
  •         Printf(“\n student is pass”);
  • }
  •   else
  • {
  •         Printf(“\n student is fail ”);
  • }
  •   Getch();
  • }
  • Flowchart :






  • Switch statements :   in this statements if conditions are more, then if statements look complex and more confusing. Fortunately, c has a built in multi-way decision statements known as switch statement. The switch –case statement looking complex but it is very easy to understand in term of the, the logic of sequence of the instruction and branching of the condition. This statement accepts value and tests it for variety of conditions. In this statements the default case execute when all conditions are false.  
  •    syntax

  •  
  •     Switch (expression )

       {    case value-1:


              Statement 1;

              Statement 2;

                       break ;

            Case value-2:


              Statement 1; 

              Statement 2;

                       break; 

      Default :


              Statement 1;

              Statement 2;

                       break;

       }




Ex

#include<stdio.h>

#include<conio.h>

Void main ( )

 {

          int ch,n1,n2,res;

          clrscr( );

          printf(“\n:add  \n2:sub  \n3:mult  /n4:div  \n5:exit ”);

          printf(“\n Enter two values=>  ”);

          scanf(“%d%d”,&n1,&n2);

printf(“\n Enter your choice=>  ”);

scanf(“%d”,&ch);

Switch (ch)

{

          Case1: printf(“\n add=%d”,n1+n2);

          break ;

          Case2: printf(“\n sub =%d”,n1-n2);

          break ;

          Case3: printf(“\n mult =%d”,n1*n2);

          break ;

          Case4: printf(“\n div=%d”,n1/n2);

          break ;

          Case5:exit(0);

          default: printf(“\n invalid choice”);

}

          getch( );

}




Flowchart











No comments:

Post a Comment

Bottom Ad [Post Page]