[go: up one dir, main page]

0% found this document useful (0 votes)
15 views4 pages

CH 13

Chapter 13 discusses decision structures in C programming, which allow for conditional execution of statements rather than a strict sequential flow. It introduces two primary decision structures: the 'if' statement, which includes variations such as simple if, if-else, nested if, and else-if ladder, and the 'switch' statement for handling multiple choices. Additionally, it covers the concept of compound relational tests to streamline decision-making processes in programs.

Uploaded by

leuvakartikey
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)
15 views4 pages

CH 13

Chapter 13 discusses decision structures in C programming, which allow for conditional execution of statements rather than a strict sequential flow. It introduces two primary decision structures: the 'if' statement, which includes variations such as simple if, if-else, nested if, and else-if ladder, and the 'switch' statement for handling multiple choices. Additionally, it covers the concept of compound relational tests to streamline decision-making processes in programs.

Uploaded by

leuvakartikey
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

Decision Structures
Need for Decision Structures in C:-
 Most of the C programs presented the sequential structure ( mean instructions are executed one by one)
in a program.
◦ But sometime, we may want to execute some of the statements based on some conditions. For that, it
is required to change the flow of the sequence of instructions.
▪ C language provides facility through special kind of statements that can change the flow of a
sequence of instructions in the program. Such statements 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 structures statements, branching
statements or decision making statements.
 As these statements are controlling the flow of execution, they are also known as control statements.

C language provides two basic types of decision structure statements:


1. if
2. switch.

The if Statement:-
, can be used to transfer control of instruction execution.
◦ The if statement can be used in following different ways:
1. Simple if statement
2. if-else statement
3. nested if statement and
4. else-if ladder statement

Simple if statement:-
 It is the simplest form of the if statement.
 It is frequently used in the program for decision making and allowing us to change the flow of program
execution.

Syntax of simple if statement:-


if (test expression)
{
Statement-block;
}

 Here the test expression is any valid C language expression.


 The logical operators are used in test expression.
 The statement-block may contain any valid C language statement or statements.
 Note that test expression part should not end with a semicolon.

( The syntax says if the test expression is true then executes the statement-block and if the test expression
is false then computer 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 is either zero or
null then it is assumed as false value. )

The if ... else statement:-


 In simple if statement, there is only one statement block, which gets executed only when test
expression is true.
 Simple if statement takes no action in case when the test expression is false.
◦ But What happens if we want to take some action in case of test expression is false.
In such cases, the if.. else structure is useful.

Syntax of if...else statement:-


if (test expression)
{
True statement-block:
}
else
{
False statement-block;
}
 Here the test expression is any valid C language expression.
 When test expression is evaluated to true, the flow of control is transferred to True statement-block.
 In case when test expression is evaluated to false, the flow of control is transferred to False statement-
block.
 In both the cases, after execution of if statement the flow control is transferred to the next statement in
a sequence of the program.

Nested if...else statement:-


 There are situations where we need to execute series of decisions in our program. We may use series of
if else statement in nested form to achieve our task.

Syntax of if...else statement (nested form):-


if (test expression1)
{
if(test expression2)
{
Statement block1;
}
else
{
Statement block2;
}
else
{
Statement block3;
}

Now the logic of execution of nested if statement is as under:


 If test expression1 is true, then test expression2 is checked.
◦ If test expression2 is true then Statement block1 will be executed
▪ otherwise Statement block2 will be executed.
 If test expression1 is false, then Statement block3 will be executed.
 Note that we may also use more if...else statement as a part of Statement block3.
Note: A user may change this structure as per his/ her needs.

The else-if ladder statement:-


 There is another way of using many if statements together when we want to evaluate multiple decision.
 When chain of if statements are used in else (false) block of nested if statements, it becomes else if
ladder statement.

Syntax of else-if ladder statement:-


if (test expression1)
Statement block1;
else if (test expression2)
Statement block2:
else if (test expression3)
Statement block3;
else if (test expression n)
Statement block n;
else
Default-statement-block;
program-statement-x;

 This construct is known as else-if ladder.


 The test expressions are evaluated from the top to bottom in the ladder.
 When any test expression in a ladder evaluated to true, the associated statement block is executed.
◦ After which control is transferred to the program-statement-x skipping rest of the ladder.
 When all the test expressions are evaluated to false then control is transferred to the Default-
statement block of final else.

The Switch Statement:-


 We have used the if...else statement for selection of various statement blocks during program execution.
◦ However when we use number of if...else statement then our program becomes more complex /
program becomes difficult to read and interpret the logic.
 To simplify our program, the C language has offered the built-in facility of multiway decision statement
called switch.
◦ When an action is to be taken based on given multiple choices, switch statement is very useful

Syntax of the Switch Statement:-


switch (expression)
{
case value-1:
statement block-1
break;
case value-2:
statement block-2
break;
….....
…....
….....
case value-n:
statement block-n
break;
default:
default statement block
break;
}
statement-x;

Note the following important points related to switch statement.


 The switch statement uses one argument (expression or variable value) which is checked for equality
with number of case options listed inside the switch statement block.
◦ The argument should be either a variable or expression of type integer or character
 Each case option contains constant or constant expressions.
◦ This constant is known as case label and it ends with colon (:).
◦ Each case label must be unique.
◦ Any two case labels cannot have the same value
 Statement block-1, statement block-2 and so on are statement lists which may contain zero or more
statements.
 There is no need to use braces around this statement blocks.
 When switch statement is executed, it first evaluates 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. Then after flow of control is transferred to the next statement (statement-x) in a
program.
 The default is an optional case.
◦ When default is written and no case found during execution at that time the default statement
block is executed.
◦ The default can be used only once and may be placed anywhere inside switch but usually we place
it at last inside the switch.

Compound Relational Test:-


 The compound relational test is nothing but simple one or more relational tests joined together by either
the logical AND operators or logical OR operators.
 These logical operators are represented by the character pairs && and || respectively.
 Use of compound relational test will help us to reduce number of if...else statements in our program.

!! Vision Computer Education !!


Query to call:
satishsir.vision@gmail.com
Whatsup : +91-9409209969

You might also like