F
F
Fox7772021-04-25 15:49:38
Pascal
Fox777, 2021-04-25 15:49:38

How to fix incorrect work with else?

Program qwert;
const n=12;
var
A : array [1..N] of integer;
i : integer;
b : integer;
begin
for i := 1 to N do
begin
A[i] := random (30) - 20;
b := ( A[i]);
if b < 0 then
  begin
  b := 0;
  writeLn ('A[', i, '] = ', b);
  else  b >= 0 then
    writeLn ('A[', i, '] = ', b);
  end;
end.

Complains about not having an operator

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2021-04-25
@Fox7777

syntactically the structure should be

if условие then
begin
 ....
end
else if условие2 then
begin
  ...
end;

you have
else b <= 0 then
is inside the first if block. because the compiler and swears. Having only the code here that is now in question - I can assume that you are trying to write something like this (this is strange from the point of view of logic, because the second block will be executed only if b = 0, but we have what we have)
Program qwert;
const n=12;
var
A : array [1..N] of integer;
i : integer;
b : integer;
begin
for i := 1 to N do
begin
    A[i] := random (30) - 20;
    b := ( A[i]);
    if b < 0 then
    begin
        b := 0;
        writeLn ('A[', i, '] = ', b);
    end
    else if b <= 0 then
        writeLn ('A[', i, '] = ', b);
    end;    
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question