3

Im trying to use a Else statement on my code, but i cant figure out the correct sintaxe. Wheres the error on that? If i comment the 'Else' line code compiles just fine.

Procedure im_dumb;
begin
If (1 > 2) Then
begin
AddToDebugJournal('if');
end;

else

begin
AddToDebugJournal('else');
end;
end.


Program New;
begin
im_dumb; 
end.
niceguy335
  • 361
  • 1
  • 3
  • 7
  • 1
    No semi-colon before `else`. – Michel Billaud Jun 09 '16 at 15:17
  • There's a [tutorial here](http://stackoverflow.com/q/28221394/62576) on using `begin..end` (which includes `else`) that should help. Your code is so badly broken I'd suggest you go read that tutorial carefully. – Ken White Jun 09 '16 at 15:18
  • 1
    If the example posted isn't your original code, it doesn't belong here. Don't ask us to debug problems with your code unless you're posting **your code**. Making up BS code for your question can change or hide the actual problem (which is what the trash you posted here does), and it's a waste of both our time and yours. Questions about your code need **YOUR CODE**. Please bear that in mind in the future; if you don't, you may end up having a very short future here. – Ken White Jun 09 '16 at 15:44
  • 1
    Your original code off-site is irrelevant. What is meaningful is the code posted here. You've said *I've posted code that isn't mine, so the problem really isn't demonstrated here, but please waste your time locating a problem in this nonsense garbage I'm posting anyway.* That's not acceptable here. A valid close reason here is asking us to debug a problem in your code and failing to post the *actual, relevant portions of your code*. Again, if you want us to help you with a problem with your code, it's absolutely required that you post **your code**. – Ken White Jun 09 '16 at 16:07
  • @KenWhite thanks, i will do that next time. – niceguy335 Jun 09 '16 at 17:00

2 Answers2

2

Don't add a semicolon before the "else". This is Pascal's way of mitigating dangling else.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
2

In pascal's logic, the semi-colon separates "statements", whereas it terminates them in C.

The syntax for the if-then-else construct is

  if expression then instruction [ else instruction ]

so no semi-colon should ever appear before an else.

Refer to your favorite syntax railroad diagram http://pascal-central.com/images/pascalposter.jpg

Michel Billaud
  • 1,758
  • 11
  • 14
  • Thank you, after i removed the semi-collon from the previous 'end' my code just works :) – niceguy335 Jun 09 '16 at 15:40
  • The separator argument doesn't really explain why before else is the only place where it has special meaning. As opposed to e.g. the last statement before an end. – Marco van de Voort Jun 09 '16 at 18:51
  • Well, the semi-colon separates the "if condition then instruction1" part, which is a perfectly valid instruction, from the following (expected) instruction. And "else instruction2" is not a valid instruction. – Michel Billaud Jun 10 '16 at 07:17