Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
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.
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