SlideShare une entreprise Scribd logo
1  sur  33
LOGICAL EXPRESSIONS
 IF STATEMENT
SWITCH STATEMENT
Selection Statements
Flow of Control
 Unless specified , the order of statement execution
through a C program is linear: one statement after
the other, in sequence.
 Some programming statements modify that
order, allowing us to:
 decide whether or not to execute a particular statement, or
perform a statement over and over, repetitively
3
Flow of Control
 These decisions are based on a boolean Or logical
expression (also called a condition) that evaluates to
true or false
 The order of statement execution is called the flow of
control
Flow of Control
Sequential Flow
Flow of Control
Selection Statements
Flow of Control
Repetition
Logical Expression
 Logical expression is an expression which uses one
or more logical operators, e.g.,
 (temperature > 90.0 && humidity > 0.90)
 !(n <= 0 || n >= 100).
 The output of the logical expression is the boolean
value either true or false.
If Statements
 If statements consists of boolean expression followed
by one or more statements.
 If the boolean expression evaluates to true, the
statements inside the block get executed otherwise
the first statement outside the block get executed.
 The false value is o and all the other values are
evaluated as true in C.
If Statement
 The syntax of an If statement in C Program is given
below
If Statements
If Statement(Example)
Output
If…else Statement
 If statements can be followed by the optional else
statements, which get executed when the boolean
expression is false.
If…else Statement
If…else Statement(Example)
If…else Statement
If…elseif…else Statement
 If statement can be followed by optional elseif..else
statement, which is very useful to test various
conditions using single if…elseif statement.
 Following things should be kept 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.
If…elseif…else Statement
If…elseif…else Statement(Example)
#include <stdio.h>
#include<conio.h>
int main ()
{
int a = 100;
if( a == 10 )
{
printf("Value of a is 10n" );
}
else if( a == 20 )
{
printf("Value of a is 20n" );
}
else if( a == 30 )
{
printf("Value of a is 30n" );
}
else
{
printf("None of the values is
matchingn" );
}
printf("Exact value of a is:
%dn", a );
getch();
return 0;
}
If…elseif…else Statement
Nested if Statements
 It is always legal in C programming to nest if-else
statements, which means we can use one if or else if
statement inside another if or else if statement(s).
Nested if Statements
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 100;
int b = 200;
if( a == 100 )
{
if( b == 200 )
{
printf("Value of a is 100
and b is 200n" );
}
}
printf("Exact value of a is :
%dn", a );
printf("Exact value of b is :
%dn", b );
getch();
return 0;
}
Nested if Statements
Switch Statement
 A switch statement allows a variable to be tested for
equality against a list of values.
 Each value is called a case, and the variable being
switched on is checked for each switch case.
 The following rules apply to a switch statement:
 The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
 You can have any number of case statements within a switch. Each
case is followed by the value to be compared to and a colon.
 The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
Switch Statement
 When the variable being switched on is equal to a case, the
statements following that case will execute until a break
statement is reached.
 When a break statement is reached, the switch terminates, and
the flow of control jumps to the next line following the switch
statement.
 Not every case needs to contain a break. If no break
appears, the flow of control will fall through to subsequent
cases until a break is reached.
 A switch statement can have an optional default case, which
must appear at the end of the switch. The default case can be
used for performing a task when none of the cases is true. No
break is needed in the default case.
Switch Statement
Switch Statement
Switch Statement
#include <stdio.h>
#include <conio.h>
int main ()
{
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!n" );
break;
case 'B' :
case 'C' :
printf("Well donen" );
break;
case 'D' :
printf("You passedn" );
break;
case 'F' :
printf("Better try againn" );
break;
default :
printf("Invalid graden" );
}
printf("Your grade is
%cn", grade );
getch();
return 0;
}
Switch Statement
Nested Switch Statements
 It is possible to have a switch as part of the statement
sequence of an outer switch.
 Even if the case constants of the inner and outer
switch contain common values, no conflicts will
arise.
Nested Switch Statements
Nested Switch Statements
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 100;
int b = 200;
switch(a)
{
case 100:
printf("This is part of outer
switchn", a );
switch(b)
{
case 200:
printf("This is part of inner
switchn", a );
}
}
printf("Exact value of a is : %dn", a
);
printf("Exact value of b is : %dn", b
);
getch();
return 0;
}
Nested Switch Statements

Contenu connexe

Tendances

Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 

Tendances (20)

Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Branching in C
Branching in CBranching in C
Branching in C
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Control statements
Control statementsControl statements
Control statements
 
Loops in C
Loops in CLoops in C
Loops in C
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 

En vedette

Nieuwe inzichten in de behandeling van ADHD
Nieuwe inzichten in de behandeling van ADHDNieuwe inzichten in de behandeling van ADHD
Nieuwe inzichten in de behandeling van ADHD
linuspauling
 

En vedette (20)

Selection statements
Selection statementsSelection statements
Selection statements
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
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
 
Conditional Statements | If-then Statements
Conditional Statements | If-then StatementsConditional Statements | If-then Statements
Conditional Statements | If-then Statements
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
Flow Control (C#)
Flow Control (C#)Flow Control (C#)
Flow Control (C#)
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
Iteration
IterationIteration
Iteration
 
Presentatie Adhd En Cogmed Velsen 2011
Presentatie Adhd En Cogmed Velsen 2011Presentatie Adhd En Cogmed Velsen 2011
Presentatie Adhd En Cogmed Velsen 2011
 
Seminar Time Management
Seminar Time ManagementSeminar Time Management
Seminar Time Management
 
說話的藝術
說話的藝術說話的藝術
說話的藝術
 
Life in 1500 History Lesson 1
Life in 1500 History Lesson 1Life in 1500 History Lesson 1
Life in 1500 History Lesson 1
 
National income
National incomeNational income
National income
 
Nieuwe inzichten in de behandeling van ADHD
Nieuwe inzichten in de behandeling van ADHDNieuwe inzichten in de behandeling van ADHD
Nieuwe inzichten in de behandeling van ADHD
 
Concentratiestoornissen
ConcentratiestoornissenConcentratiestoornissen
Concentratiestoornissen
 
Relatie en oplossingsgericht werken (Updated Version - 2013)
Relatie  en oplossingsgericht werken (Updated Version - 2013)Relatie  en oplossingsgericht werken (Updated Version - 2013)
Relatie en oplossingsgericht werken (Updated Version - 2013)
 
Time management
Time managementTime management
Time management
 
System Analysis and Design slides by yared yenealem DTU Ethiopia
System Analysis and Design slides by yared yenealem DTU EthiopiaSystem Analysis and Design slides by yared yenealem DTU Ethiopia
System Analysis and Design slides by yared yenealem DTU Ethiopia
 

Similaire à Selection Statements in C Programming

C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
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
alish 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_selection
alish sha
 

Similaire à Selection Statements in C Programming (20)

C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
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
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
03a control structures
03a   control structures03a   control structures
03a control structures
 

Plus de Kamal Acharya

Plus de Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

Selection Statements in C Programming

  • 1. LOGICAL EXPRESSIONS  IF STATEMENT SWITCH STATEMENT Selection Statements
  • 2. Flow of Control  Unless specified , the order of statement execution through a C program is linear: one statement after the other, in sequence.  Some programming statements modify that order, allowing us to:  decide whether or not to execute a particular statement, or perform a statement over and over, repetitively
  • 3. 3 Flow of Control  These decisions are based on a boolean Or logical expression (also called a condition) that evaluates to true or false  The order of statement execution is called the flow of control
  • 7. Logical Expression  Logical expression is an expression which uses one or more logical operators, e.g.,  (temperature > 90.0 && humidity > 0.90)  !(n <= 0 || n >= 100).  The output of the logical expression is the boolean value either true or false.
  • 8. If Statements  If statements consists of boolean expression followed by one or more statements.  If the boolean expression evaluates to true, the statements inside the block get executed otherwise the first statement outside the block get executed.  The false value is o and all the other values are evaluated as true in C.
  • 9. If Statement  The syntax of an If statement in C Program is given below
  • 13. If…else Statement  If statements can be followed by the optional else statements, which get executed when the boolean expression is false.
  • 17. If…elseif…else Statement  If statement can be followed by optional elseif..else statement, which is very useful to test various conditions using single if…elseif statement.  Following things should be kept 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.
  • 19. If…elseif…else Statement(Example) #include <stdio.h> #include<conio.h> int main () { int a = 100; if( a == 10 ) { printf("Value of a is 10n" ); } else if( a == 20 ) { printf("Value of a is 20n" ); } else if( a == 30 ) { printf("Value of a is 30n" ); } else { printf("None of the values is matchingn" ); } printf("Exact value of a is: %dn", a ); getch(); return 0; }
  • 21. Nested if Statements  It is always legal in C programming to nest if-else statements, which means we can use one if or else if statement inside another if or else if statement(s).
  • 22. Nested if Statements #include <stdio.h> #include <conio.h> int main () { int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { printf("Value of a is 100 and b is 200n" ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); getch(); return 0; }
  • 24. Switch Statement  A switch statement allows a variable to be tested for equality against a list of values.  Each value is called a case, and the variable being switched on is checked for each switch case.  The following rules apply to a switch statement:  The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.  You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.  The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  • 25. Switch Statement  When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.  Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.  A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
  • 28. Switch Statement #include <stdio.h> #include <conio.h> int main () { char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : case 'C' : printf("Well donen" ); break; case 'D' : printf("You passedn" ); break; case 'F' : printf("Better try againn" ); break; default : printf("Invalid graden" ); } printf("Your grade is %cn", grade ); getch(); return 0; }
  • 30. Nested Switch Statements  It is possible to have a switch as part of the statement sequence of an outer switch.  Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
  • 32. Nested Switch Statements #include <stdio.h> #include <conio.h> int main () { int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer switchn", a ); switch(b) { case 200: printf("This is part of inner switchn", a ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); getch(); return 0; }