Home computickets When trying to display a variable, “Missing operator or semicolon” ​​is issued

When trying to display a variable, “Missing operator or semicolon” ​​is issued

Author

Date

Category

I wanted to write a simple arithmetic calculation in delphi.
But it displays an error:

[Error] Project1.dpr (10): Missing operator or semicolon

Here is my code:

program Project1;
uses
 SysUtils;
var x, y: real;
begin
write ('x ='); readln (x);
if (x & gt; 1) then y: = cos (x) / (x * x * x + 3 * sin (x) +8)
else y: = x * x * x + 3 * sin (x) +8
writeln ('y =', y: 0: 3);
Readln;
end.

Answer 1, authority 100%

Missing ; after the else block. To avoid confusion, remember – always surround blocks of code with statement brackets begin and end , even if it is a block from one call:

program Project1;
uses
 SysUtils;
var x, y: real;
begin
  write ('x ='); readln (x);
  if (x & gt; 1) then
  begin
    y: = cos (x) / (x * x * x + 3 * sin (x) +8);
  end
  else
  begin
    y: = x * x * x + 3 * sin (x) +8;
  end;
 writeln ('y =', y: 0: 3);
  Readln;
end.

It’s at ideone


Answer 2, authority 100%

This will be correct both from a logical point of view and from a formatting point of view:

program Project1;
{$ APPTYPE CONSOLE} // Instructions for Delphi that this is a console application
uses
 SysUtils;
var
 x, y: real;
begin
 // We are waiting for the input of the X value from the user
 write ('x =');
 readln (x);
 // Program logic
 if (x & gt; 1) then
  y: = cos (x) / (x * x * x + 3 * sin (x) + 8)
 else
  y: = x * x * x + 3 * sin (x) + 8; // & lt; - The error was here, skipped;
 // Show the result
writeln ('y =', y: 0: 3);
 readln;
end.

Notice how correct formatting allows you to see the entire program at a glance and spot errors in it.

P.S. Just in case, trigonometric functions like cos expect an angle in radians as input, so if the user enters a number in degrees, you will need to convert it to radians.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions