Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program. It decides the direction of flow of program execution.

Types of Conditional  statement

  1. If statement
  2. If-else statement
  3. Nested If statement
  4. Switch statement

If statement

Syntax:-

If( Condition….. )
{
    //statement//
}

Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
  int age=20;
  if(age>=18) 
   {
       printf(“Welcome to My TechnoloG4world”);
   }
  getch();
}

If-else statement

Syntax:-

If( Condition….. )
{
    //statement//
}
else
{
   //statement//
}
Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
  int age=20;
  if(age>=18) 
   {
       printf(“Welcome to My TechnoloG4world”);
   }
   else
    {
       printf(“You are Not Welcome to My TechnoloG4world”);
    }
  getch();
}

Nested if statement

Syntax:-

If( Condition ….. )
{
     //statement//
}
else if( Condition ….. )
{
    //statement//
}
else
{
   //statement//
}
Ex-
#include<stdio.h>
#include<conio.h>
void main()
{
   int age;
   printf("Input the value of age:");
   scanf("%d", &age);

   if(age>=18 && age<=60)
   {
       printf(“you are eligible  for vote");
   }
   else if(age>60)
   {
       printf("deleted name in voter list, So you are not eligible !");
   }
   else
   {
       printf(“you are not eligible for vote");
   }
  getch();
}

Switch statement

Syntax:-

Switch(expression)
{
    case expression :
                            //statement//
                            break;
    case expression :
                            //statement//
                            break;
    case expression :
                            //statement//
                            break;
    case expression :
                            //statement//
                            break;
    default :
                          //statement//
}
Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
 int a=2,b=3;
 int c;
Printf(“Enter the case Number”);
Scanf(“%d”,&c);
Switch(c)
{
   case 1:
             printf(“%d”,(a+b));
             break;
   case 2:
             printf(“%d”,(a-b));
             break;
    case 3:
             printf(“%d”,(a*b));
             break;
    default:
               printf(“Invalid case !”);
}
    getch();
}


Watch the Video Lecture


            Previous                                                                                                                       Next