C++ if, if…else and Nested if…else

Statements are very important in programming through statements you make your code(programs) intelligence to make decision in different circumstances, means by using statements you tells your program what to do or how to do in different situations. 

Uses of  Statements (if..else & Nested..if)

  • A comparison of two values using operators, like if (a == b) or if (a != b) or if (a > b) or if (a <= b).
  • Checks weather a function is true/false to make sure which block of code should run regarding the checked value.
  • Use to check the validity of results are appropriate or not regarding our defined terms in statement field.

   c++

C++ if statement

This type of statement is employ when you have to do only some specific tasks(means to run a set of instructions) regarding on one or more then one conditions.

How to implement more then one condition in single if condition section??

Its easy to implement more then one condition at same place basically it has to ways to implement.

  1. Nested If..else
  2. Logical operators(&&, ||)

 

When the body of if statement execute??

if your condition is TRUE only then the body of if statement will be executed

If your condition is FALSE then it will be skipped.

If (condition)

{

//body

}

Example 1:

#include<iostream>
using namespace std;
void main()
{
int a;
a = 0;
cout << “Enter your number”<<endl;
cin >> a;
if (a < 100)
{
cout <<“Your number is less than hundred”<<endl;
}
system(“pause”);
}

Output:

Enter your number
99
Your number is less than hundred

 C++ If…else statement

When we use an else statement with if then body of else statement will be executed when condition of if is false

If (condition)

{

//body

}

else

{

//body

}

#include<iostream>
using namespace std;
void main()
{
int a = 0;
cout << “Enter your number “;
cin >> a;
if (a < 100)
{
cout << “Your no is less than hundred” << endl;
}
else
{
cout<< “Your no is greater than hundred” << endl;
}
system(“pause”);
}

Output:

Enter your number 105
Your no is greater than hundred

How if & if…else statement works?

if else statement c++

C++ else if statement

It is use when multiple conditions we have to check in a C++ program.

if (condition)

{

//body

}

else if (condition)

{

//body

}

else

{

//body

}

Example:

#include<iostream>
using namespace std;
void main()
{
int a = 0;
cout << “Enter your number “;
cin >> a;
if (a > 0)
{
cout << “Your no is positive” << endl;
}
else if (a < 0)
{
cout<< “Your no is negative” << endl;
}
else
{
cout << “Your no is zero” << endl;
}
system(“pause”);
}

Output 1:

Enter your number
91
Your no is positive

Output 2:

Enter your number
-12
Your no is negative

Output 3:

Enter your number
0
Your no is zero

C++ Nested if…else

It is use when conditions check inside the conditions for the single output.

If (condition1)

{

If (condition2)

{

//body

}

}

else

{

//body

}

Example:

#include<iostream>
using namespace std;
void main()
{
int a = 0;
cout << “Enter your number “ << endl;
cin >> a;
if (a < 100)
{
if (a % 2 == 0)
{
cout << “Your no is even & less than hundred” << endl;
}
else
{
cout << “Your no is odd & less than hundred” << endl;
}
}
else
{
cout << “Your no is greater than hundred” << endl;
}
system(“pause”);
}

Output 1:

Enter your number
22
Your no is even & less than hundred

Output 2:

Enter your number
33
Your no is odd & less than hundred

Output 3:

Enter your number
123
Your no is greater than hundred

 

How Nested if…else works

c++

if (condition1)

{

If (condition2)

{

//body

}

else

{

//body1

}

}

else

{

//body2

}

Conditional/ternary operator

There are some operands that could be use instead of if…else statement.

If (a>b)

{

A=b;

}

else

{

A= -b;

}

Example 1:

#include<iostream>
using namespace std;
void main()
{
int a;
int b;
cout << “Enter the value of a” << endl;
cin >> a;
cout << “Enter the value of b” << endl;
cin >> b;
if (a > b)
{
a = b;
cout << “value of a after the above operator = ” << a << endl;
}
else
{
a = -b;
cout << “value of b after the above operator = ” << b << endl;
}
system(“pause”);
}

 

Output 1:

Enter the value of a
2
Enter the value of b
1
value of a after the above operator = 1

 

Output 2:

Enter the value of a
2
Enter the value of b
3
value of b after the above operator = 3