Class Crib Notes for CPSC 110 Brent Dingle
Wednesday, February 6, 2002
Reminders:
Lab Assignment 2 is due Feb 6 and 7, Quiz Next Wed.
Feb 13
Review:
I/O
Stuff – EOF and using EOF with a while loop
Short
Circuit Evaluation
Comparison
Operator IN -- works on character
sets and numeric sets ranging from 0 to 255
From
last time we had a program such as:
IF
(ch IN [‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’] THEN
Begin
Writeln(‘ch is a vowel);
End
ELSE ß sneaky introduction of ELSE
Begin
Writeln(ch is a consonant’);
End;
Notice
the ELSE – this is called a multi-way if. Notice there is NO semi-colon before
the ELSE
Another
example of this might be if you have a person guess a number
You
could write something just to tell them if they are right or wrong,
But
it might be more useful to tell them if they were too high, too low or correct
EX:
if (guess > my_number) then
begin
writeln(‘too high’);
end
else if (guess < my_number) then
begin
writeln(‘too low’);
end
else
begin
writeln(‘correct’);
end
Case
Stmts (if you have n values to check for equality):
CASE [variable] OF
[value 1] : [statement 1];
[value 2] : [statement 2];
: :
[value n] : [statement n];
END; { case }
- Consider the if statement:
if (x = 5) OR ( x = 7) OR
(x = 8) then
[statement 1]
else if (x = 34) OR (x = 67) then
[statement 2]
- Contrast with
CASE x OF
5, 7, 8 : [statement 1];
34, 67 : [statement 2];
END; { case }
-
Case ONLY works on EQUALITY checks
- CASE can work on a RANGE of values:
CASE x OF
‘A’ .. ‘Z’ : [action 1];
‘0’ .. ‘9’ : [action 2];
else
[default action];
END;
-
CASE DEFAULT actions using an ending else (Turbo Pascal special)
CASE x OF
‘a’ :
[statement 1]
‘b’ :
[statement 2]
else
[default statement]
end;
We
will talk about nested ifs and whiles and maybe see how to use them together
some more.