# Chapter 5 Decision Making & Branching

© Khalid Nazim S.A. Assistant Professor, Department of Computer Science & Engineering PES School of Engineering, Bangalore -100

Introduction
Many a times we encounter situations where we may change the order of execution of statements based on certain conditions, until certain conditions are met and involves a kind of decision making to see whether a particular condition has been met or not , in this context C language provides decision making capabilities with the help of following statements1.If statement 2. Switch statement 3. Conditional operator statement 4. Goto statement Since these statements control the flow / order of execution they are also referred to as control statements/ and popularly known as decision making statements

Entry

False
Test Expression

True

The if statement
 Determines whether a statement or block is executed  Implements the selection instructions within an algorithm  Decides what to do by evaluating a Boolean expression  If the expression is true (non-zero), the statement or block is executed if ( expression ) statement

What is a statement?
Statements are lines of instructions in the programs ending with a semicolon (;) A compound statement or block is a series of statements surrounded by braces {
number = number + 1; printf("%d\n", number);
}

An empty statement is a single semicolon

Example: oddnum.c
Read in a number, and print it if it is odd

output “Enter an integer” input number if (number is odd) then { output the number }

Example: oddnum.c
Read in a number, and print it if it is odd

#include <stdio.h> /* Read in a number, and echo it if it is odd */ int main() {

output “Enter an integer” input number if (number is odd) then { output the number }

return 0; }

Example: oddnum.c
Read in a number, and print it if it is odd

#include <stdio.h> /* Read in a number, and echo it if it is odd */ int main() { int number;

output “Enter an integer” input number if (number is odd) then { output the number }

printf("Enter an integer: "); scanf("%d", &number);

return 0; }

Example: oddnum.c
Read in a number, and print it if it is odd

#include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { int number;

output “Enter an integer” input number if (number is odd) then { output the number }

printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; }

Example: oddnum.c
Read in a number, and print it if it is odd

#include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { int number;

output “Enter an integer” input number Do not put

printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; }

“then” here!
if (number is odd) then { output the number }

Example: oddnum.c
Read in a number, and print it if it is odd

#include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { int number;

output “Enter an integer” input number if (number is odd) then { output the number }

printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); }

Do not return put } semicolon here!

0;

Example: oddnum.c
Read in a number, and print it if it is odd

#include <stdio.h> /* Read in a number, and echo it if it is odd. */ int main() { int number;

output “Enter an integer” input number if (number is odd) then { output the number }

printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; }

Notes on if
Common mistake
if (number % 2 != 0); { printf ("%d is an odd ", number); } printf ("number\n");

Notes on if
Common mistake
if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n"); No semicolon here!

The semicolon is an empty statement

Notes on if
Common mistake
if (number = 0) { printf ("%d\n", number); } printf ("%d\n", number);

Notes on if
Common mistake
if (number = 0) { printf("%d\n", number); } printf("%d\n", number);

Should be ==

Simple if Statement
Syntax: If (test expression) { Statement – block; } Statement –x;
Test Expression ? False Statement X True

Statement Block

Next Statement

The IF..ELSE Statement
Syntax: If (test expression) { True block Statement(s); } else { False block Statement(s); } Statement -x
True True- block Statement Statement- X Test Expression ? False False- Block Statement

Example: oddeven.c
Read in a number, and determine if it’s odd or even

#include <stdio.h> /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); }

output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

}

Example: oddeven.c
Read in a number, and determine if it’s odd or even

#include <stdio.h> /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } }

output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

Example: oddeven.c
Read in a number, and determine if it’s odd or even

#include <stdio.h> /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } }

output “Enter an integer” No input number

semicolons if (number is odd) here! then
{

output: number “ is an odd number” } else { output: number “ is an even number” }

Example: oddeven.c
Read in a number, and determine if it’s odd or even

#include <stdio.h> /* Determine whether an input number is odd or even. */ main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); } }

output “Enter an integer” input number if (number is odd) then { output: number “ is an odd number” } else { output: number “ is an even number” }

NESTING OF IF..ELSE STATEMENTS
Entry False Test Expression ? True

False

Test Expression ?

True

Statement -3

Statement -2

Statement -1

Statement- X

Next Statement

Test Expression ? Statement -1 Test Expression ? Statement -2 Test Expression ? Statement -3 Test Expression ?
Statement -x Next Statement

Statement -n

Default statement

The SWITCH Statement
Syntax: switch (expression) { case value-1: block-1 break; case value-2: block-2 break; case value-3: block-3 break; ……………………. …………………….. Default: default block break; } Statement- x;
switch Expression

Expression = value 1 block 1
Expression = value 2

block 2

(no match) default

default block Statement X