In C++, conditional statements are control structures that allow code to make decisions. These statements are also referred to as decision-making statements. These conditions are provided by a series of decision-making statements with Boolean expressions. Boolean expressions are evaluated as true or false. These types include if statement, if-else, if-else-if ladder, switch, and more. These statements determine how the program is executed.
Types of Conditional Statements in C++ include:
An if statement is made up of a Boolean expression that is evaluated into a Boolean value. If the value is true, the block is executed; else, the next statement is executed.
An if-else statement consists of two statements: the if statement and the else statement. When an expression in an if-statement is evaluated as true, the if block is executed; otherwise, the else block is executed.
An if-else-if statement, often known as a ladder statement, checks a set of conditions using subsequent if-else-if statements. The statement that meets the outcome will be performed. When no conditions match the result, the default else statement is executed.
An if-else block can be nested within another if-else block. This generates a nested if-else expression.
A switch statement evaluates a set of conditions using one or more cases that are compared to the switch expression. The case that meets the result will be performed. If no cases match the result, the default case will be executed.