Download - Unit 3 Part1
-
8/11/2019 Unit 3 Part1
1/27
Flow of controls
Unit :3
Part-1
(decision control statements)
-
8/11/2019 Unit 3 Part1
2/27
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.
-
8/11/2019 Unit 3 Part1
3/27
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.
-
8/11/2019 Unit 3 Part1
4/27
Decision Control Statements
C++ language is able to perform different sets of
actions depending on the circumstances. The threemajor decision making instructions are :-
The If Statements
If-else Statements
The Switch Statements
Many times, we want a set of Instructions to beexecuted 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.
-
8/11/2019 Unit 3 Part1
5/27
Conditions
Conditions are expressions that evaluate to
a booleanvalue atrue or false value(trueandfalseare C++ keywords, representing
the two possible values of a boolean
expression or variable). Simple conditionsinvolve 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).
-
8/11/2019 Unit 3 Part1
6/27
Composite Conditions
Very often, we encounter situations where the condition
can not be expressed as the simple conditions from theprevious section, just by comparing two values. An exampleof such situations is testing if a number is within agiven rangeof values; for instance, testing if a number isbetween 0 and 10.
This example involves testing twoconditions, and verifyingif the two conditions are simultaneously met. In otherwords, we want to test if the number is greater than orequal to 0 andalso less than or equal to 10.
To this end, we use logical operators to combine two
conditions. In the above example, we use the logicalAND operator &&, as shown below:
For ex: if (0>= number && number
-
8/11/2019 Unit 3 Part1
7/27
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.
-
8/11/2019 Unit 3 Part1
8/27
1. The if Statement.
The general syntax:
If(test condition)
{
Statement -block1;
Statement -block2;
----------;
----------;}
Statement-x;
-
8/11/2019 Unit 3 Part1
9/27
The if Statement.contd
If the condition returns true value the all thestatements inside the brace of if block are
executed.otherwise,if the condition return
false, then the statement outside the if block
is executed.
-
8/11/2019 Unit 3 Part1
10/27
2. Ifelse statement
The simple ifstatement 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 if elsestatement.
-
8/11/2019 Unit 3 Part1
11/27
Ifelse statementcontd
The general syntax:
If(test condition)
{
statement-block1;
}
Else
{
statement-block2;
}
-
8/11/2019 Unit 3 Part1
12/27
-
8/11/2019 Unit 3 Part1
13/27
3. Nesting of if and Ifelse 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 usingnested if block and nested if-else block.
-
8/11/2019 Unit 3 Part1
14/27
Nested if statement
The General syntax:
If (condition1)
{
if (condition2)
{
statement(s);
}
}
Statement-x.
-
8/11/2019 Unit 3 Part1
15/27
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.
-
8/11/2019 Unit 3 Part1
16/27
Nested Ifelse statement..contdIf(test condition 1)
{
If(test condition 2)
{
statement-block-1;
}Else
{
statement-block-2;
}Else
{
statementblock-3;
}
-
8/11/2019 Unit 3 Part1
17/27
Nested Ifelse 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 processcontinues. If all the test expression are false
then, the last statement is executed
-
8/11/2019 Unit 3 Part1
18/27
4. else If statement
An extension of the nesting of if else is
the else ifstatement. What the nested if-else
can do is can be done byelse if statement.
-
8/11/2019 Unit 3 Part1
19/27
else If statement..contdThe 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
}
-
8/11/2019 Unit 3 Part1
20/27
In case of else if only oneof the blocks will be
executed. This includes the blockcorresponding to the optional elsepart
ifone of the previous blocks executes, then
the rest is skipped, and execution continuesafter the end of the elseblock, or after the
last else ifblock, in the cases where there is
no else.
else If statement..contd
-
8/11/2019 Unit 3 Part1
21/27
5. Switch case:
The switchstatement 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 switchstatement is handling a menu
selection.
S it h td
-
8/11/2019 Unit 3 Part1
22/27
Switch case..contd:
Switch(integer expression/char expression)
{case cont-1:
statement-block1;
Break;
-------------------;
-------------------;
default:
statement-block;Break;
}
Statement-a;
-
8/11/2019 Unit 3 Part1
23/27
Goto statement
A gotostatement provides an unconditional
jump from the goto to a labeled statement in
the same function.
-
8/11/2019 Unit 3 Part1
24/27
Goto statement
Goto label;
-----------------;
-----------------;
Label:Statement-1;
statement-2;
-
8/11/2019 Unit 3 Part1
25/27
Goto statement..contd
Where labelis 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 gotostatement is highly discouraged
because it makes difficult to trace the control
flow of a program, making the program hardto understand and hard to modify.
-
8/11/2019 Unit 3 Part1
26/27
The Conditional Operators(ternary operator):
The conditional operators ? And : are sometimes calledTernary Operators, since they take three arguments.Their
general form is:
Expression 1 ? Expression 2 : Expression 3
ie.
condition ?value_if_true :value_if_false
-
8/11/2019 Unit 3 Part1
27/27
The end of part-1