-4

below is my code and error message I had been thinking of how to correct it but still couldn't, can anyone pls help me, thx I was trying to make a programme that can make a square with numbers out side like this

input:3
output: 1 2 3
        8   4
        7 6 5

input:4
output:1 2 3 4
       12    5
       11    6
       10 9 8 7

input:1
output:1

var i,n,e1,e2,n2:longint;
begin
readln(n);
n2:=n;
   if n2<>1 then;
      begin
         for i:=1 to n do
            begin
               write(i:4);
            end;
            e1:=(n*n-(n-2)*(n-2));
            e2:=n+1;
            if n>2 then;
               begin
                  for i:=1 to n-2 do
                     begin
                        writeln();
                        write(e1:4,e2:(n-1)*4);
                        e1:=e1-1;
                        e2:=e2+1;
                     end;
               end;
            writeln();
            begin;
               for i:=1 to n2 do
                  begin;
                     if n2>2 then;
                        begin;
                           write(e1:4);
                           e1:=e1-1;
                     end
                  end
            end
      end
   else if n2=1 then;
      write('1');
end.




Free Pascal Compiler version 2.6.2-8 [2014/01/22] for x86_64
Copyright (c) 1993-2012 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling main.pas
main.pas(35,4) Fatal: Syntax error, ";" expected but "ELSE" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be `compiled)`
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Casper Wong
  • 11
  • 1
  • 4

3 Answers3

1

You use ; in a wrong way. It is used to separate statments composed by begin ... end. For example you write begin s1;s2;s3 end. Think of begin and end like of brackets which make one statement by composing many statements. If, for, while and repeat can take only one statement, so if you need to put more statements you enclose them into begin end and separate them by ;

For example: if x<0 then x := -x is one statement. If you need more than one statement in then part of the if, you would write: if x<0 then begin x := -x; y := 3 end. This is again one statement (if) which have one statement after then (which consists of two statements).

It is handy to have pascal syntax diagrams from which you can deduce things like this ...

I have indented a little bit your program so that you can more easily see what you have written, and reversed first if then else so that it is easier to follow.

All begin ends containing just one statements can be omited and you can see that you are testing same condition twice in first if:

program box;
var
  i,n, e1,e2,n2 : longint;

begin
  readln (n);

  n2 := n;
  if n2=1
  then if n2=1
       then write('1')
       else
  else begin
         for i:=1 to n do
          begin
            write (i:4);
          end;

          e1 := n*n - (n-2)*(n-2);
          e2 :=n+1;
          if n>2
          then begin
                 for i:=1 to n-2 do
                 begin
                    writeln;
                    write (e1:4, e2:(n-1)*4);
                    e1 := e1-1;
                    e2 := e2+1
                 end
               end;

          writeln;

          begin
            for i:=1 to n2 do
            begin
              if n2>2
              then begin
                     write (e1:4);
                     e1 := e1-1
                  end
            end
         end
      end
 end.

You are on right track, but it does not work for n=2.

Also, you might notice empty else in first if. This is because if a then if b then c else d puts d as else in second if. So if you want to tie that else to first if then you would do if a then if b then c else else d.

1

You can look at this program which performs what you wanted to do, and learn from it how to use pascal to hierarhicaly break down problem in smaller problems and then sort out each one individually:

program box;
var n,len, t,i : integer;

  procedure firstrow;
  var i : integer;
  begin
    for i := 1 to n do write (i:len);
    writeln
  end;

  procedure midrow (r:integer);
  var i : integer;
  begin
    write (4*(n-1)+2-r:len);
    for i := 1 to n-2 do write (' ':len);
    writeln (n+r-1:len)
  end;

  procedure lastrow;
  var x, i : integer;
  begin
    x := 3 * (n-1) + 1;
    for i := x downto x-n+1 do write (i:len);
    writeln
  end;

begin
  write ('input: '); readln (n);

  { calculate number of digits in bigest nuber printed }
  t   := (n-1)*4;
  len := 1;
  while t>9 do
  begin
    len := len+1;
    t   := t div 10
  end;
  len := len+1; { plus one for space }

  writeln ('output:');
  for i := 1 to n do
    if i = 1
    then firstrow
    else if i = n
         then lastrow
         else midrow (i)

end.

You can also see how you can use local variables (i as global variable and i as local variable in procedures). So when you write your procedures you can use any name for local variable as long as it don't shadow global variable which you want to access inside the procedure (n from global scope). It is also good practice to pass global variables as parameters to procedures because then you can use any name you want for local variables. If you write them like that, you can just copy and paste your previously written procedures and functions to any program where you need them ...

0

The semicolon after the then is incorrect. The correct syntax is

if condition then statement;
if condition then statement else statement

for two if statements. In my example one is an if, the other is an if-else. Note the semicolon separates the two statements. A begin end block may be used to group statements if there is more than one statement in a branch. Separate statements with semicolons.

kd4ttc
  • 1,075
  • 1
  • 10
  • 28