The correct structure syntax for Pascal, if you then start the end; (in Inno Setup)

About 20 years have passed since I had to write in Pascal. It seems that I cannot correctly use the elements of the structure of the language where I am nested if the blocks use the beginning and the end. For example, this gives me a compiler error "Identifier expected"

procedure InitializeWizard; begin Log('Initialize Wizard'); if IsAdminLoggedOn then begin SetupUserGroup(); SomeOtherProcedure(); else begin (*Identifier Expected*) Log('User is not an administrator.'); msgbox('The current user is not administrator.', mbInformation, MB_OK); end end; end; 

Of course, if I delete the if then block and the associated begin end blocks, then everything will be OK.

Sometimes I get this kind of syntax correctly, and it works fine, but problems get annoying when nesting if then else blocks.

Solving the problem is not enough. I want to better understand how to use these blocks. I clearly have no concept. Something from C ++ or C # will probably creep out from another part of my mind and ruin my understanding. I read several articles about this, and I think I understand this, and then I do not.

+7
delphi pascal inno-setup pascalscript
source share
1 answer

You need to map each begin to end at the same level, e.g.

 if Condition then begin DoSomething; end else begin DoADifferentThing; end; 

You can reduce the number of lines used without affecting the placement if you want. (It might be easier if you get used to the syntax for the first time.)

 if Condition then begin DoSomething end else begin DoADifferentThing; end; 

If you execute a single statement, begin..end are optional. Note that the first condition does not contain a final one ; , since you have not finished the statement:

 if Condition then DoSomething else DoADifferentThing; 

The semicolon is optional for the last statement in the block (although I usually include it even when it is optional, to avoid future problems when adding a line and forget to update the previous line at the same time).

 if Condition then begin DoSomething; // Semicolon required here DoSomethingElse; // Semicolon optional here end; // Semicolon required here unless the // next line is another 'end'. 

You can combine one and several blocks of statements:

 if Condition then begin DoSomething; DoSomethingElse; end else DoADifferentThing; if Condition then DoSomething else begin DoADifferentThing; DoAnotherDifferentThing; end; 

Proper use of your code:

 procedure InitializeWizard; begin Log('Initialize Wizard'); if IsAdminLoggedOn then begin SetupUserGroup(); SomeOtherProcedure(); end else begin Log('User is not an administrator.'); msgbox('The current user is not administrator.', mbInformation, MB_OK); end; end; 
+25
source share

All Articles