Вы находитесь на странице: 1из 11

ICS 103: Computer Programming in C

Lab #4: Selection Statements


Objectives:
1. Learn to use if and switch statements:
Two-way selection if-else statement!
One-way selection if-statement!
"ulti-way selection with else o#tion if - else if $ else!
"ulti-way selection with no else o#tion if $ else if $ else if!
%ested if statements
switch statement with default o#tion
switch statement with no default o#tion
&. Learn to use functions of ty#e void to dis#lay menu o#tions
Two-way selection if-else statement!
'n if-else statement is used to e(ecute a statement or a com#ound-statement when a condition is
true) and another statement or com#ound-statement when that condition is false.
Sin*le statements +om#ound statements
if(condition)
statement1;
else
statement2;
if(condition)
compound_statement1
else
compound_statement2
,(ecute statement1 if condition is true else
e(ecute statement2
,(ecute compound_statement1 if condition is
true else e(ecute compound_statement2
%ote:
-t is a synta( error to #ut a semicolon
after the closin* brace of
compound_statement1
' com#ound statement may contain
.ero or more statements
,(am#le: /indin* ma( of two numbers
if( 01 y!
ma( 1 ()
else
ma( 1 y)
,(am#le:
scanf23c24 5currencyTy#e!)
ifcurrencyTy#e 1 1 6s6 77 currencyTy#e 1 1 6S6!8
#rintf2,nter #ositive amount 2!)
scanf23lf24 5amount!)
riyal9alance 1 riyal9alance : amount)
;
else
1
#rintf2<ron* currency ty#e2!)
One-way selection if-statement!
=sed to e(ecute a statement or a compound_statement when a condition is true.
Sin*le statements +om#ound statements
if(condition)
statement1;
ifcondition!
com#ound>statement1
,(ecute statement1 if condition is true ,(ecute compound_statement1 if condition is
true
#rintf?,nter a number: ?!)
scanf?3lf@4 5num!)
if num 0 A!
#rintf?The sBuare root is 3lf@4 sBrtnum!!)
#rintf?,nter a number: ?!)
scanf?3lf@4 5num!)
if num 0 A! 8
#rintf?The sBuare root is 3lf@4 sBrtnum!!)
#rintf?The natural lo*arithm is 3lf@4 lo*num!!)
;
,(am#le: /indin* ma( of three numbers
ma( 1 num1)
ifnum& 0 ma(!
ma( 1 num&)
ifnumC 0 ma(!
ma( 1 numC)

,(am#le:
scanf23c24 5currencyTy#e!)
ifcurrencyTy#e 11 6s6 77 currencyTy#e 11 6S6!
8
#rintf2,nter #ositive amount 2!)
scanf23lf24 5amount!)
riyal9alance 1 riyal9alance : amount)
;
"ulti-way selection with an else o#tion if- else if - else statement!
=sed to e(ecute the first statement or the first compound_statement whose corres#ondin*
condition is true. The statement in the else #art is e(ecuted if each condition is false.
Sin*le statements +om#ound statements
if(condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement3;
.
.
.
else if(condition)
statement;
else
statement!;

if(condition1)
compound_statement1
else if(condition2)
compound_statement2
else if(condition3)
compound_statement3
.
.
.
else if(condition)
compound_statement
else
compound_Statement!
&
%ote: There may be one or more else if
branches
%ote:
There may be one or more else if
branches.
-t is a synta( error to #ut a semicolon
after the closin* brace of a com#ound
statement in an if branch.
int "alid#rade $ 1; // set validGrade to true
dou%le grade;
printf(&'nter grade&);
scanf(&(lf&) *grade);
if(grade + 0.0 ,, grade - 100.0)
"alid#rade $ 0; // set validGrade to false
else if(grade -$ ./.0)
letter#rade $ 010;
else if(grade -$ 2/.0)
letter#rade $ 030;
else if(grade -$ 4/.0)
letter#rade $ 0C0;
else if(grade -$ 5/.0)
letter#rade $ 060;
else
letter#rade $ 070;
if("alid#rade)
printf(&89e letter grade is (c&) letter#rade);
C
else
printf(&'rror: In"alid grade&);
"ulti-way selection without an else o#tion if - else if - else if statement!
=sed to e(ecute the first statement or compound_statement whose corres#ondin* condition is
true. %o if-branch is e(ecuted if each condition is false.
Sin*le statements +om#ound statements
if(condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement3;
.
.
.
else if(condition)
statement;
else if(condition!)
statement!;

if(condition1)
compound_statement1
else if(condition2)
compound_statement2
else if(condition3)
compound_statement3
.
.
.
else if(condition)
compound_statement
else if(condition!)
compound_Statement!
%ote: There may be one or more else if
branches
%ote:
There may be one or more else if
branches.
-t is a synta( error to #ut a semicolon
after the closin* brace of a com#ound
statement in an if branch e(ce#t the last
branch!.
4
,(am#le:
if(octane!um%er + :1)
printf(&6o not use t9is gasoline&);
else if(octane!um%er + :/)
printf(&;ou ma< use t9is gasoline&);

%ested if statements
The com#ound statement in an if-branch or an else-branch of an if-statement may contain one or
more of any ty#e of if-statement discussed above.
,(am#le:
if(grade + 0.0 ,, grade - 100.0)
printf(&'rror: In"alid grade&);
else=
if(grade -$ ./.0)
letter#rade $ 010;
else if(grade -$ 2/.0)
letter#rade $ 030;
else if(grade -$ 4/.0)
letter#rade $ 0C0;
else if(grade -$ 5/.0)
letter#rade $ 060;
else
D
letter#rade $ 070;
printf(&89e letter grade is (c&) letter#rade);
>
%ested if structures can be com#licated:

ifcondition1!8
statement1)
ifcondition&!
statement&)
else
statementC)
statement4)
;
else8
ifconditionC!
com#ound>statement'
else ifcondition4!
com#ound>statement9
else
com#ound>Statement+
com#ound>StatementE
;
F
Note: -n a nested if statement4 the last else is associated with the closest un#aired if4 unless braces
are used to alter the default #airin*:
,(am#le:
if(9ours + :)
if(distance - /00)
printf(&8<pe 01&);
else
printf(&8<pe 02&);
is eBuivalent to:
if(9ours + :)=
if(distance - /00)
printf(&8<pe 01&);
else
printf(&8<pe 02&);
>
Switch statement
The if- else if - else statement is used in #ro*rammin* situations where one set of statements must
be selected from many #ossible alternatives. The switch statement #rovides an alternative to this
statement for cases that com#are the value of an int or char e(#ression to a s#ecific int or char
constant.
The *eneral form of the switch statement is:
switch(int or char expression){
case constant1: statementList1;
break;
case constant: statementList;
break;
!
!
!
case constant": statementList";
break;
default: statementList#;
$
%ote:
The case constants must be distinct int or char constants) otherwise there is a synta( error.
' statementList may contain .ero or more semi-colon se#arated statements. -t is not
necessary for statementList to be a com#ound-statement.
The default label to*ether with its statementList may be missin*.
The break statement followin* a statementList may be missin*.
The int or char expression is evaluated and then the statementList of the case value that
eBuals to the expression is e(ecuted. -f there is a break statement4 control #asses to the
statement after the switch) otherwise4 the followin* statementLists are e(ecuted until a break
statement is encountered4 control then #asses to the statement after the switch statement.
-f e(#ression is not eBual to any case value4 the statementList for the default label is
e(ecuted4 control then #asses to the statement after the switch statement. -f expression is not
eBual to any case value and there is no default label4 control #asses to the statement after the
switch without e(ecutin* any switch statementList.
G
switch example01: dis#lay the name of a di*it:
int digit;
printf(&'nter an integer digit:?n&);
scanf(&(d&) *digit);
s@itc9(digit)=
case 0: printf(&Aero&);
%reaB;
case 1: printf(&one&);
%reaB;
case 2: printf(&t@o&);
%reaB;
case 3: printf(&t9ree&);
%reaB;
case 5: printf(&four&);
%reaB;
case /: printf(&fi"e&);
%reaB;
case 4: printf(&siC&);
%reaB;
case 2: printf(&se"en&);
%reaB;
case .: printf(&eig9t&);
%reaB;
case :: printf(&nine&);
%reaB;
default: printf(&'rror: In"alid digit&);
>
switch example02: classify a character
c9ar c1;
printf(&'nter an 'nglis9 alp9a%et?n&);
scanf(&(c&) *c1);
if(c1 -$ 0a0 ** c1 +$ 0A0 ,, c1 -$ 010 ** c1 +$ 0D0)=
s@itc9(c1)=
case 0a0:
case 010:
case 0e0:
case 0'0:
case 0i0:
case 0I0:
case 0o0:
case 0E0:
case 0u0:
case 0F0: printf(&;ou entered a "o@el?n&);
%reaB;
default: printf(&;ou entered a consonant?n&);
>
>else
printf(&;ou entered a nonG'nglis9 alp9a%et?n&);
H
switch example03: Ierform arithmetic o#eration on two inte*ers
int num1) num2) result;
c9ar operator;
printf(&'nter t@o num%ers and t9e operator?n&);
scanf(&(d (d (c&) *num1) *num2) *operator);
s@itc9(operator)=
case 0H0: result $ num1 H num2;
printf(&(d H (d $ (d?n&) num1) num2) result);
%reaB;
case 0G0: result $ num1 G num2;
printf(&(d G (d $ (d?n&) num1) num2) result);
%reaB;
case 0I0: result $ num1 I num2;
printf(&(d I (d $ (d?n&) num1) num2) result);
%reaB;
case 0J0: if(num2 K$ 0)=
result $ num1 J num2;
printf(&(d J (d $ (d?n&) num1) num2) result);
>
else
printf(&'rrorK num2 is Aero?n&);
%reaB;
case 0(0: if(num2 K$ 0)=
result $ num1 ( num2;
printf(&(d (( (d $ (d?n&) num1) num2) result);
>
else
printf(&'rrorK num2 is Aero?n&);
%reaB;
default: printf(&'rror: Lrong operator&);
>
J
Task#1: Kewrite the + #ro*ram below usin* one-way selection if statements instead of
if-else if -else and if-else. %ote: The #ro*ram you write will be less efficient than the one below.
LM This #ro*ram #rom#ts the user for the a*e and determines
if he is child4 teen4 adult4 retired senior4 or worNin* senior
ML
Minclude +stdio.9-
Minclude +stdli%.9-
int main("oid)=
int age;
c9ar status;
printf(N'nter t9e age: N);
scanf(N(dO) *age);
if(age - /:)=
fflus9(stdin);
printf(N'nter @orB status:O);
scanf(N(cO) *status);
if(status $$ PLQ ,, status $$ P@Q)
printf(NLorBing seniorO);
else
printf(NRetired seniorO);
>
else if(age - 20)
printf(N1dultO);
else if(age - 12)
printf(N8eenO);
else
printf(NC9ildO);
s<stem(&P1FS'&);
return 0;
>
Task# 2: +onvert the #ro*ram-fra*ment below to a com#lete + #ro*ram that uses if-else if -else
statement instead of the switch statement:
printf(N'nter t9e student letter grade (1) 3) C) 6) or 7) : N);
scanf(N(cO) *grade);
s@itc9 (grade) =
case P1Q:
case PaQ:
case P3Q:
case P%Q: printf(N#ood standingO);
%reaB;
case PCQ:
case PcQ printf(NE.S.O);
%reaB;
case P6Q:
case PdQ:
case P7Q:
case PfQ: printf(NPoor) student is on pro%ationO);
%reaB;
default:printf(NIn"alid letter gradeO);
>
1A
Task# 3: <rite a #ro*ram that #rom#ts and reads two inte*er numbers. -t then checNs the numbers
and #rints one of the followin* messa*es accordin*ly:
Oou have entered two even numbers.
Oou have entered two odd numbers.
Oou have entered one even number and one odd number.
Pint: =se the modulus o#erator ! for checNin* the numbers.
Task#!: <rite a + #ro*ram that dis#lays the followin* menu usin" a function of t#pe void:
1. /ind area and #erimeter of a sBuare
&. /ind area and #erimeter of a rectan*le
C. /ind area and #erimeter of a circle
4. ,(it the #ro*ram
The #ro*ram then reads the menu choice and behaves as in the followin* table:
"enu choice Iro*ram behavior
-n#ut other than 14 &4 C4 or 4 The #ro*ram dis#lays the followin* error messa*e and
then terminates:
$rror: %ron" menu choice
4 The #ro*ram dis#lays the followin* messa*e and then
terminates:
No shape selected
1 The #ro*ram #rom#ts for and reads the len*th of a sBuare.
-t then com#utes and dis#lays the area and the #erimeter of
the sBuare.
& The #ro*ram #rom#ts for and reads the len*th and the
width of a rectan*le. -t then com#utes and dis#lays the
area and the #erimeter of the rectan*le.
C The #ro*ram #rom#ts for and reads the radius of a circle.
-t then com#utes and dis#lays the area and the #erimeter of
the circle. Oour #ro*ram must use a named constant &'
that has a value of 3(1!2)
%ote: 'ssume that the values read by the #ro*ram for o#tions 14 &4 and C are in centimeters. /or
these o#tions4 your #ro*ram must dis#lay a##ro#riate units in the out#ut.
Task#*: =se a switch statement to write a com#lete + #ro*ram that #rom#ts and reads an inte*er
re#resentin* the number of days in a month &H4 &J4 CA4 or C1!. The #ro*ram then #rints the name of
the month or months with that number of days. Oour #ro*ram must handle the case of wron* in#ut
within the switch statement.
Oou may find the followin* #oem useful: "Thirty days have September, April, June, and
ovember! All the rest have thirty one, e"cept #ebruary alone, that has twenty ei$ht clear% but
twenty nine in each leap year"
11

Вам также может понравиться