Module 6 – Control Flow: Branching
and Looping – Part 1
BITS Pilani Dr. Jagat Sesh Challa
Pilani Campus
Department of Computer Science & Information Systems
Module Overview
• Need for additional programming constructs
• Control Flow: Branching
• Branching: if statement
• Branching: if ladder
• Branching: if-else statement
• Branching: if-else ladder
• Branching: nested if-else statement
• Control Flow: Ternary Operator
• Control Flow: Switch Statement
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Need for additional Programming
Constructs
Conditionals, Statements and Blocks
Fibonacci Numbers Definition
Consider the following mathematical definition:
0 𝑖𝑓 𝑛 = 0
𝐹! = # 1 𝑖𝑓 𝑛 = 1
𝐹!"# + 𝐹!"$ 𝑖𝑓 𝑛 > 1
• What additional constructs do we need to define such things in
C language?
o A way to specify the condition
⎯ Example: 𝑖𝑓 𝑛 = 0
o A way to selectively choose different blocks of statements
depending on the outcomes of the condition check
⎯ Example: 𝐹! = 0 when 𝑛 = 0 and 𝐹! = 1 when 𝑛 = 1
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Statements and Blocks
Statement:
An expression followed by a semi-colon
Examples: c = a + b; i++; Printf(“Hello World”);
Block:
Set of statements enclosed inside a set of braces { and }
Example: {
c = a+b;
c++;
printf(“c is %d”, c);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Control Flow: Branching
Control Flow: Branching
• Different set of instructions gets executed depending on the
outcome of a conditional expression
• Writing conditional expression
• Using relational operators such as ==, >= , <=, !=, <, >
• Using logical operators and : &&, ||, !
• Examples of conditional expressions:
(x+y >= 10)
(marks >= 90 && marks <= 100)
(no_of_transaction >= 5 && city == “Metropolitan”)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Control Flow: Branching
• Outcome of the condition
o Non- Zero or true
o Zero or false
Examples:
int x = 5, y = 10;
(x + y <= 20) è true
int Marks = 95;
(Marks <= 100 && Marks >= 90) è true
(x & y) è zero
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Branching: if Statement
Branching: if statement
• if(condition)
statement;
• if(condition)
{
statement1;
statement2;
Block of statements
……
statementn;
}
• statement or block of statements gets executed only if the condition
evaluates to true or non-zero
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Branching: if statement
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 1
int main() O/P:
{ Enter a number
int a; 10
printf(“Enter a number”); Number is positive
Rest of the program
scanf(“%d”, &a);
if( a > 0 )
{
printf(“Number is positive\n”);
}
printf(“Rest of the program”);
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 2
int main() O/P:
{ Enter a number
int a; -5
printf(“Enter a number”); Rest of the program
scanf(“%d”, &a);
if( a > 0 )
{
printf(“Number is positive\n”);
}
printf(“Rest of the program”);
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Branching: if ladder
Branching: if ladder
A single program can have more than one If statement
if (condition1) {
//These statements would execute if the condition_1 is
true
}
if(condition2) {
//These statements would execute if the condition_2 is
true
}
.
.
.
if (conditionn) {
//These statements would execute if the nth condition
is true
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
int a;
O/P:
printf(“Enter a number”);
Enter a number
scanf(“%d”, &a);
0
You entered zero
if( a > 0 ) {
Rest of the program
printf(“Number is positive\n”);
}
if( a < 0 ) {
printf(“Number is negative\n”);
}
if( a == 0 ) {
printf(“You entered zero\n”);
}
printf(“Rest of the program”);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise
Write a C program using only If statements to find the larger
value among two numbers
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Branching: if-else statement
Branching: if-else Statement
if(condition) if(condition)
{ // statement;
// If- block of statements else
} // statement;
else
{
// else-block of statements
}
If condition inside the If parentheses is true then If- block of statements
is executed, else else-block of statements is executed
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
int a; O/P:
printf(“Enter a number”); Enter a number
scanf(“%d”, &a); 5
if( a > 0 ) Number is Positive
Rest of the program
{
printf(“Number is positive\n”);
}
else
{
printf(“Number is either negative or zero\n”);
}
printf(“Rest of the program”);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
int a; O/P:
printf(“Enter a number”); Enter a number
-5
scanf(“%d”, &a);
Number is either
if( a > 0 ) negative or zero
{ Rest of the program
printf(“Number is positive\n”);
}
else
{
printf(“Number is either negative or zero\n”);
}
printf(“Rest of the program”);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 3
#include <stdio.h>
int main() {
int a = 10, b = 4, c = 10, d = 20;
if (a > b && c == d)
printf("a is greater than b AND c is equal to d\n");
else
printf("AND condition not satisfied\n");
if (a > b || c == d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal to d\n”);
if (!a) printf("a is zero\n");
else printf("a is not zero"); Output:
AND condition not satisfied
return 0; a is greater than b OR c is equal to d
} a is not zero
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Branching: if-else ladder
Branching: if-else ladder
if (condition1){
//These statements would execute if the condition1 is true
}
else if(condition2){
//These statements would execute if the condition2 is true
}
else if (condition3) {
//These statements would execute if the condition3 is true
}
.
.
else {
/* These statements would execute none of the previous
condition is true */
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
Write a C program using If-else to find the grade of a student
based on following conditions
1. Marks greater than 90 implies grade A
2. Marks between 80 and 90 implies grade B
3. Marks between 70 and 80 implies grade C
4. Else, Grade is Fail
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example: Solution
if(Marks > 90)
printf(“Grade is A”);
else if(Marks <= 90 && Marks >= 80)
printf(“Grade is B”);
else if(Marks <= 80 && Marks >= 70)
printf(“Grade is C”);
else
printf(“Grade is FAIL”);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise
Write a code to find whether a character entered is a digit,
uppercase alphabet, lowercase alphabet or any other special
character
Note: ASCII value of digits à 48 to 57
ASCII value of A à 65
ASCII value of a à 97
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Branching: Nested if-else statement
Branching: Nested if-else
statement
If one or more if and/or else statement is/are present inside the body
of another “if” or “else”
if (condition1)
{
if (condition2)
// statement or block of statements for if
else
// statement or block of statements for else
}
else{
if (condition3)
// statement or block of statements for if
else
// statement or block of statements for else
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
int a, b, c; else
printf(“Enter the numbers”); {
scanf(“%d%d%d”, a, b, c); if(b > c)
printf(“B is the largest”);
if(a > b) else
{ printf(“C is the largest”);
if(a > c) }
printf(“A is the largest”); printf(“End of the program”);
else return 0;
printf(“C is the largest”);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Control Flow: Ternary Operator
Ternary Operator
• An alternative way to write if…else construct:
if (expr1)
expr2 ;
else
expr3 ;
• Syntax: expr1? expr2:expr3
• Only one of expr2 and expr3 is evaluated
• If expr2 and expr3 are of different types, the type of the
result is determined by the type conversion rules.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
A = 43, B = 7, C = 0, D = 0
A + B == 50 ? C = 10 : C = 15
– O/P: C = 10
A > C ? printf("Hello"): printf("World");
– O/P: ?
A < C ? printf("Hello"): printf("World");
⎯ O/P: ?
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise
Write a statement (using ternary operator) to find out the largest
of the three integers a, b and c, and store the value in max.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Control Flow: Switch Statement
Switch Statement
• A multi-way decision that tests whether an expression matches one
of a number of constant integer values, and branches accordingly.
Syntax:
switch (expr) {
case const-expr : statements
break;
case const-expr : statements
break;
default: statements
break;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 1
#include <stdio.h>
int main() {
What is the
int language = 10; output?
switch (language) {
case 1: printf("C#\n");
break;
case 2: printf("C\n");
break;
case 3: printf("C++\n");
break;
default:
printf("Other programming language\n");
}
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 2
#include <stdio.h>
int main() {
int number=5; What is the
switch (number) { output?
case 1:
case 2:
case 3:
printf("One, Two, or Three.\n");
break;
case 4:
case 5:
case 6:
printf("Four, Five, or Six.\n");
break;
default:
printf("Greater than Six.\n");
}}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus