Branching
As we discussed earlier, determining the correct path or choosing one block of code to execute among multiple blocks of code can be described as Branching. Branching can be performed based on whether a Boolean expression evaluates to true
or false
. Hence, following this concept, we get to choose our desired statement or groups of statements to execute based on an outcome of a Boolean expression.
The if
and switch
statements are the two main branching control structures. if
is the most commonly used conditional structure within any programming language. switch
can be used in certain situations where multiple branches can be chosen by a single value or expression, or where a series of if
statements would be inconvenient.
The if Statement
The syntax of if
is as follows:
if (expression) Â Â Â Â statement;
Here, if (expression)
is the control structure, and statement
is a single-line statement terminated with a semicolon or multiple statements enclosed...