CPR Notes - Chapter 02 Decision Making and Branching
CPR Notes - Chapter 02 Decision Making and Branching
Chapter 02
Decision Making and Branching
Lectures allotted: 12
Marks Given: 24
Contents:
a) The if statement
b) The if - else statement
c) Nested if – else
d) The else – if ladder
[Ref. 1a, 2a]
The if Statement
if (condition)
{
//execute these statements;
}
Here, the keyword ‘if’ tells the compiler that what follows, is a decision
control instruction. The condition following the keyword if is always enclosed
within a pair of parentheses. If the condition is true, then the statements in
the parenthesis are executed. It the condition is not true then the statement
is not executed instead the program skips this part. The condition in C is
evaluated using C’s relational operators as well as logical operators. These
operators help us to build expression, which are either true or false.
For example:
X == Y X is equal to Y
X != Y X is not equal to Y
X < Y X is less than Y
X > Y X is greater than Y
X <= Y X is less than or equal to Y
X >= Y X is greater than or equal to Y
Demonstration of if statement:
/* Example 2.1 */
#include<stdio.h>
main( )
{
int num;
printf(“Enter a number less than 10 :”);
scanf(“%d”, &num);
if(num < = 10)
printf(“The number is less than 10”);
}
Sample output of this program will be:
Enter a number less than 10 : 7
The number is less than 10
true condition
false
Statement-1 Statement-2
Statement-3
if (i > 1 || j!=5)
{
printf("i = %d “, i );
}
[Ref. 2b]
Multiple Statements within if
In this program, value of cy and yoj are taken from user i.e. 2006 and
2001. Value of yos will become 5. The condition within ‘if’ will check whether
the value of yos is greater than 3 or not. Here, condition will become true
and the statements followed by if are executed.
if(condition)
{
//statements1
}
else
{
//statements2
}
da = bs * 90/100;
}
else
{
hra = 500;
da = bs * 98/100;
}
gs = bs+hra+da;
printf(“gross salary = Rs. %f”, gs);
}
true condition
false
Statement-1 Statement-2
Statement-3
Note:
1. Statements inside the ‘if’s curly braces are called as ‘if block’ and
statements for else’s curly braces are called as ‘else block’.
2. There is no necessity to have an else for if but, each else must match
an if statement else there will be a compiler error. The error will be
‘Misplaced else’.
3. If there is only one statement inside ‘if’ as well as ‘else’ block, we can
eliminate the curly braces.
Syntax:
if(condition)
{
//statements1
if (condition)
{
//statements2
}
else
{
//statements3
}
}
else
//statements4
Here, the inner condition executed only when the outer condition of ‘if’
is true. This hierarchy of nesting of ‘if’ can be extended in deep with any
number of ‘if-else’ statements.
false
condition1
true
Statements-4
Statements-1
false
condition2
true
Statements-3
Statements-2
/* Example 2.4 */
#include<stdio.h>
main( )
{
int y;
printf(“Enter year : “);
scanf(“%d”, &y);
if(y > 2000)
{
printf(“\n Number is greater than 2000”);
if(y % 4 == 0)
{
printf(“\n This is leap year”);
}
else
printf(“\n This is not leap year”);
}
else
{
printf(“\n Number is less than 2000);
if(y % 4 ==0)
{
printf(“\n This is leap year”);
}
else
printf(“\n This is not leap year”);
}
Output:
Enter year : 1988
Number is less than 2000
This is leap year
b) if (condition)
{
//perform this;
//as well as this;
}
c) if (condition)
//perform this;
else
//perform this;
d) if (condition)
{
//perform this;
//as well as this;
}
else
{
//perform this;
//as well as this;
}
e) if (condition)
{
//if (condition)
//perform this;
}
else
{
//perform this;
//as well as this;
}
f) if (condition)
{
if (condition)
//perform this;
else
//perform this;
}
else
{
if (condition)
//perform this;
else
//perform this;
}
[Ref. 2h]
Using Logical operators
As we have already seen the logical operators. The basic use of these
is related to if – else statement. These conditional operators are:
a) && (AND operator)
b) || (OR operator)
c) ! (NOT operator)
When, we want to specify more than one condition in the single if
statement we can use logical operators among these conditions. Though,
there are two & (and) in the operator it is pronounced as AND and || as OR.
&& Operator is used when there is necessity that both the conditions to be
true. || Operator is used when there is necessity that any one conditions to
be true. e.g.
printf(“Hello”);
}
! (a > 10)
This means that whether ‘a’ is not greater than 10. In other words, it
can be written as (a <= 10).
false
condition
Statement-1
false
true condition
Statement-2
true
Statement-3
Default
Statements
/* Example 2.5 */
#include<stdio.h>
main( )
{
int a, b, c, d, e, per;
printf(“Enter marks of four subjects : “);
scanf(“%d %d %d %d”, &a, &b, &c, &d);
if(a < 40 || b < 40 || c < 40 || d < 40)
printf(“\n You have failed in one subject”);
per = (a + b + c + d) / 4;
if(per > = 60)
printf(“\n First class”);
else
{
if (per <=59 && per >=55)
printf(“\n Higher second class”);
else
if(per <=54 && per >=40)
printf(“Second class);
else
printf(“\n Fail”);
}
}
This statement will check whether marks of any one subjects is less
than 40 or not? If it is, it will execute the statement followed by ‘if’ i.e.
switch(variable)
{
case value-1:
//statements1;
break;
case value-2:
//statements2;
break;
case value-3:
//statements3;
break;
- - - - -
- - - - -
default:
//default-statements;
break;
}
statements-x;
In the above given syntax, value-1, value-2, value-3…. are the set the
constants. When the value of the variable given in the switch brackets is
matched with any one of these values, the particular set of statements are
executed and then program control transfers out of the switch block. For
example, if value of variable is matched with ‘value-2’ then statements2 are
executed and the break statement transfers the program control out of the
switch. If the match is not found, the ‘default’ block is executed. Remember,
statements-x are executed in any case. Generally, switch case are executed
for menu selection problems.
For example:
/* Example 2.6 */
int number = 2;
switch(number)
{
case 0 :
printf("None\n");
break;
case 1 :
printf("One\n");
break;
case 2 :
printf("Two\n");
break;
case 5 :
printf("Several\n");
break;
default :
printf("Many\n");
break;
}
Variable
Value1 default
Value2
Next Statement
OR
Variable
Value1
Statements1
Value2
Statements2
Value3
Statements3
default
default-block
Statements-x
[Ref. 1i]
[Ref. 1j]
Rules of switch statement
while(condition)
{
//loop statements or body of loop
}
false
condition
true
Loop body
Statements after
the loop
See one more practical example of ‘while’ loop. That is, if we want to
find the addition of all the numbers from 10 to 20. It can be written as:
do
{
//loop statements or loop body
}
while(condition);
Loop body
true
condition
false
Statements after
the loop
See one more practical example of ‘do-while’ loop. That is, if we want
to find the addition of all the numbers from 10 to 20. It can be written as:
The ‘for loop is an entry controlled loop structure which provides more
concise loop structure having following general form:
int x;
for(x = 0; x < 10; x++)
{
printf(“%d”, x);
}
This for loop is executed for 10 times and prints the 0 to 9 digits in one
line. In short the statements are executed in following sequence:
1. x = 0;
2. if(x<10)
print value of x on screen
3. increment value of x by one
4. if(x<10)
print value of x on screen
5. increment value of x by one
……….
When the condition becomes false, the loop gets terminated.
For example if we want to print all the even numbers from 1 to 25 it can be
written using ‘for’ loop as:
int i;
for(i = 1;i < 25; i++)
{
if(i%2 == 0)
printf(“\n%d”, i);
}
Initialization
false
condition
true
Increment/
decrement Loop body
Statements after
the loop
/* Example 2.7 */
#include<stdio.h>
main()
{
int fact, num;
printf("Enter the number: ");
scanf("%d", &num);
for(fact=1 ; num>0 ; num--)
fact = fact * num;
printf("Factorial: %d", fact);
}
[Ref. 1o]
Additional features of ‘for’ loop
The ‘for’ loop have lot of capabilities that are not easily found in other
loop constructs. These are listed below:
a. More than one variable can be initialized at a time in the ‘for’ statement.
For example:
p = 1;
for(n = 0;n<10;n++)
for(p=1,n=0;n<10;n++)
b. Like initialization section, the increment section can also contain more
than one increment or decrement statements.
For example:
c. The condition may have any compound relation and the testing need not
be only loop control variable such as–
sum=0;
for(i=1; i<20 && sum<100; i++)
{
sum = sum + i;
}
m = 5;
for( ; m!=100 ; )
{
printf(“%d\n”, m);
m = m + 5;
}
f. Giving a semicolon at the end of ‘for’ will not give any syntactical error
but, all the iterations of the loop will be taken place. (Note: iteration is
one execution of the loop). Such as–
for(i=0;i<100;i++);
This loop will be executed for 100 times without any output.
[Ref. 1p, 2r]
Nesting of the loops
The nesting may be continued up to any desired level. Here, for each
iteration of outer loop inner loop completes its all iterations. In above
example, the inner loop statements will be executed for 10 X 10 = 100
number of times. See one application of this concept. For example we
require following output:
*
**
***
****
In this case, the loops must be nested as shown below:
/* Example 2.8 */
#include<stdio.h>
main()
{
int x,y;
for(x = 0 ; x <= 3 ; x++)
{
for( y = 0 ; y <= x ; y++)
printf("*");
printf("\n");
}
}
[Ref. 1q]
Jumps in the loop
break;
Flowchart
false
condition
true
break;
Loop body
Statements after
the loop
The dotted line shows the path where the program control will transfer.
For example:
/* Example 2.9 */
int x;
for(x = 10;x < 20; x++)
{
if(x%4==0)
break;
printf(“%d\n”);
10
11
continue;
Flowchart:
false
condition
Statements after
the loop
The dotted line shows the path where the program control will transfer.
For example:
/* Example 2.10 */
int x;
for(x = 10;x < 20; x++)
{
if(x%4==0)
continue;
printf(“%d\n”);
}
It has skipped the numbers which are divisible by 4, for which we have
skipped the printf statement.
-----------------------
References
1. Programming in ANSI C
by, E Balagurusamy, Tata McGraw Hill, 4th Edition
a. Chapter 5, Topic 5.2, Page No. 114
b. Chapter 5, Figure 5.2, Page 116
c. Chapter 5, Topic 5.4, Page 119
d. Chapter 5, Figure 5.5, Page 120
e. Chapter 5, Topic 5.5, Page 122
f. Chapter 5, Topic 5.6, Page 126
g. Chapter 5, Figure 5.9, Page 127
h. Chapter 5, Topic 5.7, Page 129
i. Chapter 5, Figure 5.11, Page 131
j. Chapter 5, Page 133
k. Chapter 6, Topic 6.1, Page 152
l. Chapter 6, Topic 6.2, Page 154
m. Chapter 6, Topic 6.3, Page 157
n. Chapter 6, Topic 6.4, Page 159
o. Chapter 6, Page 161
p. Chapter 6, Page 164
q. Chapter 6, Topic 6.5, Page 166
r. Chapter 6, Topic 6.5, Page 167
s. Chapter 6, Page 171
2. Let us C
By, Yashwant Kanetkar, BPB Publications, 7th Edition
a. Chapter 2, Page 51
b. Chapter 2, Page 56
c. Chapter 2, Page 58
d. Chapter 2, Page 59
e. Chapter 2, Figure 2.5, Page 60
f. Chapter 2, Page 61
g. Chapter 2, Page 62
h. Chapter 2, Page 64
i. Chapter 2, Page 67
j. Chapter 4, Page 136
k. Chapter 3, Page 98
l. Chapter 3, Page 99
m. Chapter 3, Figure 3.2, Page 101
n. Chapter 3, Page 121
o. Chapter 3, Figure 3.5, Page 121
p. Chapter 3, Page 108
q. Chapter 3, Figure 3.4, Page 111
r. Chapter 3, Page 114
s. Chapter 3, Page 118
t. Chapter 3, Page 120
tbkute@gmail.com