Skip to main content

Conditional Statements

There are Four Types :

  1. if
  2. if-else
  3. if-else-ladder
  4. nested-if

1. if

1. if
syntax :

if (condition)
{
//statements
}

Example 1 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num>0){
cout<<"Positive Number";
}

return 0
}

Output :

Enter Number : 10
Positive Number

Explanation :

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num>0) check condition , condition is true then print the statement otherwise end of if body.

Example 2 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num>0){
cout<<"Positive Number";
}

return 0
}

Output :

Enter Number : -5

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num>0) check condition , condition is true then print the statement otherwise end of if body.

2. if-else

2. if-else
syntax :
if (condition)
{
// True
}
else
{
//----
//----
}

Example 3 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num>0){
cout<<" Positive Number";
}
else{
cout<<" Negative Number";
}

return 0
}

Output :

Enter Number : 10
Positive Number

Explanation :

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num>0) condition is true print statement otherwise jump to else part .

Example 4 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num%2==0){
cout<<"Number is Even";
}
else{
cout<<"Number is Odd";
}

return 0
}

Output :

Enter Number : 10
Number is Even

Explanation :

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num%2==0) condition is true print statement otherwise jump to else part .

Example 5 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num%2==0){
cout<<"Number is Even";
}
else{
cout<<"Number is Odd";
}

return 0
}

Output :

Enter Number : 9
Number is Odd

Explanation :

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num>0) condition is true print statement otherwise jump to else part .

Example 6 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num%3==0 && num%5==0){
cout<<"Number is divisible by 5 & 3";
}
else{
cout<<"Number is NOT divisible by 5 & 3";
}

return 0
}

Output :

Enter Number : 15
Number is divisible by 5 & 3

Explanation :

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num%3==0 && num%5==0) condition is true print statement otherwise jump to else part .

Example 7 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num%3==0 && num%5==0){
cout<<"Number is divisible by 5 & 3";
}
else{
cout<<"Number is NOT divisible by 5 & 3";
}

return 0
}

Output :

Enter Number : 10
Number is NOT divisible by 5 & 3

Example 8 :

#include <iostream>
using namespace std;

int main() {
int num;

cout<<"Enter Number : ";
cin>>num;

if(num%3==0 || num%5==0){
cout<<"Number is divisible by 5 or 3";
}
else{
cout<<"Number is NOT divisible by 5 or 3";
}

return 0
}

Output :

Enter Number : 10
Number is divisible by 5 or 3

Explanation :

Line 6 : declare num variable.
Line 8 : Enter number from user.
Line 11 : if(num%3==0 || num%5==0) condition is true print statement otherwise jump to else part .

Example 9 :

#include <iostream>
using namespace std;

int main() {
int rate,quantity;

cout<<"Enter rate & Quantity : ";
cin>>rate>>quantity;

int bill = rate * quantity

cout<<"Bill: "<<bill;

return 0
}

Output :

Enter Rate & Quantity : 5 10
Bill 50

Explanation :

Line 5 : declare 2 vaeiable rate and quantity.
Line 7 : Enter rate and quantity from user.
Line 10 : declare bill variable and assign rate * quantity.
Line 12 : print Bill and bill variable using cout.