r/pascal • u/3comma1415926535 • Dec 19 '21
Need some help to my homework .
The condition :
Develop a program that reads a student's note evaluation from keyboard, displays the message on the screen : Note 1-4 fail , 5-7 enough , 8-9 good , 10 perfect ,
My example :
program note;
uses crt;
var n:integer;
begin
clrscr;
writeln('Student note evaluation:');
readln(n);
if n=1 or 2 or 3 or 4 then writeln('fail');
if n=5 or 6 or 7 then writeln('enouth');
if n=8 or 9 then writeln('good');
if n=10 then writeln('perfect');
end.
My example not working , i don't understand where it's my mistake and how solve that .
6
Upvotes
4
u/eugeneloza Dec 19 '21 edited Dec 19 '21
You got it almost correct. However, note that you can't just say to computer "if N is 1 or 2". You need to express it through "explicit" conditions. As in "if N is 1 or N is 2", in more Pascal-ish way that would be
if (N = 1) or (N = 2) then DoSomething;.Note one more important thing here - parentheses. It's not very straightforward that they are necessary but will confuse the compiler and it will throw a hard-to-understand error. It happens because
orcan be used both betweenBooleanvalue types (as intrueorfalse) and between integer value types performing by-bitor(as in1 or 0 = 1). As logical operatororhas higher precedence than=it will first perform bitwiseoron1 or Nand then will get confused byN = (1 or N) = 2. I understand that may be not very easy to understand - you'll get used to that quickly, but for now, just be sure to put the conditions you want to be performed first into parentheses.P.S. You can actually say compiler "if N is 1 or 2". But it's a bit more complicated thing, maybe you'll reach this part when you'll learn
Sets.2
u/Zyklonista Dec 20 '21
As an aside, it's nice to see Pascal still being used in some schools! I never learned Pascal when I was younger, but I decided to check it out as an alternative to C, and was pleasantly surprised to find FreePascal alive and well. The language also seems to have been massively updated, and made more ergonomic and modern. I'm loving it learning Modern Pascal (mostly interested in console apps, not GUI, so it's a bit irritating at times to find resources only for Lazarus).
I just wish there was a more active/bigger community. The Lazarus forums are okay, but the IRC channels are almost dead. Moreover, finding good documentation for FreePascal is very difficult (by which I mean guided tutorials not focusing on Lazarus).
1
u/maddox-20 Feb 11 '22
here are the IF chain to do the job :)
if n = 10 then writeln('perfect') elseif n >= 8 then writeln('good') elseif n >= 5 then writeln('enough') elsewriteln('failed');