Control Structures
Control Structures
Control structures allow to control the flow of program’s execution based on certain conditions.
C++ supports following basic control structures:
1) Selection Control structure
2) Loop Control structure
Selection Control structures allows to control the flow of program’s execution depending upon
the state of a particular condition being true or false. C++ supports two types of selection
statements: if and switch. Condition operator (?:) can also be used as an alternative to the if
statement.
If Statement: The syntax of an if statement in C++ is:
If (condition)
{
// statement(s) will execute if the condition is true
}
If the condition evaluates to true, then the block of code inside the if statement will be executed.
If it evaluates to false, then the first set of code after the end of the if statement (after the closing
curly brace) will be executed.
If (condition)
{
// statement(s) will execute if the condition is true
}
Else
{
// statement(s) will execute if condition is false
}
If the condition evaluates to true, then the if block of code will be executed, otherwise else block of
code will be executed.
An if statement can be followed by an optional else if...else statement, which is very useful to
test various conditions using single if...else if statement. The Syntax is shown as:
If (condition 1)
{
// executes when the condition 1 is true
}
Else if(condition 2)
{
// executes when the condition 2 is true
}
Else if(condition 3)
{
// executes when the condition 3 is true
}
Else
{
// executes when the none of the above condition is true.
}
Nested if Statement
It is always legal to nest if-else statements, which means you can use one if or else if statement
inside another if or else if statement(s). The syntax for a nested if statement is as follows:
If ( condition 1)
{
// Executes when the condition 1 is true
If (condition 2)
{
// Executes when the condition 2 is true
}
}
Switch
C++ has a built-in multiple-branch selection statement, called switch, which successively tests
the value of an expression against a list of integer or character constants. When a match is found,
the statements associated with that constant are executed. The general form of the switch
statement is:
switch (expression)
{
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
default
statement sequence
}
The expression must evaluate to a character or integer value. Floating-point expressions, for
example, are not allowed. The value of expression is tested, in order, against the values of the
constants specified in the case statements. When a match is found, the statement sequence
associated with that case is executed until the break statement or the end of the switch statement
is reached. The default statement is executed if no matches are found. The default is optional
and, if it is not present, no action takes place if all matches fail.
Nested switch Statements
It is possible to have a switch as part of the statement sequence of an outer switch. Even if the
case constants of the inner and outer switch contain common values, no conflicts will arise. C++
specifies that at least 256 levels of nesting be allowed for switch statements The syntax for a
nested switch statement is as follows:
switch(ch1)
{
case 'A':
cout << "This A is part of outer switch";
switch(ch2)
{
case 'A':
cout << "This A is part of inner switch";
break;
case 'B': // ...
}
break;
case 'B': // ...
}
2) Loop control structures
A loop statement allows us to execute a statement or group of statements multiple times. Loops
tells the program to repeat a fragment of code several times or as long as a certain condition
holds. C++ provides three type of loops: while, for, and do-while.
while loop
A while loop statement repeatedly executes a target statement as long as a given condition is true.
It is an entry-controlled loop. The syntax of a while loop in C++ is:
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any non-zero value.
do
{
statement(s);
}while( condition );
The conditional expression appears at the end of the loop, so the statement(s) in the loop execute
once before the condition is tested. If the condition is true, the flow of control jumps back up to
do, and the statement(s) in the loop execute again. This process repeats until the given condition
becomes false.
for Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times. It is also entry-level loop. The syntax of a for loop in C++
is: