Chap 5
Chap 5
Branching
Introduction
• C language possesses decision making capabilities by supporting the
following statements:
1. if statement
2. switch statement
3.Conditional operator statement
4. goto statement
Decision making with if statement
• The if statement is a powerful decision-making statement and is used
to control the flow of execution of statements.
• It takes the following form:
if(test expression)
• It allows the computer to evaluate the expression first and then,
depending on whether the value of the expression(relation or
condition) is ‘true’ ( or non zero) or ‘false’ (or zero),it transfer the
control to a particular statement.
• The if statement may be implemented in different forms depending
on the complexity of conditions to be tested.
• The different forms are:
• Simple if statement
• If……..else statement
• Nested if………else statement
• Else if ladder
Simple if statement
• The general form of a simple if statement is
if(test expression)
{
statement block;
}
statement x;
• The statement block may be 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 execution will jump to
the statement x.
Q1. Write a program to read four values a,b,c, and d from the terminal
and evaluates the ratio of (a+b) to (c-d) and prints the result, if c-d is
not equal to zero.
Q2. Write a program to read weights and heights of 10 students and
count the number of students whose weight is greater than 50 and
height is greater than 170.
The 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,
immediately following the if statements are executed otherwise the
false block statement(s) are executed.
• In either case, either true block or false block will be executed, not
both.
Q. Draw the flowchart to represent if….else statement
• Q1. Write a program to read a number and check whether the
number is even or odd
• Q2. Write a program to read a number and check whether the
number is positive number or negative.
Nesting of if……else statement
• When a series of decisions are involved, we may have to use more than one
if….else statement in nested form as shown below:
if(test condition1)
{
if(test condition2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement-3;
}
Statement-x;
• The logic of execution is below:
• If the condition1 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 the
statement-2 will be evaluated and then the control is transferred to the
statement x.
• Draw the flowchart to represent the nesting if…….else statement
• Q1. Write a C program to print the largest of three numbers.
• Q2. Write a C program to find the second smallest of 5 numbers.
The else if ladder
• There is another way of putting ifs together when multipath decisions
are involved.
• A multipath decision is a chain of ifs in which the statement
associated with each else is an if.
• It takes the following general form:
if(condition1)
{
statement-1;
}
else if(condition2)
{
statement-2;
}
else if(condition3)
{
statement-3;
}
else
{
default-statement;
}
statement;
• 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 conditions becomes false, then the final else containing
the default statement is executed.
• Q1. Write a C program to make a calculator using else if ladder.
• Q2. Write a C program to read customer number and power
consumed and prints the amount to be paid by the customer.
Electricity department charges its customers as below:
Consumption Unit Rate of Charge
0-200 Rs. 0.50 per unit
201-400 Rs. 100 plus Rs. 0.65 per unit excess of 200
401-600 Rs. 230 plus Rs. 0.80 per unit excess of 400
601 and above Rs. 390 plus Rs 1.00 per unit excess of 600
The switch statement
• When there is one of the many alternatives is to be selected, if
statement can be used to control the selection
• However, the complexity of such a program increases dramatically
when the number of alternatives increases.
• The program becomes difficult to read and follow.
• Fortunately, 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 of switch state is given below:
switch(expression)
{
case value-1:
block1
break;
case value-2:
block2
break;
……………….
……………….
default:
default-block
break;
}
Statement x;
• Rules of the switch case statement
• Following are some of the rules that we need to follow while using
the switch statement:
• In a switch statement, the “case value” must be of “char” and “int” type.
• There can be one or N number of cases.
• The values in the case must be unique.
• Each statement of the case can have a break statement. It is optional.
• The default Statement is also optional.
• Draw the flowchart of switch statement
• How switch Statement Work?
• The working of the switch statement in C is as follows:
Step 1: The switch variable is evaluated.
Step 2: The evaluated value is matched against all the present cases.
Step 3A: If the matching case value is found, the associated code is executed.
Step 3B: If the matching case is not found, then the default case is executed if
present.
Step 4A: If the break keyword is present in the case, then program control
breaks out of the switch statement.
Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
Step 5: Statements after the switch statement are executed.
• Q1. Write a C Program to print the day of the week using a switch
case.
• Q2. Write a C program to make a calculator using a switch case.
The Goto statement
• C supports goto which is unconditional branch statement.
• The goto requires a label in order to identify the place where the
branch is to be made.
• A label is a valid variable name, and must be followed by colon.
• The label is placed immediately before the statement where the
control is transferred.
• The general form of goto and label statements are shown below: