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

Std-10-Computer-Chapter 13 Decision Structures

Ch 13 computer

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Std-10-Computer-Chapter 13 Decision Structures

Ch 13 computer

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 13 Computer

Decision Structures Class 10


INTRODUCTION
 The programs follow the sequential structure of execution.
 The instructions are executed one by one in sequentially in the order in which they
appear in a program.
 C language provides facilities through special kind of statements that can change the
flow of sequence of instructions in the program are called decision structure statements.
 Decision Structure statements help us to jump from one part of the program to another
part of program based on result of some conditions.
 Sometimes decision structure statements are also known as selective structure
statements, branching statements or decision making statements.
 Statements which are controlling the flow of execution, they are also known as control
statements.
 C language provides two basic types of decision structure statements.
o If statement
o Switch statement

If Statement (if statement is the type of decision structure)


 The if statement is one of the powerful decision making statement which can be used to
transfer control of instruction execution.
 If statement can be used in four different ways:
o Simple if statement
o if...else statement
o nested if statement
o else-if ladder statement

Simple if statement
 The simplest way of decision structure is the if statement.
 If statement is used in the program for decision making and to change the flow of
program execution.
Syntax:
if(test expression)
{
Statement-block;
}
Some next instruction of the program;
 The logical operators are used in test expression.
 Note: test expression should not end with a semi-color (;)
 If the test expression is true then executes the statement-block and if the test expression
is false then computers skips the statement-block and executes the next instruction of
the program.
 C programming language assumes any non-zero and non-null values as true and if it
either zero or null then it is assumed as false value.
Presented by Nuzhat Ibrahim Memon 1
if….else statement
 In simple if statement there is only one statement block, which gets executed only when
the test expression is true.
 Simple if statement takes no action in case when the test expression is false.
 Many times, there is a need to takes an action when the test expression is true as well as
when it is false, then in such cases, the if…else structure is useful.
 if..else is a two way statement:
o If the condition is true in if ... else statement if part is executed.
o If the condition is false in if...else statement else part is executed

Syntax:
if(test expression)
{
true Statement-block;
}
else
{
false Statement-block;
}

 If test expression is evaluated to true, then flow of control is transferred to true-


statement-block .
 In case when the test expression is evaluated to false, then flow of control is transferred
to the next statement in a sequence of the program.

Nested if….else statement


 The nested if statement is used to execute series of decisions in our C program.
 Nested if statement can be used to find out the largest of given three values.
Syntax:
CASE 1: CASE 2:
if(test expression-1) if(test expression-1)
{ {
if(test expression-2) Statement-block-1;
{ }
Statement-block-1; else
} {
else if(test expression-2)
{ {
Statement-block-2; Statement-block-2;
} }
} else
else {
{ Statement-block-3;
Statement-block-3; }
} }

Presented by Nuzhat Ibrahim Memon 2


IN CASE1:
 If test expression-1 is true, then test expression-2 is checked.
 When this test expression-2 is also true, then only statement-block-1 will be executed
otherwise statement-block-2 will be executed.
 If test expression-1 is false then directly else block is going to execute and statement-
block-2 will be executed.
IN CASE2:
 A user may change this structure as per his/her needs. Nested if… else stament can be
else block even.

else... if ladder statement


 else-if ladder statement is used when many if statements together to evaluate multiple
decisions.
 When chain of if statements are used in else(false) block of nested if statements, then it
becomes else-if ladder statement
 else if ladder structure is available to evaluate more than one condition.
Syntax

if(test expression-1)
{
statement-block-1;
}
else if(test expression-2)
{
statement-block-2;
}
………………..
………………..
else if(test expression n){
statement-block-n;
}
else
{
default-statement-block;
}
program-statement-x;

 In else-if ladder the test-expression is evaluate from top to bottom


 if test_expression is true in else-if ladder,then the associated statement block is executed.
 If test expression is false in else-if ladder then control is taken to default-statement-block
of final/last else statement.

The Switch statement


 C language provides the built-in facility of multiway (multilayer) decision statement
called switch.
 When an action is to be taken based on given multiple choices, switch statement is very
useful.
Presented by Nuzhat Ibrahim Memon 3
Syntax
switch(expression){
case value1:
statement block1;
break;
case value2:
statement block2;
break;
……
……
case valuen:
statement block n;
break;
default:
default statement block;
break;
}
statement-x;

 If one option is to be selected from multiple choices switch statement is useful.


 Switch statement uses one argument (expression or variable value of type integer or
character).
 Each constant or constant expression in case option of switch statement is known as case
label.
 Each case label ends with colon (:)
 All case labels must be unique in switch statement.
 There is no need to use braces around the statement blocks (statement block1,
statement block2 and so on).
 When switch statement is executed, it first evaluated the value of expression and then
compares it with case constants from top to bottom.
 When a case found whose value is matching with the value of expression, then the
corresponding statement block of that case is executed.
 After each statement-block in switch, break statement signals the end of case causing
the exit from switch statement.
 The default is an optional case.
 If the given value does not match with any expression in switch, default is executed.
 In switch statement, the default statement can be used only once.
 Default statement can be placed anywhere inside switch but usually we place it at last
inside the switch.

Compound Relational Test


 Compound relational test means multiple test expression.
 The compound relational test is simple one or more relational test joined together by
either the logical AND or logical OR operators.
 logical AND operator is written as &&.
 logical OR operator is written as ||.
 Use of compound relational test will help us to reduce number of if…else statements in
our program.
Presented by Nuzhat Ibrahim Memon 4

You might also like