SlideShare a Scribd company logo
Decision Making Using if
Statement
Prepared By:
Dr. Chandan Kumar
Assistant Professor, Computer Science & Engineering Department
Invertis University, Bareilly
Decision Making Using if Statement
 The statements which are used to take decision
 Decision based on some test expression
 These statements are used to decide to execute
some code/s and ignore some code/s
 Four variant of decision making statement (if)
 if statement
 if-else statement
 nested if statement
 else-if statement
if statement
 This statement is also known as only if
statement or one way branching statement
 checks whether the given expression is true of
false
 If true then body of the if statement is executed
 If false then body of the if statement is ignored
 Syntax
if(test_expression)
{
statement/s; // body of if
}
 Flow Chart if statement
Write a C program to print the number entered by user only if the
number entered is even
#include <stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf(“Enter a number :”);
scanf(“%d”,&num);
if ( num%2==0)
{
printf(“ nNumber %d is an even number”,num);
}
getch();
}
Output
Enter a number : 2
Number 2 is an even number
if-else statement
 Also known as two way branching statement
 Used when programmer wants to execute
statement/s when the test expression is true
and execute another statement/s if the test
expression is false
 Only one block is executed at a time
if-else statement
 Syntax
if(test_expression)
{
statement/s; // body of if
}
else
{
statement/s; // body of else
}
Flow chart if-else
Write a C program to print the number entered by user only if the
number entered is even or odd
#include <stdio.h>
#include< conio.h>
void main()
{
int num;
clrscr();
printf(“Enter a number :”);
scanf(“%d”,&num);
if ( num%2==0)
{
printf(“ nNumber %d is an even number”,num);
}
else
{
printf(“ nNumber %d is an odd number”,num);
}
getch();
}
if-else statement
 Output 1
Enter a number : 2
Number 2 is an even number
 Output 2
Enter a number : 3
Number 3 is an odd number
Nested if statement
 An if statement inside another if statement
 Programmers used this statement when want to
check one condition inside another condition
 When an if else statement is present inside the
body of another “if” or “else”
Nested if statement
 Syntax
if(test_expression1/ condition 1)
{ //Nested if else inside the body of "if"
if(test_expression2/ condition 2)
{ //Statements inside the body of nested "if”}
else
{ //Statements inside the body of nested "else”}
}
else
{ //Statements inside the body of "else" }
Nested if statement
Write a C program to print largest number among three numbers
#include<stdio.h>
#include<conio.h>
Void main()
{
int num1,num2,num3;
clrscr();
printf(“Enter three numbers: );
scanf(“%d%d%d”,&num1,&num2,&num3);
if(num1>num2)
{
if(num1>num3)
printf(“ %d is the largest number”,num1);
else
printf(“ %d is the largest number,num3);
}
Nested if statement
else
{
if(num2>num3)
printf(“ %d is the largest number”,num2);
else
printf(“ %d is the largest number,num3);
}
getch();
}
Output1:
Enter three numbers: 10 12 11
12 is the largest number
else-if statement
 Used in program when if statement having
multiple decisions
 Syntax
if(test_expression1)
{ //execute code }
.
.
.
else if(test_expression n)
{ //execute code }
else
{ //execute code }
else-if statement
Write a C program to print largest number among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf(“Enter three numbers: );
scanf(“%d%d%d”,&num1,&num2,&num3);
if (num1>num2 && num1>num3)
printf(“ %d is the largest number”,num1);
else if (num2>num1 && num2>num3)
printf(“ %d is the largest number”,num2);
else
printf(“ %d is the largest number”,num3);
getch();
}
Decision making using if statement

More Related Content

What's hot

If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
Manash Kumar Mondal
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
Control structure
Control structureControl structure
Control structure
Samsil Arefin
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
Priyansh Thakar
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
Tech
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Hossain Md Shakhawat
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
amber chaudary
 
Control statement
Control statementControl statement
Control statement
Sakib Shahriar
 
Control structures i
Control structures i Control structures i
Control structures i
Ahmad Idrees
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
teach4uin
 

What's hot (20)

If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Control structure
Control structureControl structure
Control structure
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Control statement
Control statementControl statement
Control statement
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Control structures i
Control structures i Control structures i
Control structures i
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 

Similar to Decision making using if statement

Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
mounikanarra3
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
NabishaAK
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptx
Sasideepa
 
Cin and cout
Cin and coutCin and cout
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Chandrakant Divate
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
Mahbubay Rabbani Mim
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
COM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingCOM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdfLesson 2 C STATEMENT IN PROGRAMMING (M).pdf
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf
WeejMaderazo
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
Rakesh Roshan
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
Mehul Desai
 

Similar to Decision making using if statement (20)

Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptx
 
Cin and cout
Cin and coutCin and cout
Cin and cout
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Branching statements
Branching statementsBranching statements
Branching statements
 
COM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & BranchingCOM1407: Program Control Structures – Decision Making & Branching
COM1407: Program Control Structures – Decision Making & Branching
 
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdfLesson 2 C STATEMENT IN PROGRAMMING (M).pdf
Lesson 2 C STATEMENT IN PROGRAMMING (M).pdf
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 

More from CHANDAN KUMAR

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
CHANDAN KUMAR
 
Raid technology
Raid technologyRaid technology
Raid technology
CHANDAN KUMAR
 
Pointers in c
Pointers in cPointers in c
Pointers in c
CHANDAN KUMAR
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Linked List
Linked ListLinked List
Linked List
CHANDAN KUMAR
 
Stack and queue
Stack and queueStack and queue
Stack and queue
CHANDAN KUMAR
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
CHANDAN KUMAR
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 

More from CHANDAN KUMAR (13)

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
 
Raid technology
Raid technologyRaid technology
Raid technology
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Linked List
Linked ListLinked List
Linked List
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Technical questions for interview c programming
Technical questions for interview  c programmingTechnical questions for interview  c programming
Technical questions for interview c programming
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
 

Recently uploaded

Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 

Recently uploaded (20)

Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 

Decision making using if statement

  • 1. Decision Making Using if Statement Prepared By: Dr. Chandan Kumar Assistant Professor, Computer Science & Engineering Department Invertis University, Bareilly
  • 2. Decision Making Using if Statement  The statements which are used to take decision  Decision based on some test expression  These statements are used to decide to execute some code/s and ignore some code/s  Four variant of decision making statement (if)  if statement  if-else statement  nested if statement  else-if statement
  • 3. if statement  This statement is also known as only if statement or one way branching statement  checks whether the given expression is true of false  If true then body of the if statement is executed  If false then body of the if statement is ignored  Syntax if(test_expression) { statement/s; // body of if }
  • 4.  Flow Chart if statement
  • 5. Write a C program to print the number entered by user only if the number entered is even #include <stdio.h> #include< conio.h> void main() { int num; clrscr(); printf(“Enter a number :”); scanf(“%d”,&num); if ( num%2==0) { printf(“ nNumber %d is an even number”,num); } getch(); } Output Enter a number : 2 Number 2 is an even number
  • 6. if-else statement  Also known as two way branching statement  Used when programmer wants to execute statement/s when the test expression is true and execute another statement/s if the test expression is false  Only one block is executed at a time
  • 7. if-else statement  Syntax if(test_expression) { statement/s; // body of if } else { statement/s; // body of else }
  • 9. Write a C program to print the number entered by user only if the number entered is even or odd #include <stdio.h> #include< conio.h> void main() { int num; clrscr(); printf(“Enter a number :”); scanf(“%d”,&num); if ( num%2==0) { printf(“ nNumber %d is an even number”,num); } else { printf(“ nNumber %d is an odd number”,num); } getch(); }
  • 10. if-else statement  Output 1 Enter a number : 2 Number 2 is an even number  Output 2 Enter a number : 3 Number 3 is an odd number
  • 11. Nested if statement  An if statement inside another if statement  Programmers used this statement when want to check one condition inside another condition  When an if else statement is present inside the body of another “if” or “else”
  • 12. Nested if statement  Syntax if(test_expression1/ condition 1) { //Nested if else inside the body of "if" if(test_expression2/ condition 2) { //Statements inside the body of nested "if”} else { //Statements inside the body of nested "else”} } else { //Statements inside the body of "else" }
  • 14. Write a C program to print largest number among three numbers #include<stdio.h> #include<conio.h> Void main() { int num1,num2,num3; clrscr(); printf(“Enter three numbers: ); scanf(“%d%d%d”,&num1,&num2,&num3); if(num1>num2) { if(num1>num3) printf(“ %d is the largest number”,num1); else printf(“ %d is the largest number,num3); }
  • 15. Nested if statement else { if(num2>num3) printf(“ %d is the largest number”,num2); else printf(“ %d is the largest number,num3); } getch(); } Output1: Enter three numbers: 10 12 11 12 is the largest number
  • 16. else-if statement  Used in program when if statement having multiple decisions  Syntax if(test_expression1) { //execute code } . . . else if(test_expression n) { //execute code } else { //execute code }
  • 18. Write a C program to print largest number among three numbers #include<stdio.h> #include<conio.h> void main() { int num1,num2,num3; clrscr(); printf(“Enter three numbers: ); scanf(“%d%d%d”,&num1,&num2,&num3); if (num1>num2 && num1>num3) printf(“ %d is the largest number”,num1); else if (num2>num1 && num2>num3) printf(“ %d is the largest number”,num2); else printf(“ %d is the largest number”,num3); getch(); }