Control Statements

PropellerAds

IF – ELSE STATEMENT
The complete form of the if statement is
if(expression) statement
else statement

where the targets of the if and else are single statements. The else clause is optional.
The targets of both the if and else can also be blocks of statements. The general form of
the if using blocks of statements is

if(expression)
{
statement sequence
}
else
{
statement sequence
}

If the conditional expression is true, the target of the if will be executed; otherwise, the target of the else, if it exists, will be executed. At no time will both be executed. The conditional expression controlling the if may be any type of valid C++ expression that produces a true or false result.

NESTED IFS :
A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. The main thing to remember about nested ifs in C++ is that an else statement always refers to the nearest if statement that is within the same block as the else and not already associated with an else. The general form of the nested if using blocks of statements is

if(expression)
{
statement sequence
if(expression)
{
statement sequence
}
else
{
statement sequence
}
}
else
{
statement sequence
}
Here is an example
if(i)
{
if(j) result =1;
if(k) result =2;
else result = 3;
}
else result = 4;

The final else is not associated with if(j) (even though it is the closest if without an else),
because it is not in the same block. Rather, the final else is associated with if(i). The
inner else is associated with if(k) because that is the nearest if. You can use a nested if
to add a further improvement to the Magic Number program. This addition provides the
player with feedback about a wrong guess.

THE SWITCH STATEMENT
The second of C++’s selection statements is the switch. The switch provides for a multiway branch. Thus, it enables a program to select among several alternatives. Although a series of nested if statements can perform multiway tests, for many situations the switch is a more efficient approach. It works like this: the value of an expression is successively tested against a list of constants. When a match is found, the statement
sequence associated with that match is executed.

The general form of the switch statement is
Switch(expression)
{ case constant 1 :
Statement sequence
Break;
case constant 2 :
Statement sequence
Break;
case constant 3 :
Statement sequence
Break;

Default:
Statement sequence
}

The switch expression must evaluate to either a character or an integer value. (Floatingpoint expressions, for example, are not allowed.) Frequently, the expression controlling the switch is simply a variable. The case constants must be integer or character literals.

The default statement sequence is performed if no matches are found. The default is
optional; if it is not present, no action takes place if all matches fail. When a match is
found, the statements associated with that case are executed until the break is
encountered or, in a concluding case or default statement, until the end of the switch is
reached. There are four important things to know about the switch statement:
The switch differs from the if in that switch can test only for equality (that is, for matches
between the switch expression and the case constants), whereas the if conditional
expression can be of any type.

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. A
switch statement is usually more efficient than nested ifs. The statement sequences
associated with each case are not blocks. However, the entire switch statement does
define a block.

THE FOR LOOP
You have been using a simple form of the for loop since Module 1. You might be surprised at just how powerful and flexible the for loop is. Let’s begin by reviewing the basics, starting with the most traditional forms of the for. The general form of the for loop for repeating a single statement is
for(initialization; expression; increment) statement;

For repeating a block, the general form is
for(initialization; expression; increment)
{
statement sequence
}

The initialization is usually an assignment statement that sets the initial value of the loop
control variable, which acts as the counter that controls the loop. The expression is a
conditional expression that determines whether the loop will repeat. The increment
defines the amount by which the loop control variable will change each time the loop is
repeated. Notice that these three major sections of the loop must be separated by
semicolons. The for loop will continue to execute as long as the conditional expression
tests true. Once the condition becomes false, the loop will exit, and program execution
will resume on the statement following the for block.

THE WHILE LOOP
Another loop is the while. The general form of the while loop is
while(expression)
statement;

where statement may be a single statement or a block of statements. The expression defines the condition that controls the loop, and it can be any valid expression. The statement is performed while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.

THE DO-WHILE LOOP
The last of C++’s loops is the do-while. Unlike the for and the while loops, in which the
condition is tested at the top of the loop, the do-while loop checks its condition at the
bottom of the loop. This means that a do-while loop will always execute at least once.

The general form of the do-while loop is
do {
statements;
}
while(condition);

Although the braces are not necessary when only one statement is present, they are often used to
improve readability of the do-while construct, thus preventing confusion with the while.

The do-while loop executes as long as the conditional expression is true.

USING BREAK TO EXIT A LOOP
It is possible to force an immediate exit from a loop, bypassing the loop’s conditional
test, by using the break statement. When the break statement is encountered inside a
loop, the loop is immediately terminated, and program control resumes at the next
statement following the loop.

Using continue
It is possible to force an early iteration of a loop, bypassing the loop’s normal control
structure. This is accomplished using continue. The continue statement forces the next
iteration of the loop to take place, skipping any code between itself and the conditional
expression that controls the loop.

USING THE GOTO STATEMENT
The goto is C++’s unconditional jump statement. Thus, when encountered, program flow
jumps to the location specified by the goto.

There are times when the use of the goto can clarify program flow rather than confuse it.
The goto requires a label for operation. A label is a valid C++ identifier followed by a colon. Furthermore, the label must be in the same function as the goto that uses it.

Try Now – PROGRAMMING IN C++ MCQs