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<iostream.h>
#include<conio.h>
class aptech
{
public:
eligible()
{
int age;
cout<<"Enter age=";
cin>>age;
if(age>=18)
{
cout<<"Eligible";
}
}
};
void main()
{
aptech ap;
clrscr();
ap.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<iostream.h>
#include<conio.h>
class aptech
{
public:
eligible()
{
int age;
cout<<"Enter age=";
cin>>age;
if(age>=18)
{
cout<<"Eligible";
}
else
{
cout<<"Not Eligible";
}
}
};
void main()
{
aptech ap;
clrscr();
ap.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<iostream.h>
#include<conio.h>
class aptech
{
public:
greatest()
{
int a,b,c;
cout<<"Enter three nos=";
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<"A is greatest";
}
if(b>a && b>c)
{
cout<<"B is greatest";
}
if(c> && c>b)
{
cout<<"C is greatest";
}
}
};
void main()
{
aptech ap;
clrscr();
ap.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<iostream.h>
#include<conio.h>
class aptech
{
public:
operation()
{
int a,b,ch;
clrscr();
cout<<"Enter two nos=";
cin>>a>>b;
cout<<"Enter 1 for sum\nEnter 2 for mul\nEnter 3 for sub\nEnter 4 for div\nEnter your choice=";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Sum="<<a+b;
break;
case 2:
cout<<"Mul="<<a*b;
break;
case 3:
cout<<"Sub="<<a-b;
break;
case 4:
cout<<"Div="<<a/b;
break;
default:
cout<<"Invalid choice";
break;
}
}
};
void main()
{
aptech ap;
ap.operation()
getch();
}