SlideShare a Scribd company logo
Decision making
statements in C
1Presented by: Group-E (The Anonymous)
Rabin BK
Bikash Dhakal
Bimal Pradhan
Bikram Bhurtel
Gagan Puri
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

PDF
Python programming : Files
ODP
OOP java
PDF
TFLite NNAPI and GPU Delegates
PPTX
php user defined functions
PDF
“A Practical Guide to Implementing ML on Embedded Devices,” a Presentation fr...
PPTX
File handling in c++
PPTX
structured programming
PPTX
Virtual machines and their architecture
Python programming : Files
OOP java
TFLite NNAPI and GPU Delegates
php user defined functions
“A Practical Guide to Implementing ML on Embedded Devices,” a Presentation fr...
File handling in c++
structured programming
Virtual machines and their architecture

What's hot (20)

PPTX
Three Address code
PDF
Introduction to OpenMP
PPTX
INSTRUCTION LEVEL PARALLALISM
PPT
Operators in C++
PPTX
Pointers, virtual function and polymorphism
PPT
Storage classes
PPTX
Parameterized Constructor
PPTX
C# Delegates
PDF
Fragments In Android
PDF
Files and streams
PPT
Asp.net control
PPTX
Inheritance in c++
PPTX
Operator.ppt
PPTX
C functions
PPTX
Storage classes in c++
PPTX
Looping Statements and Control Statements in Python
PPT
Oop Presentation
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
Three Address code
Introduction to OpenMP
INSTRUCTION LEVEL PARALLALISM
Operators in C++
Pointers, virtual function and polymorphism
Storage classes
Parameterized Constructor
C# Delegates
Fragments In Android
Files and streams
Asp.net control
Inheritance in c++
Operator.ppt
C functions
Storage classes in c++
Looping Statements and Control Statements in Python
Oop Presentation
C Programming Language Tutorial for beginners - JavaTpoint
Ad

Similar to Decision making statements in C (20)

PPTX
presentation of data structure and algorithm
PPTX
Decision Making and Branching
PPTX
chapter 3enejngewngjnjnfjndnennwnnefwf.pptx
PPTX
Basics of Control Statement in C Languages
PDF
Control statements-Computer programming
PPTX
CH-4 (1).pptx
PPTX
Branching statements
PPT
Decision making in C(2020-2021) statements
PPTX
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
PPTX
control_structures_c_language_regarding how to represent the loop in language...
PPTX
Control Structures in C
PDF
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
PPTX
computer programming Control Statements.pptx
PPTX
Conditional statements
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
Control statement-Selective
PDF
Programming basics
 
PPTX
unit 2-Control Structures.pptx
DOCX
Branching
PDF
4_Decision Making and Branching in C Program.pdf
presentation of data structure and algorithm
Decision Making and Branching
chapter 3enejngewngjnjnfjndnennwnnefwf.pptx
Basics of Control Statement in C Languages
Control statements-Computer programming
CH-4 (1).pptx
Branching statements
Decision making in C(2020-2021) statements
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
control_structures_c_language_regarding how to represent the loop in language...
Control Structures in C
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
computer programming Control Statements.pptx
Conditional statements
Unit ii chapter 2 Decision making and Branching in C
Control statement-Selective
Programming basics
 
unit 2-Control Structures.pptx
Branching
4_Decision Making and Branching in C Program.pdf
Ad

More from Rabin BK (20)

PPTX
Artificial Intelligence in E-commerce
PPTX
Three address code generation
PPTX
Consumer Oriented Application, Mercantile process and Mercantile models
PPTX
Clang compiler `
PPTX
Simple Mail Transfer Protocol
PPTX
HTML text formatting tags
PPTX
Data encryption in database management system
PPTX
Object Relational Database Management System(ORDBMS)
PPTX
Kolmogorov Smirnov
PPTX
Job sequencing in Data Strcture
PPTX
Stack Data Structure
PPTX
Bluetooth
PPTX
Data Science
PPTX
Graphics_3D viewing
PPTX
Neural Netwrok
PPTX
Watermarking in digital images
PPTX
Heun's Method
PPTX
Mutual Exclusion
PPTX
Systems Usage
PPTX
Manager of a company
Artificial Intelligence in E-commerce
Three address code generation
Consumer Oriented Application, Mercantile process and Mercantile models
Clang compiler `
Simple Mail Transfer Protocol
HTML text formatting tags
Data encryption in database management system
Object Relational Database Management System(ORDBMS)
Kolmogorov Smirnov
Job sequencing in Data Strcture
Stack Data Structure
Bluetooth
Data Science
Graphics_3D viewing
Neural Netwrok
Watermarking in digital images
Heun's Method
Mutual Exclusion
Systems Usage
Manager of a company

Recently uploaded (20)

PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Essential Infomation Tech presentation.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
AI in Product Development-omnex systems
PPTX
Introduction to Artificial Intelligence
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms II-SECS-1021-03
2025 Textile ERP Trends: SAP, Odoo & Oracle
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Essential Infomation Tech presentation.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Upgrade and Innovation Strategies for SAP ERP Customers
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
history of c programming in notes for students .pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
AI in Product Development-omnex systems
Introduction to Artificial Intelligence
Which alternative to Crystal Reports is best for small or large businesses.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PTS Company Brochure 2025 (1).pdf.......
VVF-Customer-Presentation2025-Ver1.9.pptx
ai tools demonstartion for schools and inter college
Softaken Excel to vCard Converter Software.pdf
Wondershare Filmora 15 Crack With Activation Key [2025

Decision making statements in C

  • 1. Decision making statements in C 1Presented by: Group-E (The Anonymous) Rabin BK Bikash Dhakal Bimal Pradhan Bikram Bhurtel Gagan Puri
  • 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