0% found this document useful (0 votes)
29 views18 pages

Decision Making and Branching

Programing in c

Uploaded by

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

Decision Making and Branching

Programing in c

Uploaded by

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

DECISION MAKING AND BRANCHING

The statements of C program are executed sequentially


i.e. one statement after another in order. We have a
number of situations where we may have to change the
order of execution of statements based on certain
conditions, or repeat a group of statements until certain
specified conditions are met. This involves a kind of
decision making to see whether a particular condition has
occurred or not and then direct the computer to execute
certain statements accordingly. For this purpose, C
provides the following statements
1. if statement
2. switch statement
3. Conditional operator statement
4. goto statement
These statements are popularly known as decision-
making statements. Since these statements ‘control’ the
flow of execution, they are also known as control
statements.

if statement
The if statement is a powerful decision-making statement
and is used to control the flow of execution of statements.
The if statement may be implemented in different forms
depending on the complexity of conditions to be tested.
The different forms are :
1. Simple if statement
2. if … else statement
3. Nested if … else statement
4. else if ladder

1. Simple if statement
The general form of if statement is
if(test_expression)
{
statement-block;
}
statement-x;
The statement-block may be a single statement or a
group of statements. If the test expression is true, the
statement-block will be executed; otherwise the statement-
block will be skipped and the execution will jump to the
statement-x. When the condition is true, both the
statement-block and the statement-x are executed in
sequence.

Flowchart
Example :
if (a>0)
printf(“Given number is Positive”);
if (a<0)
printf(“Given number is Negative”);
if (a == 0)
printf(“Give number is zero”);
Explanation
If a value is greater than zero, it will print positive.
If a value is less than zero, it will print negative. If a value
is equal to zero, it will print zero.
if … else statement
The if … else statement is an extension of the simple if
statement. The general form is
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x;
If the test expression is true, then the true-block
statement(s), immediately following the if statement are
executed; otherwise, the false-block statement(s) are
executed. In either case, either true-block or false-block
will be executed, not both. In both the cases, the control is
transferred subsequently to the statement-x.

Flowchart
Example :
if (a > b)
{
printf(“a is greater”);
}
else
{
printf(“b is greater”);
}
Explanation :

Nested if … else statement


When a series of decisions are involved, we have to
use more than one if … else statement in nested form.

If the test condition-1 is false, the statement-3 will be


executed; otherwise it continues to perform the second
test. If the condition-2 is true, the statement-1 will be
evaluated; otherwise statement-2 will be evaluated and
then the control is transferred to the statement-x.
Example :
if (a >= b)
{
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else
{
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}

Explanation :
4. The else if ladder
A multiple decision is a chain of ifs in which the
statement associated with each else is an if. The general
form is :

This construct is known as the else if ladder. The


conditions are evaluated from the top (of the ladder),
downwards. As soon as a true condition is found, the
statement associated with it is executed and the control is
transferred to the statement-x (skipping the rest of the
ladder). When all the n conditions become false, then the
final else containing the default-statement will be
executed.

Flowchart

Example :
if (n<0)
printf(“The given number is negative”);
else if (n>0)
printf(“The given number is positive”);
else
printf(“The given number is zero”);
Explanation :

#include <stdio.h>
void main()
{
int n;
printf(“Enter a number :”);
scanf(“%d”,&n);
if (n<0)
printf(“The given number is negative”);
else if (n>0)
printf(“The given number is positive”);
else
printf(“The given number is zero”);
}

Rules for Indentation


When using control structures, a statement often controls
many other statements that follow it. In such situations it
is a good practice to use indentation to show that the
indented statements are dependent on the preceding
controlling statement. Some guidelines that could be
followed while using indentation are :
● Indent statements that are dependent on the previous
statements; provide at least three spaces of
indentation.
● Align vertically else clause with their matching if
clause.
● Use braces on separate lines to identify a block of
statements.
● Indent the statements in the block by at least three
spaces to the right of the braces.
● Align the opening and closing braces.
● Use appropriate comments to signify the beginning
and end of blocks.
● Indent the nested statements as per the above rules.
● Code only one clause or statement on each line.

The switch Statement


When one of the many alternatives is to be selected, we
can use an if statement to control the selection. However,
the complexity of the program increases when the number
of alternatives increases. The program becomes difficult to
read and follow.
C has a built-in multiway decision statement known as
switch. The switch statement tests the value of a given
variable (or expression) against a list of case values and
when a match is found, a block of statements associated
with that case is executed. The general form is :

● The expression is an integer expression or character.


● value1, value-2….are constants or constant
expressions (evaluable to an integral constant) and
are known as case labels.
● Each of these values should be unique within a switch
statement.
● block-1, block-2...are statement lists and may contain
zero or more statements. There is no need to put
braces around these blocks.
● Case labels end with a colon ( : )
● When the switch is executed, the value of the
expression is successfully compared against the
values value-1, value-2…. If a case is found whose
value matches with the value of the expression, then
the block of statements that follows the case are
executed.
● The break statement at the end of each block signals
the end of a particular case and causes an exit from
the switch statement, transferring the control to the
statement-x following the switch.
● The default is an optional case. When present, it will
be executed if the value of the expression does not
match with any of the case values. If not present, no
action takes place if all matches fail and the control
goes to the statement-x. (ANSI C permits the use of
as many as 257 labels).
Flowchart :

Example :
switch(n)
{
case 1 :
printf(“ONE”);
break;
case 2 :
printf(“TWO”);
break;
case 3 :
printf(“THREE”);
break;
default :
printf(“Enter a number between 1 to 3”);
}
Explanation :

Rules for switch Statement


● The switch expression must be an integral type.
● Case labels must be constants or constant
expressions.
● Case labels must be unique. No two labels can have
the same value.
● Case labels must end with colon.
● The break statement transfers the control out of the
switch statement.
● The break statement is optional. That is, two or more
case labels may belong to the same statements.
● The default label is optional. If present, it will be
executed when the expression does not find a
matching case label.
● There can be at most one default label.
● The default may be placed anywhere but usually
placed at the end.
● It is permitted to nest switch statements.

Conditional Operator Statement - The ? : Operator


This is used for making two-way decisions.
This operator is a combination of ? and :, and takes
three operands.
This operator is popularly known as the conditional
operator.
The general form is :
conditional expression ? expression1 : expression2
The conditional expression is evaluated first. If the
result is non-zero (True), expression1 is evaluated and is
returned as the value of the conditional expression.
Otherwise (False or 0), expression2 is evaluated and its
value is returned.
Example :
a>b ? printf(“a is greater”) : printf(“b is greater);
Explanation :

The goto Statement


The goto Statement branches unconditionally from
one point to another in the program.
The goto statement requires a label in order to
identify the place where the branch is to be made. A label
is a valid identifier name, and must be followed by a colon.
The label is placed immediately before the statement
where the control is to be transferred.
The general form of goto statement is :

The label : can be anywhere in the program either


before or after the goto label : statement
When goto is met when the program is run, the flow of
control will jump to the statement immediately following
the label. This happens unconditionally.
If the label is before the goto statement, a loop will be
formed and some statements will be executed repeatedly.
Such a jump is known as a backward jump.
If the label is placed after the goto, some statements
will be skipped and the jump is known as a forward jump.
Example :

read:
scanf(“%d”,&n);
sum=sum+n;
If (n>0)
goto read;
sum=sum-n;
printf(“%d”,sum);

i) sum=0
n=5
sum=0+5=5
ii) n=5
sum=5+5=10
iii)n=10
sum=10+10=20
iv)n=15
sum=20+15=35
v)n=5
sum=35+5=40
vi)n=-5
sum=40-5=35
sum=35-(-5)=40

You might also like