Jump to content
357mag

Confused on two things

Recommended Posts

I can't post the code at the moment, but I wrote a program that inputs a number and then it determines if the number is odd or even. I used a if-else structure. At the end of the code I put and end(with a semi-colon), and immediately after that I put an end(with a period). The compiler complained. It said "expected a . but got a ; instead.

 

I thought you were supposed to use begin and end(with a semi-colon) kind of like an opening brace and a closing brace in other languages. But I had to completely remove the end(with the semi-colon) to get rid of that error. I don't understand why.

 

And then I had a semi-colon on the statement right before the else. Then I find out by reading that you don't do that either. No semi-colon there.

 

 

Share this post


Link to post

You only need an end for each begin in your code, and a final end at the end of a program or unit.

 

My guess is that your if then else did not use a begin end block.

Share this post


Link to post
12 hours ago, 357mag said:

I can't post the code at the moment

Please do.

12 hours ago, 357mag said:

I wrote a program that inputs a number and then it determines if the number is odd or even. I used a if-else structure. At the end of the code I put and end(with a semi-colon), and immediately after that I put an end(with a period). The compiler complained. It said "expected a . but got a ; instead.

The only way that could happen is if the compiler is interpreting the 'end' as belonging to a 'begin' at the top-level of the unit itself, not in a nested control block, eg:

unit <name>;

function DoIt;
begin // <-- nested
  if <condition> then
  begin // <-- nested
    ...
  end else
  begin // <-- nested
    ...
  end; // <-- ; here
end; // <-- ; here

begin // <-- top-level
  ...
end. // <-- . here

If you believe your 'end' is not the final 'end' of the unit, then you probably missed a 'begin' somewhere above it.  Hard to say without seeing your actual code.

12 hours ago, 357mag said:

I thought you were supposed to use begin and end(with a semi-colon) kind of like an opening brace and a closing brace in other languages.

That is correct in most cases.  The exception being the top-level 'begin..end' of a unit, which uses dot instead of semicolon.

12 hours ago, 357mag said:

But I had to completely remove the end(with the semi-colon) to get rid of that error. I don't understand why.

Neither can we, since we can't see your actual code.

12 hours ago, 357mag said:

And then I had a semi-colon on the statement right before the else.

That is wrong if the statement is not inside a 'begin..end', eg:

if <condition> then
  <statement> // <-- OK
else
...
if <condition> then begin
  <statement>; // <-- OK
end else
if <condition> then
  <statement>; // <-- wrong
else
12 hours ago, 357mag said:

Then I find out by reading that you don't do that either. No semi-colon there.

Programs and Units (Delphi): The_Block

Declarations and Statements (Delphi): Compound Statements

Declarations and Statements (Delphi): Blocks and Scope

 

Share this post


Link to post

This code is the incorrect version. This is the version I first came up with:

 

var
  x: integer;

begin
  Write('Enter an integer: ');
  ReadLn(x);

  if (x mod 2 = 0) then
    WriteLn('The number is even');
  else
    WriteLn('The number is odd');

  WriteLn;
  Write('Press Enter to quit...');
  ReadLn;
end;
end.

 

Share this post


Link to post

This code is the corrected version:

 

var
  x: integer;

begin
  Write('Enter an integer: ');
  ReadLn(x);

  if (x mod 2 = 0) then
    WriteLn('The number is even')
  else
    WriteLn('The number is odd');

  WriteLn;
  Write('Press Enter to quit...');
  ReadLn;
end.

 

Share this post


Link to post
1 hour ago, 357mag said:

This code is the incorrect version. This is the version I first came up with:

Yes, that code is wrong. The end's are unbalanced, and the semicolon on the 1st WriteLn doesn't belong there.

1 hour ago, 357mag said:

This code is the corrected version:

Yes, that code is correct.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×