If And If Else In Cpp
Control Structures:
A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances.
With the introduction of control structures we are going to have to introduce a new concept: the compoundstatement or block. A block is a group of statements which are separated by semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { }:
Syntax
{ statement1; statement2; statement3; }
Most of the control structures that we will see in this section require a generic statement as part of its syntax. A statement can be either a simple statement (a simple instruction ending with a semicolon) or a compound statement (several instructions grouped in a block), like the one just described. In the case that we want the statement to be a simple statement, we do not need to enclose it in braces ({}). But in the case that we want the statement to be a compound statement it must be enclosed between braces ({}), forming a block.
Conditional structure: if and else:
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:
Syntax
if (condition) statement
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.
Following is the general form of a typical decision making structure found in most of the programming languages −
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:
CODE/PROGRAM/EXAMPLE
if (x == 100)
cout << “x is 100”;
If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:
CODE/PROGRAM/EXAMPLE
if (x == 100)
{
cout << “x is”;
cout << x;
}
We can additionally specify what we want to happen if the condition is not fulfilled by using the keyword else. Its form used in conjunction with if is:
Syntax
if (condition) statement1 else statement2
Flow Chart:
For example :
CODE/PROGRAM/EXAMPLE
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
prints on the screen x is 100 if indeed x has a value of 100, but if it has not -and only if not- it prints out x is not 100.
The if + else structures can be concatenated with the intention of verifying a range of values.
The following example shows its use telling if the value currently stored in x is positive, negative or none of them (i.e. zero):
CODE/PROGRAM/EXAMPLE
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
Remember that in case that we want more than a single statement to be executed, we must group them in a block by enclosing them in braces { }.
For example :
CODE/PROGRAM/EXAMPLE
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
int a = 100;
// check the boolean condition
if( a == 10 ) {
// if condition is true then print the following
cout << "Value of a is 10" << endl;
} else if( a == 20 ) {
// if else if condition is true
cout << "Value of a is 20" << endl;
} else if( a == 30 ) {
// if else if condition is true
cout << "Value of a is 30" << endl;
} else {
// if none of the conditions is true
cout << "Value of a is not matching" << endl;
}
cout << "Exact value of a is : " << a << endl;
return 0;
}
O/P : Value of a is not matching
Exact value of a is : 100