EECE 1208 - University of Memphis

Download Report

Transcript EECE 1208 - University of Memphis

EECE 1207
Fall 2005
Lecture 4
More conditionals
Jul-15
Salem
EECE1207
Topics
•
•
•
•
•
Compound if statements
Nested if statements
If – else chains
Switch statements
DeMorgan’s law
Jul-15
Salem
EECE1207
Introduction 2
Multiple actions
• What if there’s more than one conditional
action?
• Ex:
“If your temperature is high, then you have a
fever and should take 2 aspirins, then go to
bed, and call in sick tomorrow”
Jul-15
Salem
EECE1207
Introduction 3
Compound statements
• Group together statements so that they are treated as a
single statement:
if (condition)
{
statement1;
statement2;
…
}
• Also called a “block”
• Useful not just in conditionals, but also in other structures
in C
Jul-15
Salem
EECE1207
Introduction 4
Compound statements
if (temperature > 98.5) {
printf (“you have fever. \n”);
aspirin = aspirin –2;
printf (“go to bed. \n”);
printf (“call in sick tomorrow. \n”);
}
Jul-15
Salem
EECE1207
Introduction 5
What if { } are omitted
Another dramatic example
if (DangerLevel > 5){
openhatch();
ejectseat();
}
if (DangerLevel > 5)
openhatch();
ejectseat();
Jul-15
EECE1207
Salem
Introduction 6
Compound if-else
if (Nhours<=40) {
salary = Nhours*rate;
printf (“no overtime. salary = %f\n”, salary );
}
else {
salary = 40*rate + overtime();
printf (“overtime. salary = %f\n”, salary );
}
Jul-15
Salem
EECE1207
Introduction 7
Flow chart for
compound if-else
NHours<=40
Yes
Statement
Statement
Statement
No
Statement
Statement
Statement
Next statements
Jul-15
Salem
EECE1207
Introduction 8
Compound if-else statements
if (gpa >= 3.8)
printf (“Magna Cum Laude”);
else if (gpa >= 3.0)
printf (“Summa Cum Laude”);
else if (gpa >= 2.0)
printf (“Good standing”);
else
printf (“I ETA PI”);
gpa >= 3.8
Yes
Output “Magna
Cum Laude”
No
Yes
Output “Summa
Cum Laude”
Gpa >=3.0
No
Yes
Gpa >=2.0
Output “I ETA
PI”
No
printf (“end of transcript”);
Output “on
probation”
Output “end of
transcript”
Jul-15
Salem
EECE1207
Introduction 9
Switch statements
Yes
switch (m_status){
Output “single”
m_status = ‘s’
case ‘s’ :
printf (“single”);
No
break; //optional but recommended
Yes
case ‘m’:
m_status = ‘m’
Output
printf (“married”);
“married”
No
break; //optional but recommended
case ‘w’ :
Yes
Output
printf (“widowed”);
m_status = ‘w’
“widowed”
break; //optional but recommended
No
Default: //optional but recommended
Output “status
printf (“status unknown”);
unknown”
}
printf (“end of decoding marital status”);
Output “end of
decoding
marital status”
Jul-15
Salem
EECE1207
Introduction 10
Switch statements
multiple decisions
Switch (m_status){
case ‘s’ : case ‘S’ :
printf (“single”);
break; //optional but recommended
case ‘m’: case ‘M’ :
printf (“married”);
break; //optional but recommended
case ‘w’ : case ‘W’ :
printf (“widowed”);
break; //optional but recommended
Default: //optional but recommended
printf (“status unknown”);
}
printf (“end of decoding marital
status”);
Jul-15
Salem
‘s’ or ‘S’
Yes
Output “single”
No
Yes
‘m’ or ‘M’
Output
“married”
No
‘w’ or ‘W’
Yes
Output
“widowed”
No
Output “status
unknown”
EECE1207
Output “end of
decoding
marital status”
Introduction 11
Switch with no breaks?
Switch (m_status){
case ‘s’ : case ‘S’ :
printf (“single”);
case ‘m’: case ‘M’ :
printf (“married”);
case ‘w’ : case ‘W’ :
printf (“widowed”);
Default: //optional but
recommended
printf (“status unknown”);
}
printf (“end of decoding marital
status”);
Jul-15
Salem
‘s’ or ‘S’
Yes
Output “single”
No
Yes
‘m’ or ‘M’
Output
“married”
No
‘w’ or ‘W’
Yes
Output
“widowed”
No
Output “status
unknown”
EECE1207
Output “end of
decoding
marital status”
Introduction 12
Switch
• Switch is used ONLY with integers or
characters
• IS not used with floats/doubles
Jul-15
Salem
EECE1207
Introduction 13
Continued on blackboard
Jul-15
Salem
EECE1207