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

PPTX
Variables, Data Types, Operator & Expression in c in detail
PPTX
Function in c language(defination and declaration)
PPT
C operator and expression
PPTX
Introduction to c programming language
PPTX
Decision making statements in C programming
DOCX
Pseudo code practice problems+ c basics
PPT
C material
PPTX
Types of methods in python
Variables, Data Types, Operator & Expression in c in detail
Function in c language(defination and declaration)
C operator and expression
Introduction to c programming language
Decision making statements in C programming
Pseudo code practice problems+ c basics
C material
Types of methods in python

What's hot (20)

PPTX
building blocks of algorithm.pptx
PPTX
Type Conversion, Precedence and Associativity
PPSX
Conditional statement
PDF
Poset in Relations(Discrete Mathematics)
PPT
Introduction to Algorithms & flow charts
PPTX
Polymorphism in c++(ppt)
PPT
5.2 Strong Induction
PDF
Programming for Problem Solving
PPTX
Functions in C
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
PDF
Graph theory narsingh deo
PPTX
PPTX
Introduction to c++
DOCX
C – operators and expressions
PPTX
Module 3-Functions
PPTX
Expression and Operartor In C Programming
PPTX
Top down parsing
PDF
2nd PUC computer science chapter 2 boolean algebra 1
PPTX
Loops in C Programming Language
PPTX
Python With MongoDB in advanced Python.pptx
building blocks of algorithm.pptx
Type Conversion, Precedence and Associativity
Conditional statement
Poset in Relations(Discrete Mathematics)
Introduction to Algorithms & flow charts
Polymorphism in c++(ppt)
5.2 Strong Induction
Programming for Problem Solving
Functions in C
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
Graph theory narsingh deo
Introduction to c++
C – operators and expressions
Module 3-Functions
Expression and Operartor In C Programming
Top down parsing
2nd PUC computer science chapter 2 boolean algebra 1
Loops in C Programming Language
Python With MongoDB in advanced Python.pptx
Ad

Similar to Selection & Making Decisions in c (20)

PDF
ICP - Lecture 7 and 8
PPTX
Programming note C#
PPT
Chaptfffffuuer05.PPT
PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
PPT
Control statments in c
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PPTX
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
PDF
Fundamentals of Computer Programming - Flow of Control I
PDF
Programming Fundamentals Decisions
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
PPTX
Ch5 Selection Statements
PPT
Fcp chapter 2 upto_if_else (2)
PDF
Programming for Problem Solving
PPTX
Decision Making and Branching
PDF
C++ problem solving operators ( conditional operators,logical operators, swit...
PPT
Mesics lecture 6 control statement = if -else if__else
PPTX
Computer Programming - if Statements & Relational Operators
PPTX
COM1407: Program Control Structures – Decision Making & Branching
PDF
Control statements anil
ICP - Lecture 7 and 8
Programming note C#
Chaptfffffuuer05.PPT
unit2 C-ProgrammingChapter 2 Control statements.pptx
Control statments in c
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
Fundamentals of Computer Programming - Flow of Control I
Programming Fundamentals Decisions
[ITP - Lecture 08] Decision Control Structures (If Statement)
Ch5 Selection Statements
Fcp chapter 2 upto_if_else (2)
Programming for Problem Solving
Decision Making and Branching
C++ problem solving operators ( conditional operators,logical operators, swit...
Mesics lecture 6 control statement = if -else if__else
Computer Programming - if Statements & Relational Operators
COM1407: Program Control Structures – Decision Making & Branching
Control statements anil
Ad

More from yndaravind (14)

PDF
Introduction to UML
PDF
Classes and Objects
PDF
The Object Model
PDF
PDF
PDF
FILES IN C
PDF
Repetations in C
PPT
Bitwise Operators in C
PDF
Structure In C
PDF
Strings part2
PDF
Arrays In C
PDF
Strings IN C
PDF
Fnctions part2
PDF
Functions part1
Introduction to UML
Classes and Objects
The Object Model
FILES IN C
Repetations in C
Bitwise Operators in C
Structure In C
Strings part2
Arrays In C
Strings IN C
Fnctions part2
Functions part1

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Trump Administration's workforce development strategy
PPTX
Lesson notes of climatology university.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
01-Introduction-to-Information-Management.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Weekly quiz Compilation Jan -July 25.pdf
Trump Administration's workforce development strategy
Lesson notes of climatology university.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
A systematic review of self-coping strategies used by university students to ...
01-Introduction-to-Information-Management.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Computing-Curriculum for Schools in Ghana
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.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