SlideShare a Scribd company logo
B Y
Y N D ARAVIND
A S S O C I A T E P R O F E S S O R , D E P T O F C S E
N E W T O N ’ S G R O U P O F I N S T I T U T I O N S , M A C H E R L A
@2020 Presented By Mr.Y N D Aravind
1
SELECTION – MAKING
DECISIONS
P A R T - I I
L O G I C A L D A T A A N D O P E R A T O R S
T W O W A Y S E L E C T I O N
M U L T I W A Y S E L E C T I O N
M O R E S T A N D A R D F U N C T I O N S
@2020 Presented By Mr.Y N D Aravind
2
SELECTION – MAKING DECISIONS
Logical Data and Operators
Presented By Mr.Y N D Aravind
3
A piece of data is called logical if it gives information as true or
false.
For this, we have boolean data type called _bool, declared as
unsigned integer in stdbool.h file.
To support logical data we have to use bool type. Logical
operators are used to combine two or more relations.
The logical operators are called Boolean operators. Because the
test between values are reduced to either true or false, with
zero being false and one being true.
Logical Data and Operators
Presented By Mr.Y N D Aravind
4
Operator Meaning
&&
Logical AND
||
Logical OR
!
Logical NOT
AND OR NOT
X Y X || Y
False False False
False True True
True False True
True True True
X Y X && Y
False False False
False True False
True False False
True True True
X !X
False True
True False
Logical Data and Operators
Presented By Mr.Y N D Aravind
5
Evaluating Logical Expressions
There are two methods to evaluate binary logical relations. First, expression
must be completely evaluated before the result is determined. Second, sets
the result as soon as it is known.
If the first operand of the logical and expression is false then there is no
need to evaluate the second half of the expression. And in the case of
logical or if the first operand is true then there is no need to evaluate the
second half of the expression. This is known as short circuit evaluation.
False && Anything True || Anything
FALSE TRUE
Program to illustrate about logical operators
#include<stdio.h>
#include<stdbool.h>
int main()
{
bool a = true;
bool b = false;
printf(“%d”, a&&b);
printf(“%d”, a||b);
printf(“%d”, !a&&b);
printf(“%d”, a&&!b);
printf(“%d”, !a||b);
printf(“%d”, a||!b);
return 0;
}
Presented By Mr.Y N D Aravind
6
OUTPUT
010101
In addition to logical operators, C provides six comparative operators. They all are
binary operators. They take two operands and compare to produce Boolean
value.(true or false). They are divided into two groups.
1. Relational operators 2. Equality operators
The relational and equality operators
are listed below:
Operator Meaning
< Is less than
<= Is less than or equal to
> Is Greater than
>= Is Greater than or
equal to
== Is Equalto
!= Is Not Equal to
Comparative Operators
Presented By Mr.Y N D Aravind
7
For example, x>y, x<y, x==y,
x>=y, x<=y, x!=y
Operator Complement
< >=
> <=
== !=
Comparative Operators
If we want to simplify an expression containing not less than operators,
we use greater than or equal to operator.
Presented By Mr.Y N D Aravind
8
Original expression Simplified expression
!(x < y) x >= y
!(x > y) x <= y
!(x != y) x == y
!(x <= y) x > y
!(x >= y) x < y
!(x == y) x != y
CONDITIONAL STATEMENT
Decision making is about deciding the order of execution of statements based on
certain conditions or repeat a group of statements until certain specified
conditions are met. C language handles decision-making by supporting the
following statements,
1. If statement
2. Switch statement
3. Conditional operator statement
4. Go to statement
if Statement
The if statement is powerful decision making statement and is used to control the
flow of execution of statements. The if statement may be complexity of
conditions to be tested.
1. Simple if statement
2. If else statement
3. Nested If-else statement
4. Else –If ladder statement
Presented By Mr.Y N D Aravind
9
Simple – If Statement
Syntax :-
if (test expression)
{
Statement block;
}
Statement - x ;
SINGLE WAY SELECTION
Presented By Mr.Y N D Aravind
10
Flow Chart
The statement block may be a single statement or a group of statements. If the
test expression is true then the statement block will be executed.
Otherwise the statement block will be skipped and the execution will jump to
the statement –x. If the condition is true both the statement - block and statement-x
in sequence are executed.
Example Program
if(category = sports)
{
marks = marks + bonus marks;
}
printf(“%d”, marks);
 If the student belongs to the
sports category then additional
bonus marks are added to his
marks before they are printed.
For others bonus marks are not
added.
#include<stdio.h>
Void main()
{
int x;
clrscr();
printf(“enter the value of x”);
scanf(“%d”, &x);
if (x == 1)
printf(“ x value is one”);
printf(“%d”, x);
}
Simple – If Statement
Presented By Mr.Y N D Aravind
11
If – else Statement
Syntax :-
if (test expression)
{
True block - 1;
}
Else
{
False block – 2;
}
Statement - x ;
TWO WAY SELECTION
Presented By Mr.Y N D Aravind
12
Points to note
 The expression must be enclosed in
parenthesis.
 No semicolon is required for if else
statement
 Both true and false block may
contain null statement or another if
else statement
 Both blocks may contain either one
statement or group of statements if
group then they should be presented
between a pair of braces called
compound statement.
Example Program
If (code == 1)
boy = boy + 1;
else
girl = girl + 1;
st-x;
 Here if the code is equal to „1‟ the statement
boy=boy+1; is executed and the control is
transferred to the statement st-x, after
skipping the else part. If code is not equal to
„1‟ the statement boy =boy+1; is skipped
and the statement in the else part girl
=girl+1; is executed before the control
reaches the statement st-x.
#include<stdio.h>
#include<conio.h>
Void main()
{
int num;
clrscr();
printf(“enter the value “);
scanf(„%d”, &num);
if(num%2==0)
printf(“the number is even”);
else
printf(“the number is odd”);
}
If - else Statement
Presented By Mr.Y N D Aravind
13
If – else Statement
TWO WAY SELECTION
Presented By Mr.Y N D Aravind
14
Points to note
 The expression is a C
expression. After its evaluation
its value is either true or false.
 If the test expression is true
then true-block statements are
executed, otherwise the false–
block statements are executed.
 In both cases either true-block
or false-block will be executed
but not both.
Flow Chart
Nested If – else Statement
Syntax :-
if(test expression1)
{
if(test expression2)
{ statement block –1;
}
else
{ statement block – 2;
}
}
else
{ statement block – 3;
}
Statement – x;
TWO WAY SELECTION
Presented By Mr.Y N D Aravind
15
Test
expression
St- 1 St-3St- 2
Test
expression
St -x
Flow Chart
When a series of decisions are involved we may have to use
more than one if-else statement in nested form. An if-else is
included in another if-else is known as nested if else statement.
Example Program
if(sex ==female)
{
if(balance>5000)
{
bonus=0.5*balance;
else
bonus=0.2*balance;
}
}
else
{
bonus=0.6*balance;
}
balance=balance+bonus;
printf(“%f”, balance);
#include<stdio.h>
Void main()
{
int a, b, c;
printf(“enter the values of a, b, c”);
scanf(“%d%d%d”, &a, &b, &c);
if(a>b)
{
if(a>c)
printf(“%d a is big”, a);
else
printf(“%d c is big”, c);
}
else
if(c>b)
printf(“%d c is big”, c);
else
printf(“%d b is big”, b);
}
Nested If - else Statement
Presented By Mr.Y N D Aravind
16
Dangling else problem
In nested if-else we have a problem known as dangling else problem. This problem
is created when there is no matching else for every if. In C, we have the simple
solution. “always pair an else to most recent unpaired if in the current
block”.
But however it may lead to problems hence the solution is to simplify if statements by putting braces.
if(test expression1)
{
if(test expression2)
st –1;
}
else
st – 2;
st – x;
Presented By Mr.Y N D Aravind
17
else - if ladder
Syntax :-
if (condition1)
St block–1;
else if (condition2)
St block –2;
else if (condition 3)
St block –3;
else
St block -4;
St –x;
MULTI WAY SELECTION
Presented By Mr.Y N D Aravind
18
A multi path decision is chain of if’s in which the
statement associated with each else is an if.
Flow Chart
Example Program
if (code = = 1)
Color = “red”;
else if ( code = = 2)
Color = “green”
else if (code = = 3)
Color = “white”;
else
Color = “yellow”
 If code number is other than 1, 2 and 3
then color is yellow.
#include<stdio.h>
Void main()
{ int m1,m2,m3,m4,m5, total; float per;
printf(“enter the marks for five subjects”);
scanf(“%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5);
total = m1 + m2 + m3 + m4 + m5;
per = total / 500;
printf(“%f”, per);
if(per >= 90)
printf(“A”);
else if(per >= 80)
printf(“B”);
else if(per >= 70)
printf(“C”);
else if(per >= 60)
printf(“D”);
else
printf(“F”);
}

else - if ladder
Presented By Mr.Y N D Aravind
19
Switch Statement
Syntax :-
switch (expression)
{
case value1 : block1;
break;
case value 2 : block 2;
break;
default : default block;
break;
}
st – x;
MULTI WAY SELECTION
Presented By Mr.Y N D Aravind
20
Instead of else – if ladder, ‘C’ has a built-in multi-
way decision statement known as a switch.
Flow Chart
Switch
(Expression)
Block 1 Block 2 Default
St - x
Program Program Cont
main()
{
int a , b , choice;
float c;
clrscr();
printf(“enter the values of a & b”);
scanf(“%d%d”, &a, &b);
printf(“enter the choice”);
scanf(“%d”, &choice);
switch(choice)
{
case „1‟ : c=a+b;
printf(“%f”, c);
break;
case „2‟ : c=a-b;
printf(“%f”, c);
break;
case „3‟ : c=a/b;
printf(“%f”, c);
break;
case „4‟ : c=a*b;
printf(“%f”, c);
break;
case „5‟ : c=a%b;
printf(“%f”, c);
break;
default case : printf(“Invalid option”);
break;
} getch();
}
Switch Statement
Presented By Mr.Y N D Aravind
21
Y. N. D. ARAVIND
ASSOCIATE PROFESSOR, DEPT OF CSE
N E W T O N ’ S G R O U P O F I N S T I T U T I O N S , M A C H E R L A
Presented By Mr.Y N D Aravind
22
Thank YouThank You

More Related Content

What's hot

Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
C functions
C functionsC functions
C++
C++C++
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
 
Function
FunctionFunction
Function
yash patel
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Harsh Patel
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
History of C Programming Language
History of C Programming LanguageHistory of C Programming Language
History of C Programming Language
Niloy Biswas
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
Saket Pathak
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
YeurDreamin'
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
RITHIKA R S
 

What's hot (20)

Generics C#
Generics C#Generics C#
Generics C#
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
C functions
C functionsC functions
C functions
 
C++
C++C++
C++
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
interface in c#
interface in c#interface in c#
interface in c#
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
Function
FunctionFunction
Function
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
History of C Programming Language
History of C Programming LanguageHistory of C Programming Language
History of C Programming Language
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
This pointer
This pointerThis pointer
This pointer
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Call by value
Call by valueCall by value
Call by value
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 

Similar to Selection & Making Decisions in c

Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
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
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
NabishaAK
 
C tutorial
C tutorialC tutorial
C tutorial
Anurag Sukhija
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
Mahbubay Rabbani Mim
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
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
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
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 statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
Moni Adhikary
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
Elsayed Hemayed
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
yarkhosh
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 

Similar to Selection & Making Decisions in c (20)

Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
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
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
C tutorial
C tutorialC tutorial
C tutorial
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
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
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
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 statments in c
Control statments in cControl statments in c
Control statments in c
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
Csci101 lect04 advanced_selection
Csci101 lect04 advanced_selectionCsci101 lect04 advanced_selection
Csci101 lect04 advanced_selection
 
C fundamental
C fundamentalC fundamental
C fundamental
 
L3 control
L3 controlL3 control
L3 control
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 

More from yndaravind

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
yndaravind
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
yndaravind
 
The Object Model
The Object Model  The Object Model
The Object Model
yndaravind
 
OOAD
OOADOOAD
OOAD
OOADOOAD
FILES IN C
FILES IN CFILES IN C
FILES IN C
yndaravind
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
yndaravind
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
yndaravind
 
Structure In C
Structure In CStructure In C
Structure In C
yndaravind
 
Strings part2
Strings part2Strings part2
Strings part2
yndaravind
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
yndaravind
 
Functions part1
Functions part1Functions part1
Functions part1
yndaravind
 

More from yndaravind (14)

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
The Object Model
The Object Model  The Object Model
The Object Model
 
OOAD
OOADOOAD
OOAD
 
OOAD
OOADOOAD
OOAD
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
 
Structure In C
Structure In CStructure In C
Structure In C
 
Strings part2
Strings part2Strings part2
Strings part2
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions part1
Functions part1Functions part1
Functions part1
 

Recently uploaded

Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Selection & Making Decisions in c

  • 1. B Y Y N D ARAVIND A S S O C I A T E P R O F E S S O R , D E P T O F C S E N E W T O N ’ S G R O U P O F I N S T I T U T I O N S , M A C H E R L A @2020 Presented By Mr.Y N D Aravind 1 SELECTION – MAKING DECISIONS
  • 2. P A R T - I I L O G I C A L D A T A A N D O P E R A T O R S T W O W A Y S E L E C T I O N M U L T I W A Y S E L E C T I O N M O R E S T A N D A R D F U N C T I O N S @2020 Presented By Mr.Y N D Aravind 2 SELECTION – MAKING DECISIONS
  • 3. Logical Data and Operators Presented By Mr.Y N D Aravind 3 A piece of data is called logical if it gives information as true or false. For this, we have boolean data type called _bool, declared as unsigned integer in stdbool.h file. To support logical data we have to use bool type. Logical operators are used to combine two or more relations. The logical operators are called Boolean operators. Because the test between values are reduced to either true or false, with zero being false and one being true.
  • 4. Logical Data and Operators Presented By Mr.Y N D Aravind 4 Operator Meaning && Logical AND || Logical OR ! Logical NOT AND OR NOT X Y X || Y False False False False True True True False True True True True X Y X && Y False False False False True False True False False True True True X !X False True True False
  • 5. Logical Data and Operators Presented By Mr.Y N D Aravind 5 Evaluating Logical Expressions There are two methods to evaluate binary logical relations. First, expression must be completely evaluated before the result is determined. Second, sets the result as soon as it is known. If the first operand of the logical and expression is false then there is no need to evaluate the second half of the expression. And in the case of logical or if the first operand is true then there is no need to evaluate the second half of the expression. This is known as short circuit evaluation. False && Anything True || Anything FALSE TRUE
  • 6. Program to illustrate about logical operators #include<stdio.h> #include<stdbool.h> int main() { bool a = true; bool b = false; printf(“%d”, a&&b); printf(“%d”, a||b); printf(“%d”, !a&&b); printf(“%d”, a&&!b); printf(“%d”, !a||b); printf(“%d”, a||!b); return 0; } Presented By Mr.Y N D Aravind 6 OUTPUT 010101
  • 7. In addition to logical operators, C provides six comparative operators. They all are binary operators. They take two operands and compare to produce Boolean value.(true or false). They are divided into two groups. 1. Relational operators 2. Equality operators The relational and equality operators are listed below: Operator Meaning < Is less than <= Is less than or equal to > Is Greater than >= Is Greater than or equal to == Is Equalto != Is Not Equal to Comparative Operators Presented By Mr.Y N D Aravind 7 For example, x>y, x<y, x==y, x>=y, x<=y, x!=y Operator Complement < >= > <= == !=
  • 8. Comparative Operators If we want to simplify an expression containing not less than operators, we use greater than or equal to operator. Presented By Mr.Y N D Aravind 8 Original expression Simplified expression !(x < y) x >= y !(x > y) x <= y !(x != y) x == y !(x <= y) x > y !(x >= y) x < y !(x == y) x != y
  • 9. CONDITIONAL STATEMENT Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements, 1. If statement 2. Switch statement 3. Conditional operator statement 4. Go to statement if Statement The if statement is powerful decision making statement and is used to control the flow of execution of statements. The if statement may be complexity of conditions to be tested. 1. Simple if statement 2. If else statement 3. Nested If-else statement 4. Else –If ladder statement Presented By Mr.Y N D Aravind 9
  • 10. Simple – If Statement Syntax :- if (test expression) { Statement block; } Statement - x ; SINGLE WAY SELECTION Presented By Mr.Y N D Aravind 10 Flow Chart The statement block may be a single statement or a group of statements. If the test expression is true then the statement block will be executed. Otherwise the statement block will be skipped and the execution will jump to the statement –x. If the condition is true both the statement - block and statement-x in sequence are executed.
  • 11. Example Program if(category = sports) { marks = marks + bonus marks; } printf(“%d”, marks);  If the student belongs to the sports category then additional bonus marks are added to his marks before they are printed. For others bonus marks are not added. #include<stdio.h> Void main() { int x; clrscr(); printf(“enter the value of x”); scanf(“%d”, &x); if (x == 1) printf(“ x value is one”); printf(“%d”, x); } Simple – If Statement Presented By Mr.Y N D Aravind 11
  • 12. If – else Statement Syntax :- if (test expression) { True block - 1; } Else { False block – 2; } Statement - x ; TWO WAY SELECTION Presented By Mr.Y N D Aravind 12 Points to note  The expression must be enclosed in parenthesis.  No semicolon is required for if else statement  Both true and false block may contain null statement or another if else statement  Both blocks may contain either one statement or group of statements if group then they should be presented between a pair of braces called compound statement.
  • 13. Example Program If (code == 1) boy = boy + 1; else girl = girl + 1; st-x;  Here if the code is equal to „1‟ the statement boy=boy+1; is executed and the control is transferred to the statement st-x, after skipping the else part. If code is not equal to „1‟ the statement boy =boy+1; is skipped and the statement in the else part girl =girl+1; is executed before the control reaches the statement st-x. #include<stdio.h> #include<conio.h> Void main() { int num; clrscr(); printf(“enter the value “); scanf(„%d”, &num); if(num%2==0) printf(“the number is even”); else printf(“the number is odd”); } If - else Statement Presented By Mr.Y N D Aravind 13
  • 14. If – else Statement TWO WAY SELECTION Presented By Mr.Y N D Aravind 14 Points to note  The expression is a C expression. After its evaluation its value is either true or false.  If the test expression is true then true-block statements are executed, otherwise the false– block statements are executed.  In both cases either true-block or false-block will be executed but not both. Flow Chart
  • 15. Nested If – else Statement Syntax :- if(test expression1) { if(test expression2) { statement block –1; } else { statement block – 2; } } else { statement block – 3; } Statement – x; TWO WAY SELECTION Presented By Mr.Y N D Aravind 15 Test expression St- 1 St-3St- 2 Test expression St -x Flow Chart When a series of decisions are involved we may have to use more than one if-else statement in nested form. An if-else is included in another if-else is known as nested if else statement.
  • 16. Example Program if(sex ==female) { if(balance>5000) { bonus=0.5*balance; else bonus=0.2*balance; } } else { bonus=0.6*balance; } balance=balance+bonus; printf(“%f”, balance); #include<stdio.h> Void main() { int a, b, c; printf(“enter the values of a, b, c”); scanf(“%d%d%d”, &a, &b, &c); if(a>b) { if(a>c) printf(“%d a is big”, a); else printf(“%d c is big”, c); } else if(c>b) printf(“%d c is big”, c); else printf(“%d b is big”, b); } Nested If - else Statement Presented By Mr.Y N D Aravind 16
  • 17. Dangling else problem In nested if-else we have a problem known as dangling else problem. This problem is created when there is no matching else for every if. In C, we have the simple solution. “always pair an else to most recent unpaired if in the current block”. But however it may lead to problems hence the solution is to simplify if statements by putting braces. if(test expression1) { if(test expression2) st –1; } else st – 2; st – x; Presented By Mr.Y N D Aravind 17
  • 18. else - if ladder Syntax :- if (condition1) St block–1; else if (condition2) St block –2; else if (condition 3) St block –3; else St block -4; St –x; MULTI WAY SELECTION Presented By Mr.Y N D Aravind 18 A multi path decision is chain of if’s in which the statement associated with each else is an if. Flow Chart
  • 19. Example Program if (code = = 1) Color = “red”; else if ( code = = 2) Color = “green” else if (code = = 3) Color = “white”; else Color = “yellow”  If code number is other than 1, 2 and 3 then color is yellow. #include<stdio.h> Void main() { int m1,m2,m3,m4,m5, total; float per; printf(“enter the marks for five subjects”); scanf(“%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5); total = m1 + m2 + m3 + m4 + m5; per = total / 500; printf(“%f”, per); if(per >= 90) printf(“A”); else if(per >= 80) printf(“B”); else if(per >= 70) printf(“C”); else if(per >= 60) printf(“D”); else printf(“F”); }  else - if ladder Presented By Mr.Y N D Aravind 19
  • 20. Switch Statement Syntax :- switch (expression) { case value1 : block1; break; case value 2 : block 2; break; default : default block; break; } st – x; MULTI WAY SELECTION Presented By Mr.Y N D Aravind 20 Instead of else – if ladder, ‘C’ has a built-in multi- way decision statement known as a switch. Flow Chart Switch (Expression) Block 1 Block 2 Default St - x
  • 21. Program Program Cont main() { int a , b , choice; float c; clrscr(); printf(“enter the values of a & b”); scanf(“%d%d”, &a, &b); printf(“enter the choice”); scanf(“%d”, &choice); switch(choice) { case „1‟ : c=a+b; printf(“%f”, c); break; case „2‟ : c=a-b; printf(“%f”, c); break; case „3‟ : c=a/b; printf(“%f”, c); break; case „4‟ : c=a*b; printf(“%f”, c); break; case „5‟ : c=a%b; printf(“%f”, c); break; default case : printf(“Invalid option”); break; } getch(); } Switch Statement Presented By Mr.Y N D Aravind 21
  • 22. Y. N. D. ARAVIND ASSOCIATE PROFESSOR, DEPT OF CSE N E W T O N ’ S G R O U P O F I N S T I T U T I O N S , M A C H E R L A Presented By Mr.Y N D Aravind 22 Thank YouThank You