Saturday, 18 August 2018

Difference between if else statement an switch statement in C using a program

Program To Find Maximum Of Three Number Using 'If else' Statement 

/*program to find maximum of 3 number*/
#include <stdio.h>
void main ()
{
int a,b,c;
clrscr ();
printf ("/n Enter value of a :");
scanf ("%d", & a);
printf ("/n Enter value of b :");
scanf ("%d", & b);
printf ("/n Enter value of c :");
scanf ("%d", & c);
if ((a>>b)&&(a>>c))
printf ("/n a is maximum number:");
else if ((b>>a)&&(b>>c))
printf ("/n b is maximum number:");
else
printf ("/n c is maximum number:");
getch ();
}

Here we use if else statement for finding maximum of three number.

Output :: Enter value of a 
               a = 25                                                                                                                                                                                           Enter value of b
               b = 35
               Enter value of c
               c = 45 

               Maximum number is c.

Program To Displays Color According To The Enter Character From User in C

/*program to print color*/
# include <stdio.h>
void main ()
{
char ch ;
clrscr () ;
printf ("/n enter a character :") ;
scanf ("%c", & ch) ;
switch (ch)
{
case 'r' ;
printf ("\n colour is red :") ;
break ;
case 'w' ;
printf ("\n color is white :") ;
break ;
case 'y' ;
printf ("\n color is yellow :") ;
break ;
case 'g' ;
printf ("\n color is green :") ;
break ;
case 'b' ;
printf ("\n color is blue :") ;
break ;
default :
printf ("\n invalid character :") ;
}
getch () ;
}

Difference in points :

IF else statement in C

1. Here we can use more than one statement for execution.

2. Here we can use all types of variable.

3. If else statement contains two types of statement means one in if and other in else.

4. If condition written inside if is false then compiler will shift to the statement written in else statement.

5. syntax :  @  if statement 
   
1. syntax :  if (condition)
                   statement :

2. syntax :  if (condition)
                  {
                  statement 1 ;
                  statement 2 ;
                  '
                  '
                 }

@  if else statement 

1. syntax;  if (condition)
      {
      statement ;
      '
      '
      }
      else
      {
      statement ;
       '
       '
       }


Switch statement in C

 1. Here we can use only one statement for execution.

 2. Here we can use only character types of variable.

 3. switch statement contains two types of statement means one in switch and other in default statement.

4. If condition written inside switch is false then compiler will shift to the statement written in default statement.

5. @  switch statement 
    
   syntax ;   switch ( conditional variable )
                  {
                  case value 1 ;
                  statement ;
                  break ;
                  case value 2 ;
                  statement ;
                  break ;
                  '
                  '
                  '
                 default :
                 statement ;
                 }












No comments:

Post a Comment

Featured post

Program to find maximum of three number using user define function in C++

A function can be define by the user as per the requirement of the user or programmer . To implement a user define function , a programmer ...