Chapter2-Decision Making Statements
Chapter2-Decision Making Statements
There will be many situations when you will be given two or more
options and you will have to select an option based on the given
conditions. For example, we want to print a remark about a student
based on secured marks and following is the situation: Assume
given marks are x for a student. 1) If given marks are more than
95 then Student is brilliant 2) If given marks are less than 30 then
Student is poor 3) If given marks are less than 95 and more than
30 then Student is average .
decision making statements which work based on the following flow
diagram:
1
3/24/15
if...else statement
if...else statement
An if...else statement is useful when we have to take a decision out of two
options. For example, if student secures more marks than 95, then student is
brilliant otherwise no, such situation can be coded as follows:
#include <stdio.h>
main()
{
int x = 45;
if( x > 95)
{
printf( "Student is brilliant\n");
}
else
{
printf( "Student is not brilliant\n");
}
}
When above program is executed, it produces the following result:
if...elseif...else statement
An if statement can be followed by an optional else if...else statement, which is very useful
to test various conditions using single if...else if statement. When using if , else if , else
statements, there are few points to keep in mind:
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.
The syntax of an if...else if...else statement in C programming language is:
if(boolean_expression 1) Now with the help of if...elseif...else
{ statement, very first program can
be coded as follows:
/* Executes when the boolean expression 1 is true */
} #include <stdio.h>
else if( boolean_expression 2)
{ main()
/* Executes when the boolean expression 2 is true { */
int x = 45;
}
else if( boolean_expression 3) if( x > 95)
{ {
/* Executes when the boolean expression 3 is trueprintf( */ "Student is brilliant\n");
} }
else else if( x < 30)
{
{
printf( "Student is poor\n");
/* Executes when the none of the above condition } is true */
} else if( x < 95 && x > 30 ) 5
3/24/15
if...elseif...else statement
Now with the help of if...elseif...else statement, very first
program can be coded as follows:
#include <stdio.h>
main()
{
int x = 45;
8
#include <stdio.h> 3/24/15
Hello, World! 10
3/24/15
for Loop
Loops cause program to execute the certain block of code repeatedly until
test condition is false. Loops are used in performing repetitive task in
programming. Consider these scenarios:
You want to execute some code/s 100 times.
You want to execute some code/s certain number of times depending upon input from user.
for Loop Syntax:
for(initialization; test expression; update statement)
{
code/s to be executed;
}
The initialization statement is executed only once at the beginning of the for
loop
Then the test expression is checked by the program.
If the test expression is false, for loop is terminated.
But if test expression is true then the code/s inside body of for loop is
executed and then
update expression is updated. This process repeats until test expression is
false.
14
3/24/15
For loop
15
3/24/15
For loop
for loop example: Write a program to find the sum of the first n natural
numbers where n is entered by user. Note: 1,2,3... are called natural
numbers.
Output
#include <stdio.h>
Enter the value of n.
int main()
19
{ Sum=190
int n, count, sum=0; In this program, the user is asked
printf("Enter the value of n.\n");
to enter the value of n. Suppose
scanf("%d“, &n); you entered 19 then, count is
for(count=1;count<=n;++count) initialized to 1 at first. Then, the
test expression in the for loop,i.e.,
//for loop terminates if count>n
{ (count<= n) becomes true. So,
sum+=count;
the code in the body of for loop is
executed which makes sum to 1.
/* sum=sum+count */
Then, the expression ++count is
} executed and again the test
printf("Sum=%d“, sum); expression is checked, which
return 0; becomes true. Again, the body of
} for loop is executed which makes
sum to 3 and this process
16
3/24/15
18
3/24/15
goto Statement
20
3/24/15
Examples
Write a C program to find the factorial of a number using while
loop, where the number is entered by user. (Hints: factorial of
n = 1*2*3*...*n
/*C program to demonstrate the working of while loop*/
#include <stdio.h>
Output
Enter a number.
int main()
5
{
Factorial=120
int number, factorial;
printf("Enter a number.\n");
scanf("%d“, &number);
factorial=1;
while (number>0)
{
/* while loop continues until test condition number>0 is true */
factorial=factorial*number;
- - number;
}
printf("Factorial=%d“, factorial);
return 0; 23
3/24/15
examples
Write a C program to add all the numbers entered by a user until user
enters 0.using do..while loop.
/*C program to demonstrate the working of do...while statement*/
#include <stdio.h>
Output
int main() Enter a number
{ 3
int sum=0,num; Enter a number-
/* Codes inside the body of do...while
2 loops
are at least executed once. */ Enter a number
do 0
{ sum=1
printf("Enter a number\n");
scanf("%d“, &num); In this C program, user is asked a
sum+=num; number and it is added with sum.
} Then, only the test condition in
while(num!=0); the do...while loop is checked. If
the test condition is true,i.e, num
printf("sum=%d“, sum);
is not equal to 0, the body of
return 0;
do...while loop is again executed
}
until num equals to zero. 24
3/24/15
examples
Write a C program to find average of maximum of n positive numbers entered by user.
But, if the input is negative, display the average(excluding the average of negative input)
and end the program.
/* C program to demonstrate the working of break statement by terminating a loop, if
user inputs negative number*/
Output
#include<stdio.h>
int main()
Maximum no. of inputs
{ 4
float num, average, sum; Enter n1: 1.5
int I, n; Enter n2: 12.5
printf("Maximum no. of inputs\n");
Enter n3: 7.2
scanf("%d“, &n);
for(i=1;i<=n;++i)
Enter n4: -1
{ Average=7.07
printf("Enter n%d: “, i); In this program, when the user
scanf("%f”, &num); inputs number less than zero, the
if(num<0.0) loop is terminated using break
break; //for loop breaks if num<0.0
sum=sum+num;
statement with executing the
} statement below it i.e., without
average=sum/(i-1); executing sum=sum+num.
printf("Average=%.2f",average);
return 0;
}
25
3/24/15
examples
Write a C program to find the product of 4 integers entered by a user. If user
enters 0 skip it.
//program to demonstrate the working of continue statement in C
programming
#include<stdio.h> Output
int main() Enter num1: 3
{ Enter num2: 0
int i, num,product; Enter num3: - 5
for(i=1,product=1;i<=4;++i) Enter num4: 2
{ product= - 30
printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)
continue; / *In this program, when num equals to zero, it skips the statement
product*=num and continue the loop. */
product*=num;
}
printf("product=%d“, product);
return 0;
}
26
3/24/15
Examples
Write a program that asks user an arithmetic operator('+','-','*' or '/') and two operands and perform the
corresponding calculation on the operands.
/* C program to demonstrate the working of switch...case statement */
/* C Program to create a simple calculator for addition, subtraction, multiplication and division */
# include <stdio.h>
int main() Output
{
char o; Enter operator either + or -
float num1,num2; or * or /
printf("Select an operator either + or - or * or / \n");
scanf("%c",&o); *
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
Enter two operands: 2.3
switch(o) 4.5
{
case '+':
2.3 * 4.5 = 10.3
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
The break statement at the
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2); end of each case cause
break;
case '*': switch statement to exit. If
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
break statement is not
case '/': used, all statements below
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
that case statement are
also executed.
default: /* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
27
}
3/24/15
examples
example
example
Write a program to ask a user to enter three numbers and this
program will find the largest number among three numbers
/* Centered
programbytouser.
find largest /* C program to find largest number using
if...else statement */
number using if statement #include <stdio.h>
only */ int main()
#include <stdio.h> {
int main() float a, b, c;
printf("Enter three numbers: ");
{ scanf("%f %f %f", &a, &b, &c);
float a, b, c; if (a>=b)
printf("Enter three {
if(a>=c)
numbers: "); printf("Largest number =
scanf("%f %f %f", &a, &b, %.2f",a);
&c); else
if(a>=b && a>=c) printf("Largest number =
%.2f",c);
printf("Largest number = %.2f", a);
}
if(b>=a && b>=c) else
printf("Largest number = %.2f", b); {
if(b>=c)
if(c>=a && c>=b)
printf("Largest number =
printf("Largest number = %.2f", c); %.2f",b);
return 0; else 30
printf("Largest number =