Decision Making Statement in C++

In the C++ language decision making statement executes if the given condition is  true otherwise conditional block will never execute.

C++ language assumes non-zero and non-null values as true, and zero or null is assumed as false value.

There are following types of decision making statements in C++ programming language.

if statement

Syntax of an ‘if’ statement −

if(Condition) {
/* statement will execute if the condition is true */
}

In a ‘if’ statement if the Condition is true, then the block of statement of the ‘if’ statement will be executed.

If the Condition is false, then the block of statement of the ‘if’ statement will not executed and control sent to the next line of if block.

Example: Write a program to demonstrate the ‘if’ statement.

Output

In the above program if reverse the ‘if’ condition of ‘if’ statement then the condition will be false and ‘if’ block will never execute.

Output

if-else  statement

Syntax of an ‘if-else’ statement −

if(Condition) {
/* if-statement will execute if the Condition is true */
}
else
{ if the condition is false then else block will execute
}

In a ‘if-else’ statement if the Condition is true, then the block of statements of the ‘if’ statement will be executed.

If the Condition is false, then the block of code of the ‘else’ statement will executed.

Example: Write a program to  take two numbers from user and find the grater between them using ‘if-else’ statement.

Output

Nested -if statement
In a C language we can use if statement inside another if statement(s).
Syntax of an ‘nested -if’ statement −


if(condition 1) {
/* statement will execute if the condition 1 is true */
If (condition 2) { /* statement will execute if the condition 2 is true */
}
}

Example: Write a program to take two numbers from user if both number are between 1 to 9 then print “Good” using nested-if statement.

Output

Nested if-else statements

In a C language we can use  if or if-else statement inside another if or  if-else statement(s).

Syntax of an ‘if-else’ statement −

if(condition 1) { /* statement will execute if the condition 1 is true */
If (condition 2) {
/*if- statement will execute if the condition 1 & 2 both are true */
}
else {
/* else-statement will execute if the condition1 is true Condition2 is false */
}
}
else {
If (condition 3) {
/* statement will execute if the condition 3 is true */
}
}

Example: Write a program to  take three numbers from user and find the grater among them using ‘nested if-else’ statement.

Output