357mag 2 Posted May 6, 2023 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
dummzeuch 1505 Posted May 6, 2023 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
Patrick PREMARTIN 69 Posted May 6, 2023 If you are not sure of what you do with begin/end blocks, try the code formater (Ctrl+D or contextual menu "format code"). Share this post Link to post
Remy Lebeau 1393 Posted May 6, 2023 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
357mag 2 Posted May 6, 2023 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
357mag 2 Posted May 6, 2023 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
Remy Lebeau 1393 Posted May 6, 2023 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