If Statement

if check the condition if condition is true then do something otherwise do nothing

Syntax:-

if(condition)
{
statement
}

Q-WAP which accept age from the user and check he/she is eligible for vote?

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter age=");
scanf("%d",&age);
if(age>=18)
{
printf("Eligible");
}
getch();
}

If else Statement

if check the condition if condition is true then do something otherwise do something else

Syntax:-

if(condition)
{
statement
}
else
{
statement
}

Q-WAP which accept age from the user and check he/she is eligible for vote or not?

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter age=");
scanf("%d",&age);
if(age>=18)
{
printf("Eligible");
}
else
{
printf("Not Eligible");
}
getch();
}

Multiple If Statement

if check the condition if condition is true then do something otherwise check the next condition

Syntax:-

if(condition)
{
statement
}
if(condition)
{
statement
}

Q-WAP which accept three nos from the user and check which number is greatest

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter three nos=");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("A is greatest");
}
if(b>a && b>c)
{
printf("b is greatest");
}
if(c>a && c>b)
{
printf("c is greatest");
}
getch();
}

Switch Case Statement

It checks the condition choice-wise

Syntax:-

switch(choice)
{
case _:
statement
break;
case _:
statement
break;
default:
statement
break;
}

Q-WAP which accept two nos from the user and choice from the user if choice:-
1-Display Sum
2-Display Mul
3-Display Sub
4-Display Div

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ch;
clrscr();
printf("Enter two nos=");
scanf("%d%d",&a,&b);
printf("Enter 1 for sum\nEnter 2 for mul\nEnter 3 for sub\nEnter 4 for div\nEnter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Sum=%d",a+b);
break;
case 2:
printf("Mul=%d",a*b);
break;
case 3:
printf("Sub=%d",a-b);
break;
case 4:
printf("Div=%d",a/b);
break;
default:
printf("Invalid choice");
break;
}
getch();
}