You are on page 1of 27

Flow of controls

Unit :3
Part-1
(decision control statements)
Syllabus-part1
1. compound statements(blocks).
2. Decision control statement:-
1. if statement.
2. if-else statement.
3. nested if-else.
4. else if ladder.
5. switch statement.
6. goto statement.
7. conditional operators.

compound statements(blocks)
A block of statements, also called as
compound statement, is a group of
statements that is treated by the compiler as a
single statement.
Blocks begin with a brace bracket { and ends
also close with a }. Block can be used any
place where a single statement is allowed.

Decision Control Statements
C++ language is able to perform different sets of
actions depending on the circumstances. The three
major decision making instructions are :-
The If Statements
If-else Statements
The Switch Statements
Many times, we want a set of Instructions to be
executed in one situation, and an entirely different
set of instructions to be executed in another
situation.This kind of situation is done in C
programs using decision control Instruction.
Conditions
Conditions are expressions that evaluate to
a boolean valuea true or false value
(true andfalse are C++ keywords, representing
the two possible values of a boolean
expression or variable). Simple conditions
involve two operands, each of which can be a
variable or a literal value, and an operator,
typically a comparison operator.
For ex: to check a is greater than and equal to
b we can writr the conditio as:- if(a>=b).
Composite Conditions

Very often, we encounter situations where the condition
can not be expressed as the simple conditions from the
previous section, just by comparing two values. An example
of such situations is testing if a number is within a
given range of values; for instance, testing if a number is
between 0 and 10.
This example involves testing two conditions, and verifying
if the two conditions are simultaneously met. In other
words, we want to test if the number is greater than or
equal to 0 and also less than or equal to 10.
To this end, we use logical operators to combine two
conditions. In the above example, we use the logical
AND operator&&, as shown below:

For ex: if (0>= number && number <= 10)



Decision Control Statements
1. The if Statement.
2. The if-else Statement.
3. The Nested if-else.
4. else-if ladder
5. The switch statement.
6. The conditional operators.
7. The go to statement.


1. The if Statement.
The general syntax:
If(test condition)
{
Statement -block1;
Statement -block2;
----------;
----------;
}
Statement-x;

The if Statement.contd
If the condition returns true value the all the
statements inside the brace of if block are
executed.otherwise,if the condition return
false, then the statement outside the if block
is executed.
2. If else statement
The simple if statement covers the cases
where we have a fragment of code that should
be executed depending on a condition. If we
have a situation where we have two possible
actions, and we want to do one or the other,
depending on a given condition, we use
the ifelse statement.

If else statementcontd
The general syntax:
If(test condition)
{
statement-block1;
}
Else
{
statement-block2;
}

If else statement..contd
In the if-else statements, if the condition is
true, then block 1 is executed, and then
execution continues after the end of block 2; if
the condition is false, then block 2 is executed
(block 1 is skipped), and then execution
continues after the end of block 2.
3. Nesting of if and If else statement
The if-else statement allows a choice to be made
between two possible alternatives. Sometimes a
choice must be made between more than two
possibilities. Which ca be achieved by using
nested if block and nested if-else block.

Nested if statement
The General syntax:
If (condition1)
{
if (condition2)
{
statement(s);
}
}
Statement-x.

Nested if statement..contd
If the condition1 is true than only the outer if
body will execute to check the condition2
otherwise statement-x will get executed.
Nested If else statement..contd
If(test condition 1)
{
If(test condition 2)
{
statement-block-1;
}
Else
{
statement-block-2;
}
Else
{
statement block-3;
}


Nested If else statement.contd
If the test expression is true, it will execute the
code before else part but, if it is false, the
control of the program jumps to the else part
and check test expression 1 and the process
continues. If all the test expression are false
then, the last statement is executed
4. else If statement
An extension of the nesting of if else is
the else if statement. What the nested if-else
can do is can be done by else if statement.

else If statement..contd
The general syntax

if (condition 1)
{
// block 1
}
else if (condition 2)
{
// block 2
}
...
else if (condition N)
{
// block N
}
else
{
// Optional block to be executed if all of the
// previous conditions evaluated to false
}
In case of else if only one of the blocks will be
executed. This includes the block
corresponding to the optional else part
if one of the previous blocks executes, then
the rest is skipped, and execution continues
after the end of the else block, or after the
last else if block, in the cases where there is
no else.
else If statement..contd
5. Switch case:
The switch statement provides a convenient
mechanism to execute one out of several
possible fragments of code, depending on the
value of an integral variable or expression.
A typical example that illustrates the use of
the switch statement is handling a menu
selection.

Switch case..contd:
Switch(integer expression/char expression)
{
case cont-1:
statement-block1;
Break;
-------------------;
-------------------;
default:
statement-block;
Break;
}
Statement-a;
Goto statement
A goto statement provides an unconditional
jump from the goto to a labeled statement in
the same function.
Goto statement
Goto label;
-----------------;
-----------------;
Label:
Statement-1;
statement-2;

Goto statement..contd
Where label is an identifier that identifies a
labeled statement. A labeled statement is any
statement that is preceded by an identifier
followed by a colon (:).
Use of goto statement is highly discouraged
because it makes difficult to trace the control
flow of a program, making the program hard
to understand and hard to modify.
The Conditional Operators(ternary operator):
The conditional operators ? And : are sometimes called
Ternary Operators, since they take three arguments.Their
general form is:
Expression 1 ? Expression 2 : Expression 3
ie.
condition ? value_if_true : value_if_false

The end of part-1

You might also like