[…] I would like to know how to shorten it maybe with "case" […]
Pascal – as defined by ISO standards 7185 “Standard Pascal” and 10206 “Extended Pascal” – has the data type integer
.
An integer
value is an integral value in the range −maxInt‥+maxInt
.
Thus
case myIntegerExpression of
−maxInt‥+maxInt:
writeLn('Ya1k');
end;
will always print Ya1k
regardless of the specific value of myIntegerExpression
(presuming it is defined).
In your case you could write multiple non-overlapping case
-labels, −maxInt‥0
to match non-positive integer
values and 1‥maxInt
.
However, this will not necessarily reduce your code’s length, yet it may become more readable.
Remember, you write code for humans.
Humans should be able to read and understand your code.
It is a minor, (almost) negligible issue that a machine, a computer, needs to be able to process your code, too.
[…] I tested it and already works, but I feel it is too repetitive. […]
Generally speaking, you use routines for repetitive tasks.
In your particular case it is remarkable that you do not use write
(without the Ln
in the end).
You could print a common prefix with write
and write the final word(s) with writeLn
.
writeln('Ingresa la coordenada en X.');
readln(x);
writeln('Ingresa la coordenada en Z.');
readln(z);
read
/readLn
accept multiple variables.
You could merge both prompts into one prompt.
It is advisable that your prompt message tells the user all details.
In particular your prompt should emphasize that only integer
values (no real
values) are valid since discrete coordinate systems are somewhat less common (= less expected).
Again, write your programs for humans.
In Pascal instead of writing
if (x=0) and (z=0) then
it is (incidentally more concise and) more idiomatic to write
if [x, z] = [0] then
At least mathematicians use sets a lot.
(Keep in mind that Pascal, however, supports only sets with an ordinal base type.)