0% found this document useful (0 votes)
4 views

controlstructures (1)

Uploaded by

wutibyku
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

controlstructures (1)

Uploaded by

wutibyku
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CONTROL STRUCTURES

CONTROL STRUCTURE

Control structure is divided into


three parts:
 Selection statement

 Iteration statement
 Jumps in statement
SELECTION STATEMENT
 Selection statement is also called as
Decision making statements because
it provides the decision making capabilities to
the statements.

 In selection statement, there are two types:


if statement
switch statement

 These two statements allows you to control


the flow of a program with their conditions.
1. DECISION MAKING WITH IF
STATEMENTS
 The “if statement” is also called as
conditional branch statement.
 It is used to control program execution
Entry
through two paths.
Test false
If (test expression) Condition ?

true
 It allows the computer to evaluate the
expression first and then depending on
whether the value of the expression is
‘true’ or ‘false’, it transfers the control to
particular statement.
IF STATEMENT
 The if-statement may be implemented
in different forms depending on the
complexity of condition to be tested.
1. Simple if Statement
2. If…else Statement
3. Nested if...else Statement
4. else if ladder
SIMPLE IF STATEMENT
 The general form of a simple if statement
is…
• The ‘Statement-block’ may be
a single statement or a group of
statements.
if (test expression)
• If the test expression is true, {
the statement-block will be statement-
executed; block;
• Otherwise the statement-block }
will be skipped and the statement-x;
execution will jump to the
statement-x.
2. THE IF…ELSE STATEMENT
 The if…else statement is an extension of the
simple if statement.
• The ‘Statement-block’ may If (condition)
be a single statement or a {
true - Statement block;
group of statements. }
• If the test expression is true, else
{
the true-statement-block will false - Statement block;
be executed; }
• otherwise the false- Statement-a;
statement-block will be
executed and the execution
will jump to the statement-a.
3. NESTING OF IF…ELSE STATEMENT
 When the series of decisions are involved, we
may have to use more than one if…else
statement in nested form.
If (condition1)
{
If (condition2)
{
Statement block1;
}
else
{
Statement block2;
}
}
else
{
Statement block3;
}
Statement 4;
IF-ELSE-IF LADDER
if(condition-1)
{//if condition-1 is true }
else if (condition-2)
{//if condition-2 is true }
else if (condition-3)
{//if condition-n is true }
.
.
else if (condition-n)
{//if condition-n is true }
else {
//if none of the conditions are true.}
Statements which will be executed
always
THE SWITCH STATEMENT
 The switch statement provides another way
to decide which statement to execute next
 The switch statement evaluates an
expression, then attempts to match the result
to one of several possible cases
 Each case contains one value (a constant)
and a list of statements
 The flow of control transfers to statement
associated with the first case value that
matches
THE SWITCH STATEMENT
 The general syntax of a switch statement is:

switch switch ( expression )


and {
case case value1 :
are statement-list1
reserved case value2 :
statement-list2 If expression
words matches value2
case value3 :
statement-list3 control jumps
case ... to here

}
THE SWITCH STATEMENT
 Often a break statement is used as the last
statement in each case's statement list
 A break statement causes control to transfer
to the end of the switch statement
 If a break statement is not used, the flow of
control will continue into the next case
 Sometimes this may be appropriate, but often
we want to execute only the statements
associated with one case
THE SWITCH STATEMENT
 An example of a switch statement:

switch (option)
{
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
THE SWITCH STATEMENT
 A switch statement can have an optional
default case
 The default case has no associated value and
simply uses the reserved word default
 If the default case is present, control will
transfer to it if no other case value matches
 If there is no default case, and no other value
matches, control falls through to the
statement after the switch

You might also like