0% found this document useful (0 votes)
2 views13 pages

2 - Second Chapter Decision Making Statement

The document provides an overview of decision-making statements in C programming, including various types such as simple if, if-else, nested if, else if ladder, and switch statements. It includes syntax, explanations, and example programs demonstrating how to implement these statements. Additionally, it covers practical applications like calculating electricity bills and discounts based on conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

2 - Second Chapter Decision Making Statement

The document provides an overview of decision-making statements in C programming, including various types such as simple if, if-else, nested if, else if ladder, and switch statements. It includes syntax, explanations, and example programs demonstrating how to implement these statements. Additionally, it covers practical applications like calculating electricity bills and discounts based on conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

DECISION MAKING STATEMENT

Decision making statement means if we have number of choices in


programme
And we want to select the particular choice from then we need to check the
condition and select the option when we want.

Ways of decision making statement in c language.


1] Simple if statement
2]if else statement
3]Else if ladder statement
4]Nested if statement
5]Switch statement

How to use simple if statement


Syntax:- if(condition)
{
The statement block;
}
Other programme part.

Explanation:-
In simple if statement if condition is true then execute the if block if
condition
False then execute the other programme part.

e.g. consider in programme a=10 and b=5


if(a>b)
{
Printf(“A is a greater %d”,a);
If(b>a){
Printf(“B is a greater %d”,b);
}
In above example the programme generate the output : a is greater
10because
A is a greater then b and condition is true.

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

//Following programme shows the use of simple if statement


#include<stdio.h>
#include<conio.h>
void main(){
int a,b,c;
clrscr();
printf(“enter the two values”);
scanf(“%d %d”,&a,&b);
if(a>b)
{
printf(“A is greater %d”,a);
}
if(b>a)
{
printf(“Bis greater %d”,b);
}
getch();
}
Output:-

How to use the if else statement


If (condition)
{
True statement block
}
Else
{
False statement block
}

Other programme statement

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

ExPlanation :- in if else statement if condition is true then execute the if


block otherwise excute the else block.
#include<stdio.h>
#include<conio.h>
void main(){
int no;
printf(“enter the number from keyboard”);
scanf(“%d”,&no);
if(no%2==0)
{
printf(“Number is even %d”,no);
}
else
{
printf(“Number is odd %d”,no);
}
getch();
}
Output:-

How to use the nested if statement


In nested if statement if outer if statement is true then excute the inner if
statement if inner if statement is true then excute the inner if true statement
block otherwise execute the else block of appropriate if statement.
Syntax:-
if (condition){
if(condition){
true statement block
}

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

else{
false statement block
}

else
{
statement block
}
other programme part

//programme demonstrate the use of nested if statement


Write a programme to enter the id and sal of employee if id is equal to 1 or 2
and salary is greater than 5000 then employee is permanent.if salary is less
than
5000 then employee is temporary.if id is not equal to q or 2 then employee is
not a member of organization.

#include<stdio.h>
#include<conio.h>
void main(){
int id,sal;
clrscr();
printf(“enter the id of employee \n”);
scanf(“%d”,&id);
if(id==1||id==2)
{
printf(“enter the salary of employee \n”);
scanf(“%d”,&sal);
if(sal>5000)
{
printf(“employee is permanent”);
}
else
{
printf(“employee is temporary \n”);
}
else
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

{
printf(“employee is not a member of organization”);
}
getch();
}
Output:

How to use the else if ladder


In else if ladder every if statement excute self no condition is depend on
another condition if first if statement is true then execute otherwise check
the another else if condition if any one else if statement is not true then
execute the default
Else block
Syntax:- if(condition){
true statement block
}
else if (condition ){
True statement block
}
else
{
false statement block
}

Other programme part


//Programme demonstrate the use of else if ladder statement

Write a programme to enter the three digit number and check


which is greater.
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

#include<stdio.h>
#include<conio.h>
void main(){
int a,b,c;
clrscr();
printf(“enter the three number “);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
{
printf(“a is the greater %d”,a);
}
else if(b>a&&b>c)
{
printf(“b is the greater %d”,b);
}
else{
printf(“c is the greater %d”,c);
}
getch();
}
Output:

How to use the switch statement


Syntax:-
Switch(choice){
case 1:
statement
break;

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

case 2:
statement
break;

case 3:
statement
break;

default:
Statement
}

In switch if case match with given choice then the particular case block
Will execute.

Note:-in switch statement we can pass the character as a choice .any


arithmetic expression without decimal point but can not pass the any
floating value as a choice as well as string.

//Following programme demonstrate the use of switch


statement

#include<stdio.h>
#include<conio.h>
void main(){
int choice;
clrscr();
printf(“enter the your choice”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“your choice is %d”,choice);
break;
case 2:
printf(“your choice is %d”,choice);

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

break;
case 3:
printf(“your choice is %d”,choice);
break;
default:
printf(“your choice is wrong”);
}
getch();
}

Solved programme for decision making statement

//Programme for calculate the electricity bill


#include<stdio.h>
#include<conio.h>
void main(){
int units;
float bill;clrscr();
printf(“\n enter the number of units consumed”);
scanf(“%d”,&units);
if(units<=100)
bill=units*2.35;
else
if(units<=500)
bill=100*2.35+(units-100)*5.65;
else
if(units <=1000)
bill=235+400*5.65+(units-500)*7.35;

else
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

bill=235+400*5.65+500*7.25+(units-1000)*9.85;
printf(“\n the bill is %2f”,bill);
getch();
}
Output:

//Programme demonstrate the discount on bill


#include<stdio.h>
#include<conio.h>
void main(){
int items;
float discount ,bill,price;
clrscr();
printf(“\n enter the number of items purchased:”);
scanf(“%d”,&items);
printf(“\n enter the price of items purchased:”);
scanf(“%f”,&price);
bill=items*price;
printf(“\n the bill before discount %2f”,bill);
if(bill>1000)
{
discount =bill*0.1;
bill=bill-discount;}
else
if(bill>5000)
discount =bill*0.1;
bill=bill-discount;
}
printf(“\n the final bill is %2f”,bill);
getch();
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

}
Output:-

//Programme demonstrate the number is positive or not

#include<stdio.h>
#include<conio.h>
void main(){
int num;
clrscr();
printf(“\n enter the number “);
scanf(“%d”,&num);
if(num>0)
printf(“\n the number is positive”);
if(num<0)
printf(“\n the number is negative”);
if(num==0)
printf(“\n the number is zero”);
getch();
}

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

//Programme for calculating the salary on hour basis


#include<stdio.h>
#include<conio.h>
void main(){
int items;
float discount,bill,price;
clrscr();
printf(“\n enter the number of items purchased”);
scanf(“%d”,&items);
printf(“\n enter the price of items purchased”);
scanf(“%d”,&price);
bill=items*price;
printf(“\n the bill before discount %2f”,bill);
if(bill>1000){
discount =bill*0.1;
bill=bill-discount;
}
else
if(bill>5000)
{
discount =bill*0.1;
bill=bill-discount;
else
if(age<22&&(gender==’m’||gender==’m’)&&(cat==’s’||cat==’s’)&&per>60)
printf(“\n Eligible”);
else

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

printf(“\n not eligible”);


getch();
}

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

GIRI’S TECH HUB,Pune(IT Training Institute With 100% Guaranteed Placement Support Mob:9175444433)

You might also like