Skip to content

Decision Making

In making decisions in programming, the programmer should use comparison expression and then choose a path in which to take depending on the answer to the comparison.

Comparison Operators

Comparison operators are used to compare two values which then results to a boolean value. Here are some of the comparison operators in C++:

OperatorNameExampleResult
==Equal to25 == 25true
!=Not equal to25 != 30true
<Less than25 < 30true
<=Less than or equal to25 <= 30true
>Greater than30 > 25true
>=Greater than or equal to30 >= 25true

Logical Operators

Logical operators are used to combine two or more comparison expressions. Here are some of the logical operators in C++:

OperatorNameExampleResult
&&AND25 < 30 && 5 < 10true
||OR(25 < 30) || (5 > 10)true
!NOT!(25 < 30)false

Decision Making Statements

Decision making statements are used to make decisions in a program. Here are some of the decision making statements in C++:

if Statement

The if statement is used to execute a block of code only if the condition is true. Here is the syntax of the if statement:

if (condition) {
// code to be executed if the condition is true
}

Here is an example of using the if statement:

int age = 25;
if (age >= 18) {
cout << "You are an adult";
}

if-else Statement

The if-else statement is used to execute a block of code if the condition is true and another block of code if the condition is false. Here is the syntax of the if-else statement:

if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}

Here is an example of using the if-else statement:

int age = 15;
if (age >= 18) {
cout << "You are an adult";
} else {
cout << "You are a minor";
}

nested if-else Statement

The nested if-else statement is used to execute a block of code if the condition is true and another block of code if the condition is false. Here is the syntax of the nested if-else statement:

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if both condition1 and condition2 are false
}

Here is an example of using the nested if-else statement:

int age = 15;
if (age >= 18) {
cout << "You are an adult";
} else if (age >= 13) {
cout << "You are a teenager";
} else {
cout << "You are a child";
}

switch Statement

The switch statement is used to execute a block of code based on the value of a variable. It is a more efficient way to write multiple if-else statements. Here is the syntax of the switch statement:

switch (variable) {
case value1:
// code to be executed if variable is equal to value1
break;
case value2:
// code to be executed if variable is equal to value2
break;
default:
// code to be executed if variable is not equal to any of the values
}

Here is an example of using the switch statement:

int day = 3;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
default:
cout << "Invalid day";
}

Tenary Operator

The tenary operator is a shorthand way of writing an if-else statement. Here is the syntax of the tenary operator:

(condition) ? expression1 : expression2;

Here is an example of using the tenary operator:

int age = 15;
string result = (age >= 18) ? "You are an adult" : "You are a minor";
cout << result;

In the example above, the value of result will be "You are a minor" because the condition age >= 18 is false.