SlideShare a Scribd company logo
Decision making
statements in C
1Presented by: Group-E (The Anonymous)
Bikash Dhakal
Bimal Pradhan
Bikram Bhurtel
Gagan Puri
Rabin BK
Introduction to decision making statements
Decision making statements in C
If statement
If….else statement
If….else if….else statement
Nested if statement
Switch statement
The conditional operator(? : )
GOTO statement
References
Queries
2
Introduction to decision making statement in C
Also known as control statements
Controls the flow of execution
Executes program until a specified condition is met
One of the most important parts in programming
3
Decision making statements in C
if statement
switch statement
Conditional operator statement
goto statement
4
If statement
The expression must evaluate to true or false.
The “statement” can be a group of statements in braces:
Syntax:
5
if(expression)
{
Statement 1;
Statement 2;
}
6
#include<stdio.h>
#include<conio.h>
main( )
{
int n ;
printf ( "Enter your age: " ) ;
scanf ("%d", &n ) ;
if ( n>=20 )
{
printf (“nYou are in your adulthood. ” ) ;
}
getch();
}
Enter any number: 25
You are in your adulthood.
If else statement
 An extension version of if statement.
 Generally in the form of if (test expression)
7
if (test expression)
{
true block statement(s)
}
else
{
false block statement(s)
}
Example
8
#include<stdio.h>
#include<conio.h>
main()
{
int age;
printf("n enter your age:");
scanf("%d",&age);
if(age>=18)
{
printf("eligible for citizenship");
}
else
{
printf("n NOT ELIGIBLE");
}
getch();
}
enter your age:13
NOT ELIGIBLE
enter your age:20
eligible for citizenship
If …else if…. else statement
 It is used to give series of decision
 Syntax:
9
if (condition)
{
//statement 1
}
else if (condition 2)
{
//statement 2
}
else
{
// statement when all condition are false
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
r1=-b/(2*a);
printf("n Roots are equal and is %.3f",r1);
}
else if(d>0)
{
d=sqrt(d);
r1=(-b+d)/(2*a);
r2=(-b-d)/(2*a);
printf("nRoots are real and r1=%.3f, r2=%.3f",r1,r2);
}
else
{
d=sqrt(fabs(d));
printf("n Roots are imaginary");
printf("n r1=%.3f + i%.3f",-b/(2*a),d/(2*a));
printf("n r2=%.3f - i%.3f",-b/(2*a),d/(2*a));
}
getch();
10
//Program to find the roots of the quadratic equations ax^2+bx+c=0
Enter coefficients a, b, c: 1
2
1
Roots are equal and is -1.000
Nested if-else statement
New block of if-else statement defined
in existing if or else block statement.
Syntax:
11
if(condition)
{
if(condition)
{
statement
}
else
{
statement
}
}
else
{
statement
}
12
#include <stdio.h>
#include <conio.h>
void main( )
{
int a,b,c;
printf("Enter 3 numbers: n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if( a > c)
{
printf(“%d is greatest“,a);
}
else
{
printf(“%d is greatest“,c);
}
}
else
{
if( b> c)
{
printf(“%d is greatest“,b);
}
else
{
printf(“%d is greatest“,c);
}
}
getch();
}
Using Nested-if statement to check greatest
number among three input numbers
Enter 3 numbers:
9
11
5
11 is greatest
Enter 3 numbers:
49
55
88
88 is greatest
 Multi-way decision statement
 Tests the value of a given variable against a list of case values
 when a match is found, a block of statements associated with that
case is executed.
13
switch (var)
{
case case value 1:
statements;
break;
case case value 2:
statements;
break;
…….
…….
case case value n:
default:
statements;
break;
}
Should be an integer or characters
Known as case labels and should be constants or
constant expressions
And should be end with semicolon
14
#include<stdio.h>
int main()
{
char operator;
float firstNumber,secondNumber;
printf("Enter an operator (+, -): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%f + %f = %f",firstNumber, secondNumber, firstNumber +
secondNumber);
break;
case '-':
printf("%f - %f = %f",firstNumber, secondNumber, firstNumber -
secondNumber);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Enter an operator(+,-): -
Enter two operands: 6
4
6.000000 – 4.000000 = 2.000000
Enter an operator(+,-): /
Enter two operands: 9
3
Error! operator is not correct
15
The ?: operator
Known as conditional operator
Used for making two-way decisions
Syntax:
16
Conditional expression ? exp1: exp2
Example:
17
//If True first exp is executed
//If False second exp is executed
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int a;
printf("Enter a number: ");
scanf("%d",&a);
(a%2==0?printf("Even"):printf("Odd"));
getch();
}
Enter a number: 9
Odd
Enter a number: 8
Even
The GOTO statement
Rarely used statement
Provides an unconditional jump from ‘goto’ to a labelled statement
in the same function
Syntax:
Requires label to identify the place where the branch is to be made
18
goto label;
-------------
-------------
label;
statement;
label;
statement;
-------------
-------------
goto label;
Forward jump Backward jump
Example:
19
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float y,x;
read:
printf("nEnter a number: ");
scanf("%f",&y);
if(y<0) goto read;
x=sqrt(y);
printf("nSquare root of %f---->%fn",y,x);
goto read;
}
Enter a number: 9
Square root of 9.000000---->3.000000
Enter a number: 25
Square root of 25.000000---->5.000000
References:
Websites:
google.com
codecadamy.com
tutorialspoint.com
External sources:
Text book: Programming in ANSIC (E-Balagurusamy)
20
Queries
21

More Related Content

What's hot

Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++Bishal Sharma
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case StatementsDipesh Pandey
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C ProgrammingKamal Acharya
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programmingCHANDAN KUMAR
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming LanguageMahantesh Devoor
 

What's hot (20)

Strings in C
Strings in CStrings in C
Strings in C
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Loops c++
Loops c++Loops c++
Loops c++
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
String functions in C
String functions in CString functions in C
String functions in C
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Looping in C
Looping in CLooping in C
Looping in C
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 

Similar to Decision making statements in C programming

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 LanguagesChandrakantDivate1
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Branching statements
Branching statementsBranching statements
Branching statementsArunMK17
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiirakshithatan
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Conditional statements
Conditional statementsConditional statements
Conditional statementsNabishaAK
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
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 CSowmya Jyothi
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptxishaparte4
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptSanjjaayyy
 
3. control statements
3. control statements3. control statements
3. control statementsamar kakde
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming DharmaKumariBhandari
 

Similar to Decision making statements in C programming (20)

Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
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
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Branching statements
Branching statementsBranching statements
Branching statements
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
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
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Branching
BranchingBranching
Branching
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
3. control statements
3. control statements3. control statements
3. control statements
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming
 

More from Rabin BK

Artificial Intelligence in E-commerce
Artificial Intelligence in E-commerceArtificial Intelligence in E-commerce
Artificial Intelligence in E-commerceRabin BK
 
Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
Consumer Oriented Application, Mercantile process and Mercantile models
Consumer Oriented Application, Mercantile process and Mercantile modelsConsumer Oriented Application, Mercantile process and Mercantile models
Consumer Oriented Application, Mercantile process and Mercantile modelsRabin BK
 
Clang compiler `
Clang compiler `Clang compiler `
Clang compiler `Rabin BK
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer ProtocolRabin BK
 
HTML text formatting tags
HTML text formatting tagsHTML text formatting tags
HTML text formatting tagsRabin BK
 
Data encryption in database management system
Data encryption in database management systemData encryption in database management system
Data encryption in database management systemRabin BK
 
Object Relational Database Management System(ORDBMS)
Object Relational Database Management System(ORDBMS)Object Relational Database Management System(ORDBMS)
Object Relational Database Management System(ORDBMS)Rabin BK
 
Kolmogorov Smirnov
Kolmogorov SmirnovKolmogorov Smirnov
Kolmogorov SmirnovRabin BK
 
Job sequencing in Data Strcture
Job sequencing in Data StrctureJob sequencing in Data Strcture
Job sequencing in Data StrctureRabin BK
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data StructureRabin BK
 
Data Science
Data ScienceData Science
Data ScienceRabin BK
 
Graphics_3D viewing
Graphics_3D viewingGraphics_3D viewing
Graphics_3D viewingRabin BK
 
Neural Netwrok
Neural NetwrokNeural Netwrok
Neural NetwrokRabin BK
 
Watermarking in digital images
Watermarking in digital imagesWatermarking in digital images
Watermarking in digital imagesRabin BK
 
Heun's Method
Heun's MethodHeun's Method
Heun's MethodRabin BK
 
Mutual Exclusion
Mutual ExclusionMutual Exclusion
Mutual ExclusionRabin BK
 
Systems Usage
Systems UsageSystems Usage
Systems UsageRabin BK
 
Manager of a company
Manager of a companyManager of a company
Manager of a companyRabin BK
 

More from Rabin BK (20)

Artificial Intelligence in E-commerce
Artificial Intelligence in E-commerceArtificial Intelligence in E-commerce
Artificial Intelligence in E-commerce
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Consumer Oriented Application, Mercantile process and Mercantile models
Consumer Oriented Application, Mercantile process and Mercantile modelsConsumer Oriented Application, Mercantile process and Mercantile models
Consumer Oriented Application, Mercantile process and Mercantile models
 
Clang compiler `
Clang compiler `Clang compiler `
Clang compiler `
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer Protocol
 
HTML text formatting tags
HTML text formatting tagsHTML text formatting tags
HTML text formatting tags
 
Data encryption in database management system
Data encryption in database management systemData encryption in database management system
Data encryption in database management system
 
Object Relational Database Management System(ORDBMS)
Object Relational Database Management System(ORDBMS)Object Relational Database Management System(ORDBMS)
Object Relational Database Management System(ORDBMS)
 
Kolmogorov Smirnov
Kolmogorov SmirnovKolmogorov Smirnov
Kolmogorov Smirnov
 
Job sequencing in Data Strcture
Job sequencing in Data StrctureJob sequencing in Data Strcture
Job sequencing in Data Strcture
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Bluetooth
BluetoothBluetooth
Bluetooth
 
Data Science
Data ScienceData Science
Data Science
 
Graphics_3D viewing
Graphics_3D viewingGraphics_3D viewing
Graphics_3D viewing
 
Neural Netwrok
Neural NetwrokNeural Netwrok
Neural Netwrok
 
Watermarking in digital images
Watermarking in digital imagesWatermarking in digital images
Watermarking in digital images
 
Heun's Method
Heun's MethodHeun's Method
Heun's Method
 
Mutual Exclusion
Mutual ExclusionMutual Exclusion
Mutual Exclusion
 
Systems Usage
Systems UsageSystems Usage
Systems Usage
 
Manager of a company
Manager of a companyManager of a company
Manager of a company
 

Recently uploaded

Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTechSoup
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Abhinav Gaur Kaptaan
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17Celine George
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxjmorse8
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17Celine George
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 

Recently uploaded (20)

Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 

Decision making statements in C programming

  • 1. Decision making statements in C 1Presented by: Group-E (The Anonymous) Bikash Dhakal Bimal Pradhan Bikram Bhurtel Gagan Puri Rabin BK
  • 2. Introduction to decision making statements Decision making statements in C If statement If….else statement If….else if….else statement Nested if statement Switch statement The conditional operator(? : ) GOTO statement References Queries 2
  • 3. Introduction to decision making statement in C Also known as control statements Controls the flow of execution Executes program until a specified condition is met One of the most important parts in programming 3
  • 4. Decision making statements in C if statement switch statement Conditional operator statement goto statement 4
  • 5. If statement The expression must evaluate to true or false. The “statement” can be a group of statements in braces: Syntax: 5 if(expression) { Statement 1; Statement 2; }
  • 6. 6 #include<stdio.h> #include<conio.h> main( ) { int n ; printf ( "Enter your age: " ) ; scanf ("%d", &n ) ; if ( n>=20 ) { printf (“nYou are in your adulthood. ” ) ; } getch(); } Enter any number: 25 You are in your adulthood.
  • 7. If else statement  An extension version of if statement.  Generally in the form of if (test expression) 7 if (test expression) { true block statement(s) } else { false block statement(s) }
  • 8. Example 8 #include<stdio.h> #include<conio.h> main() { int age; printf("n enter your age:"); scanf("%d",&age); if(age>=18) { printf("eligible for citizenship"); } else { printf("n NOT ELIGIBLE"); } getch(); } enter your age:13 NOT ELIGIBLE enter your age:20 eligible for citizenship
  • 9. If …else if…. else statement  It is used to give series of decision  Syntax: 9 if (condition) { //statement 1 } else if (condition 2) { //statement 2 } else { // statement when all condition are false }
  • 10. #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,d,r1,r2; printf("Enter coefficients a, b and c: "); scanf("%f%f%f",&a,&b,&c); d=b*b-4*a*c; if(d==0) { r1=-b/(2*a); printf("n Roots are equal and is %.3f",r1); } else if(d>0) { d=sqrt(d); r1=(-b+d)/(2*a); r2=(-b-d)/(2*a); printf("nRoots are real and r1=%.3f, r2=%.3f",r1,r2); } else { d=sqrt(fabs(d)); printf("n Roots are imaginary"); printf("n r1=%.3f + i%.3f",-b/(2*a),d/(2*a)); printf("n r2=%.3f - i%.3f",-b/(2*a),d/(2*a)); } getch(); 10 //Program to find the roots of the quadratic equations ax^2+bx+c=0 Enter coefficients a, b, c: 1 2 1 Roots are equal and is -1.000
  • 11. Nested if-else statement New block of if-else statement defined in existing if or else block statement. Syntax: 11 if(condition) { if(condition) { statement } else { statement } } else { statement }
  • 12. 12 #include <stdio.h> #include <conio.h> void main( ) { int a,b,c; printf("Enter 3 numbers: n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if( a > c) { printf(“%d is greatest“,a); } else { printf(“%d is greatest“,c); } } else { if( b> c) { printf(“%d is greatest“,b); } else { printf(“%d is greatest“,c); } } getch(); } Using Nested-if statement to check greatest number among three input numbers Enter 3 numbers: 9 11 5 11 is greatest Enter 3 numbers: 49 55 88 88 is greatest
  • 13.  Multi-way decision statement  Tests the value of a given variable against a list of case values  when a match is found, a block of statements associated with that case is executed. 13
  • 14. switch (var) { case case value 1: statements; break; case case value 2: statements; break; ……. ……. case case value n: default: statements; break; } Should be an integer or characters Known as case labels and should be constants or constant expressions And should be end with semicolon 14
  • 15. #include<stdio.h> int main() { char operator; float firstNumber,secondNumber; printf("Enter an operator (+, -): "); scanf("%c", &operator); printf("Enter two operands: "); scanf("%f %f",&firstNumber, &secondNumber); switch(operator) { case '+': printf("%f + %f = %f",firstNumber, secondNumber, firstNumber + secondNumber); break; case '-': printf("%f - %f = %f",firstNumber, secondNumber, firstNumber - secondNumber); break; default: printf("Error! operator is not correct"); } return 0; } Enter an operator(+,-): - Enter two operands: 6 4 6.000000 – 4.000000 = 2.000000 Enter an operator(+,-): / Enter two operands: 9 3 Error! operator is not correct 15
  • 16. The ?: operator Known as conditional operator Used for making two-way decisions Syntax: 16 Conditional expression ? exp1: exp2
  • 17. Example: 17 //If True first exp is executed //If False second exp is executed #include <stdio.h> #include <conio.h> #include <math.h> void main() { int a; printf("Enter a number: "); scanf("%d",&a); (a%2==0?printf("Even"):printf("Odd")); getch(); } Enter a number: 9 Odd Enter a number: 8 Even
  • 18. The GOTO statement Rarely used statement Provides an unconditional jump from ‘goto’ to a labelled statement in the same function Syntax: Requires label to identify the place where the branch is to be made 18 goto label; ------------- ------------- label; statement; label; statement; ------------- ------------- goto label; Forward jump Backward jump
  • 19. Example: 19 #include <stdio.h> #include <conio.h> #include <math.h> void main() { float y,x; read: printf("nEnter a number: "); scanf("%f",&y); if(y<0) goto read; x=sqrt(y); printf("nSquare root of %f---->%fn",y,x); goto read; } Enter a number: 9 Square root of 9.000000---->3.000000 Enter a number: 25 Square root of 25.000000---->5.000000