Conditional Statements

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

CONDITIONAL STATEMENTS

Conditional statements are the statements that checks an expression then


may or may not execute a statement or group of statements depending on the
result of the condition.
In C++ the following are the types of conditional statements:

1. if Statement
The general form of the if statement is:
if (expression)
{
statement;
}
where:
if is a reserve word in C++.
expression is a relational or Boolean expression that evaluates
to a true (1) or false (0) value.
statement may either be a single C++ statement or a block of
C++ statements.

In if statement, if the expression evaluates to true (1), the statement or


the block of statements that forms the target of if statement will be executed.
Otherwise, the program will ignore the statement or block of statements.
NOTE: Never place a semicolon after the expression in if
statement.

2. if else Statement
The general form of if else statement is:
if (expression)
{
statement_1;
}
else
{
statement_2;
}

where:
if and else are reserve words in C++.
expression is a relational or Boolean expression that evaluates
to a true (1) or false (0) value.
statement_1 and statement_2 may either be a single C++
statement or a block of C++ statements.

In if else statement, if the expression is evaluated as true (1), the


statement or block of statements after if statement will be executed;
otherwise, the statement or block of statement in the else statement will be
executed.

3. if else if Ladder
A common programming construct in C++ is the if else if ladder.
The general form of if else if ladder is:
if (expression_1)
{
statement_1;
}
else if (expression_2)
{
statement_2;
}
else if (expression_3)
{
statement_3;
}
else
{
statement_else;
}

Where:
if and else are reserve words in C++.
expression_1 up to expression_n is a relational or Boolean
expression that evaluates to a true (1) or false (0) value.
statement_1 up to statement_else may either be a single C++
statement or a block of C++ statements.

In if else if ladder statement, the expressions are evaluated from top


downward. As soon as a true condition is found, the statement associated with it is
executed, and the rest of the ladder will not be executed. If none of the condition is
true, the final else is executed.
The final else acts as a default condition. If all other conditions are false, the
last statement is performed. If the final else is not present, then no action takes
place.
NOTE: The final else is optional, you may include this part if needed
in the program or you may not include if not needed.

4. Nested if Statement
One of the most confusing aspects of the if statement in any
programming language is nested ifs. A nested if is an if statement that is the
object of the either an if or else. This is sometimes referred to as an if within
an if.
The reason why nested if is so confusing is that it can be difficult to
know about what else associates with what if.
Fortunately, C++ provides a very simple rule for resolving this type of
situation. In C++, the else is linked to the closest preceding if that does not
already have an else statement associated with it.
5. switch Statement
The switch statement is a multiple branch decision statement.
The general form of the switch statement is:

switch (variable)
{
case constant_1:
{
statement_1;
break;
}

case constant_2:
{
statement_2;
break;
}
case constant_3:
{
statement_3;
break;
}
default:
{
statement_default;
}
}
In a switch statement, a variable is successively tested against a list of
integer or character constants. If a match is found, a statement or block of
statements is executed. The default part of the switch is executed if no matches are
found.
According to Herbert Schildt (1992), there are three important things to know
about switch statements:
1. The switch differs from if statements in such a way that switch can only
test for equality whereas if can evaluate a relational or logical expression.
2. No two case constants in the same switch can have identical values. Of
course, a switch statement enclosed by an outer switch may have case
constants that are the same.
3. If character constants are used in the switch, they are automatically
converted to their integer values.

NOTE: The break statement is used to terminate the statement sequence


associated with each case constant. It is C++ keyword which means that
at that point of execution, the code should jump to the end of the switch
statement terminated by the } symbol.

You might also like