PWC Unit-2 (Decision Making and Branching)
PWC Unit-2 (Decision Making and Branching)
Subject
Basics of Logic Development
(IT3009)
Unit – 2 (Part – 2)
Decision Making and Branching
CONTENTS
1. Basics of Decision Making (Branching) Control Structures
2. Selection Statements
2.1. if Statement
2.2. if…else Statement
2.3. Nested if Statement
3. Multiway Selection Statements
3.1. if-else-if ladder (if...else if...else) Statement
3.2. switch Statement
4. Basics of Looping Statements
4.1. while Loop Statement
4.2. for Loop Statement
4.3. do-while Loop Statement
4.4. Difference between while loop and do-while loop
4.5. Nested Loops
5. Solved Programs
C programming language assumes any non-zero and non-null values as true, and
if it is either zero or null, then it is assumed as false value.
C programming language provides the following types of decision-making
statements.
• Selection Statements
o if Statement
o if-else Statement
o Nested if Statement
• Multiway Selection Statements
o if-else-if ladder (if...else if...else) Statement
o switch statements
2. Selection Statements
2.1. if Statement
An if statement consists of a boolean expression followed by one or more
statements.
Syntax –
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
If the Boolean expression evaluates to true, then the block of code inside the
'if' statement will be executed. If the Boolean expression evaluates to false, then
the first set of code after the end of the 'if' statement (after the closing curly brace)
will be executed.
Flow Diagram –
Example –
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Output −
a is less than 20
value of a is : 10
Syntax –
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}
If the boolean expression evaluates to true, then the if block of code will be
executed, otherwise else block of code will be executed.
Flow Diagram –
Example –
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
} else {
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Output −
Program –
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n % 2 == 0)
printf("%d is even",n);
else
printf("%d is odd",n);
return 0;
}
Output −
Enter an integer: 5
5 is odd
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement
of the colon.
Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value
of the entire ? expression.
If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the
expression.
Syntax –
if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}
You can nest else if...else in the similar way as you have nested if statement.
Example –
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
/* check the boolean condition */
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
Output –
Program –
Write a program to find largest number among three numbers.
#include <stdio.h>
int main ()
{
int n1, n2, n3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf ("Largest number: %d",n1);
if (n2 >= n1 && n2 >= n3)
printf ("Largest number: %d",n2);
if (n3 >= n1 && n3 >= n2)
printf ("Largest number: %d",n3);
return 0;
}
Output –
#include <stdio.h>
int main ()
{
int n1, n2, n3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf ("Largest number: %d",n1);
else if (n2 >= n1 && n2 >= n3)
printf ("Largest number: %d",n2);
else
printf ("Largest number: %d",n3);
return 0;
}
Output –
#include <stdio.h>
int main ()
{
int n1, n2, n3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf ("Largest number: %d",n1);
else
printf ("Largest number: %d",n3);
} else {
if (n2 >= n3)
printf ("Largest number: %d",n2);
else
printf ("Largest number: %d",n3);
}
return 0;
}
Output –
When using if, else if, else statements there are a few points to keep in mind.
o An if can have zero or one else's and it must come after any else if's.
o An if can have zero to many else if's and they must come before the else.
o Once an else if succeeds, none of the remaining else if's or else's will be
tested.
Syntax –
if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
} else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
} else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
} else {
/* executes when the none of the above condition is true */
}
Example –
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10\n" );
} else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20\n" );
} else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30\n" );
} else {
/* if none of the conditions is true */
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
return 0;
}
Output −
Program –
Write a program to check whether the entered number is positive or negative.
#include <stdio.h>
int main()
{
int num;
printf("Enter integer number: ");
scanf("%d",&num);
//Conditions to check if the number is negative or positive
if (num > 0)
printf("The number is positive");
else if (num < 0)
printf("The number is negative");
else
printf("Zero");
return 0;
}
Output –
Output –
Program –
Write a program to read marks from keyboard and display equivalent grade
according to following table (if else ladder)
Marks Grade
100 - 80 Distinction
79 - 60 First Class
59 - 40 Second Class
< 40 Fail
#include<stdio.h>
int main()
{
int marks;
printf("Enter Marks between 0 to 100: ");
scanf("%d",&marks);
if(marks>100 || marks<0)
printf("Invalid Input");
else if(marks>=80)
printf("You got Distinction");
else if(marks>=60)
printf("You got First Class");
else if(marks>=40)
printf("You got Second Class");
else
printf("You are Fail");
return 0;
}
Output –
Output –
Syntax –
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
o A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a
task when none of the cases is true. No break is needed in the default case.
Flow Diagram –
Example –
#include <stdio.h>
int main ()
{
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n");
break;
case 'B' :
case 'C' :
printf("Well done\n");
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n");
break;
default :
printf("Invalid grade\n");
}
printf("Your grade is %c\n", grade);
return 0;
}
Output –
Well done
Your grade is B
Program –
Write a program to read number 1 to 7 and print relatively day Sunday to
Saturday. (switch case)
/* C program to read number 1 to 7 and print relatively day Sunday
to Saturday */
#include <stdio.h>
int main ()
{
int day;
printf("Enter day number between 1 to 7: ");
scanf("%d",&day);
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Wrong input\n");
}
return 0;
}
Output –
Output –
Programming languages provide various control structures that allow for more
complicated execution paths.
• while loop
• for loop
• do-while loop
• nested loops
Depending on where the condition is checked, we can have two types of loop
structures:
• Entry Control
• Exit Control
In entry control loop, the condition is written first and then the body of statements.
While in exit control loop, the body of the statements is written first and then condition
is written. This means that body of statements in exit control loop will be executed at
least once.
While loop is an entry control loop and do-while is an exit control loop.
Figure 4.1(a) explains Entry control loop while figure 4.1(b) explains Exit control
loop.
Syntax –
while(condition) {
statement(s);
}
When the condition becomes false, program control passes to the line
immediately following the loop.
Flow Diagram –
Here, key point of the while loop is that the loop might not ever run. When the
condition is tested and the result is false, the loop body will be skipped and the
first statement after the while loop will be executed.
Example –
#include <stdio.h>
int main ()
{
int a = 10;
while (a < 20) {
printf("value of a: %d\n", a);
a++;
}
return 0;
}
Output –
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Program –
#include <stdio.h>
int main()
{
int i=1,sum=0;
while(i<=100) {
if(i%2==0) {
sum=sum+i;
}
i++;
}
printf("Sum of even numbers between 1 to 100 is: %d", sum);
return 0;
}
Output −
/* Write a program to print the sum of first n integer numbers using while
loop */
/* C program to find sum of first n integer numbers */
#include <stdio.h>
int main ()
{
int n, sum=0, i=1;
printf("Give integer number: ");
scanf("%d",&n);
while(i<=n) {
sum=sum+i;
i++;
}
printf("Sum of first %d numbers = %d\n",n,sum);
return 0;
}
Output −
Output −
A for loop is useful when you know how many times a task is to be repeated.
Syntax –
• The init step is executed first, and only once. This step allows you to declare
and initialize any loop control variables. You are not required to put a
statement here, as long as a semicolon appears.
• Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and flow of
control jumps to the next statement just after the for loop.
• After the body of the for loop executes, the flow of control jumps back up
to the increment statement. This statement can be left blank, as long as a
semicolon appears after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the for loop terminates.
Flow Diagram –
Example –
#include <stdio.h>
int main ()
{
int a;
for (a = 10; a < 20; a = a + 1) {
printf("value of a: %d\n", a);
}
return 0;
}
Output –
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Program –
#include <stdio.h>
int main()
{
int no, i;
printf("Enter a number: ");
scanf("%d",&no);
for(i=2; i<no; i++) {
if(no%i == 0) {
printf("%d is not a prime number", no);
break;
}
}
if(no==i)
printf("%d is a prime number", no);
return 0;
}
Output −
Enter a number: 9
9 is not a prime number
Output −
Enter a number: 89
89 is a prime number
#include <stdio.h>
int main()
{
int n, i;
long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d",&n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist");
else {
for(i = 1; i <= n; i++) {
factorial = factorial * i;
}
printf("Factorial of %d = %ld", n, factorial);
}
return 0;
}
Output −
Output −
Syntax –
do {
statement(s);
}
while (condition);
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s)
in the loop execute again. This process repeats until the given condition becomes
false.
Flow Diagram –
Example –
#include <stdio.h>
int main ()
{
int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
} while (a < 20);
return 0;
}
Output –
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Program –
#include <stdio.h>
int main()
{
int n, i = 1;
printf("Enter number: ");
scanf("%d",&n);
do {
printf("%d ",i);
i++;
} while (i <= n);
return 0;
}
Output −
Enter number: 8
1 2 3 4 5 6 7 8
#include <stdio.h>
int main()
{
int n,i,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
do {
sum=sum+i;
i++;
} while(i<=n);
printf("Sum of first %d numbers = %d", n, sum);
return 0;
}
Output −
Enter a number: 7
Sum of first 7 numbers = 28
Write a program to check whether the given number is prime or not. (do-
while loop)
#include <stdio.h>
int main()
{
int no, i, count=0;
printf("Enter a number: ");
scanf("%d",&no);
i=2;
do {
// check for non-prime number
if(no % i == 0) {
count=1;
break;
}
i++;
} while(i<=no/2);
if (count==0)
printf("%d is a prime number", no);
else
printf("%d is not a prime number", no);
return 0;
}
Output −
Enter a number: 9
9 is not a prime number
Output −
Enter a number: 7
7 is a prime number
Syntax –
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
do {
statement(s);
do {
statement(s);
} while(condition);
} while(condition);
Program –
*
**
***
****
*****
#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print column
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int size = 5;
int space = 4;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < space ; j++) {
printf(" ");
}
// print stars
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
space--; /* decrement one space after one row*/
}
return 0;
}
#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 1; i <= size; i++) {
// print column
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int alphabet = 'A';
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) {
printf("%c ",(char)(alphabet - 1 + i));
}
printf("\n");
}
return 0;
}
5. Solved Programs
Write a program to accept name and marks in 6 subjects, find out percentage
marks and print grade to students according to following condition.
• If student is failed in one subject, then grade is FAIL.
• If Percentage >= 75 then Grade = Distinction.
• If Percentage <75 and Percentage >=60, then grade = First Class.
• If Percentage <60 and Percentage >=50, then grade = Second Class.
• If Percentage <50 and Percentage >=40, then grade = Third Class.
#include<stdio.h>
int main()
{
char name[40];
int m1,m2,m3,m4,m5,m6,total;
float percent;
printf("Enter name of the student\n");
scanf("%s",&name);
printf("Enter marks in 6 subjects\n");
scanf("%d%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5,&m6);
total=m1+m2+m3+m4+m5+m6;
percent=total/6;
if((m1<40)||(m2<40)||(m3<40)||(m4<40)||(m5<40)||(m6<40))
printf("Name: %s \nTotal: %d \nGrade: Fail" ,name,total);
else if(percent >= 75)
printf("Name: %s \nTotal: %d \nGrade: Distinction",name,total);
else if(percent >= 60)
printf("Name: %s \nTotal: %d \nGrade: First Class",name,total);
else if(percent >= 50)
printf("Name: %s \nTotal: %d \nGrade: Second Class",name,total);
else
printf("Name: %s \nTotal: %d \nGrade: Third Class",name,total);
return 0;
}
Output −
Write a program to print the individual digits of a given integer number using
while statement.
#include<stdio.h>
int main()
{
int n,i;
printf("Give Integer Number: ");
scanf("%d",&n);
while(n!=0) {
i = n % 10;/* separate the digit */
printf("The digit is = %d \n", i);
n = n/10; /* new value of n after separation of digit */
}
return 0;
}
Output −
#include<stdio.h>
int main()
{
int num,i;
printf("Give Integer Number\n");
scanf("%d",&num);
printf("Reverse of %d is = ",num);
while(num!=0) {
i = num % 10;
printf("%d",i);
num = num/10;
}
return 0;
}
Output –
#include<stdio.h>
int main()
{
int n,sum=0; /* sum initialized to 0 */
int i,num; /* separated digit stored in i */
printf("Give Integer Number: ");
scanf("%d",&n);
num = n; /* n stored in num, because n will change in loop */
while(n!=0) {
i = n % 10; /* separate the digit */
sum = sum + i*i*i; /* find cube of digit and add to sum */
n = n/10; /* new value of n after separation of digit */
}
if (sum == num)
printf("Given number %d is Armstrong\n",num);
else
printf("Given number %d is not Armstrong\n",num);
return 0;
}
Output –
Output –
#include<stdio.h>
int main()
{
int num1,num2,num3,n;
int count;
num1 = 0;
num2 = 1;
printf("How many Fibonacci Numbers?\n");
scanf("%d",&n);
if(n > 2) {
printf("The Fibonacci Numbers are\n");
printf("%d%5d",num1,num2);
count = 2;
while(count < n) {
num3 = num1 + num2;
printf("%5d",num3);
num1 = num2;
num2 = num3;
count++;
}
} else
printf("By definition first two numbers are 0 1\n");
return 0;
}
Output –
Output –
#include<stdio.h>
int main()
{
int n,sum=0;
int digit;
printf("Give Positive Integer Number: ");
scanf("%d",&n);
printf("Given Number = %d\n",n);
if(n < 0)
printf("Please Give Positive Number\n");
else {
do {
digit = n % 10;
sum = sum + digit;
n = n/10;
} while(n > 0);
printf("Summation of Individual Digits = %d\n",sum);
}
return 0;
}
Output –
Write a program to reverse a given integer number and also check for palindrome.
/* Palindrome is a number which is same after reversal also */
#include<stdio.h>
int main()
{
int num,temp;
int i,rev=0;
printf("Give Integer Number: ");
scanf("%d",&num);
printf("Reverse of %d is = ", num);
temp = num;
while(num != 0) {
i = num % 10;
rev = rev * 10 + i;
num = num/10;
}
printf("%d\n", rev);
if(temp == rev)
printf("Given Number %d is Palindrome\n" ,temp);
else
printf("Given Number %d is not Palindrome\n" ,temp);
return 0;
}
Output –
Output –
#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 1; j < size - i; j++) {
printf(" ");
}
// print stars
for (int k = 0; k <= i; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int alphabet = 'A';
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ",(char)(alphabet + j-1));
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int alphabet = 'A';
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ",(char)(alphabet + i-1));
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int rows=4, number = 1;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= i; ++j) {
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}