0

I have this code:

program Coordenadas;
var
    x,z : integer;
begin
  writeln('Ingresa la coordenada en X.');
  readln(x);
  writeln('Ingresa la coordenada en Z.');
  readln(z);
  if (x=0) and (z=0) then 
    writeLn('La coordenada 0 0 es el centro del mundo.')
  else if abs(x)=abs(z) then
    if x>0 then 
      if z>0 then 
        writeln(x,' ', z,' se ubica en el Sureste') 
      else 
        writeln(x,' ', z,' se ubica en el Noreste')
    else if z>0 then 
      writeln(x,' ', z,' se ubica en el Suroeste') 
    else 
      writeln(x,' ', z,' se ubica en el Noroeste')
  else if abs(x)>abs(z) then
    if x>0 then 
      writeln(x,' ', z, ' se ubica en el Este.')
    else 
      writeln(x,' ', z, ' se ubica en el Oeste.')
    else if z>0 then 
      writeln(x,' ', z, ' se ubica en el Sur.')
    else 
      writeln(x,' ', z, ' se ubica en el Norte.')
end.

It seems too repetitive. Can I shorten it, perhaps by using case?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Ya1000
  • 1
  • You can't use a "case of" because that works with integers compared to fixed integer values. You could use some begin/end to simplify this convoluted tests, but that is all you can do, the structure of the tests is quite univoque. – linuxfan says Reinstate Monica Aug 24 '23 at 05:06
  • Sometimes you can linearize all possibilities with e.g. something like sign(x)+factor*sign(z) , but there are multiple checks here using both abs() and not, so that will be difficult. – Marco van de Voort Aug 24 '23 at 11:11
  • Does this answer your question? [How do i use multiple statements to one case statement?](https://stackoverflow.com/questions/27536579/how-do-i-use-multiple-statements-to-one-case-statement) – Karl Knechtel Aug 26 '23 at 01:20

2 Answers2

0

[…] 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.)

Kai Burghardt
  • 1,046
  • 1
  • 8
  • 23
0

Your program has nine distinct outcomes. Without skipping newlines, there's only so concise you can make it. At the risk of actually increasing repetition, you can reduce indentation, which may make it easier to follow the logic of the program.

if A then
  if B then
    V
  else
    W
else if C then
  if D then
    X
  else
    Y
else
  Z

Is equivalent to:

if A and B then
  V
else if A then
  W
else if C and D then
  X
else if C then
  Y
else
  Z
Chris
  • 26,361
  • 5
  • 21
  • 42