Professional Documents
Culture Documents
Module 6 - Control Flow - Branching and Looping - Part 1 - CP
Module 6 - Control Flow - Branching and Looping - Part 1 - CP
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Control Flow: Branching
• Different set of instructions gets executed depending on the
outcome of a conditional expression
Examples:
(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;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Branching: if statement
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
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
if(condition) if(condition)
{ // statement;
// If- block of statements else
} // statement;
else
{
// else-block of statements
}
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 either 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
BITS Pilani
Pilani Campus
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
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
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
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
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
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Exercise
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
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