Python to Delphi Blog 2

Blog 2: Conditional Statements, the If’s of Coding!

Hello there, dear reader, and welcome to our blog series covering how to switch from Python to Object Pascal!

Welcome back!

We are hoping that you had the chance to read our previous blog post in this series. If not, please make sure to check it out, as this blog is the continuation of the previous one! Today, we will build up on some important lessons we obtained in the first block, and dive deeper into the differences in notation between Python and Object-Oriented Pascal in the field of conditional statements!

The structure of the conditional If statement in Object Pascal

As we all know, the structure of the conditional If statement and else if in Python is: 

if condition:
dosomething
elif condition:
doalternative
else:
dodefault

We already know how conditionals work. The condition (the certain parameter we set) is verified with the answer in form of either of the booleans (True or False). If the result is True, the code will execute whatever actions we programmed for that specific condition. Otherwise, it will skip that part of the code, and go on executing other blocks of code. 

Now let’s take a look at how the syntax for If statements is in Pascal. It is a bit different, yet more intuitive than Python, even for the complete beginners:

if condition then
dosomething
else if condition then
doalternative
else:
dodefault;

As seen previously, it is necessary to use the semicolon at the end of the directive line in the “else” section. Even though, Pascal is not sensitive to indentation, meaning, both versions will be correct:

Version 1

var 
num : Integer;
begin
num := 3;
if num > 3 then
Write(‘Hello there!’);
end.

Version 2

var
num : Integer;
begin
num:=3; if num >3 then Write(‘Hello there!’);
end.

You have to use the semicolon at the end of the if-conditional. The indentation is used appropriately by most of the developers to make the code more readable, and open for debugging later on. Lastly, we need to explicitly tell the program to do a certain action if the condition is True. For that purpose, we use the keyword ‘then’.  Note, that if you have more than one directive within the if condition (or else), you have to wrap those directives in an additional pair of begin-end. Keep in mind that the “end” used for the “if” part of the statement and before “else” does not have a semicolon. 

Case Iteration

In addition to If statements, Pascal suits us up with the feature reminding of the ‘switch’ feature in Javascript. Let’s see how it is implemented, before breaking down what means what (note that ‘//’ is used to provide comments to the code, just like in Python):

var 
num : Integer;
begin
num := x; // any number 
Case  num of 
1..3 : dosomething; // the ‘..’ is the Pascal’s alternative to ‘range’ of Python, so ‘1..3’ means 1, 2, 3
5..10: doalternative;
else
dodefault;
end.

As you can see when using the ‘Case’ conditional statement, we can select a certain variable, and then iterate through certain cases (values that the variable can have). Based on that, we can perform certain actions. This tool is very useful especially in Delphi development. Some examples of uses are: creating any type of form that prompts user’s input, but at the moment of coding, not being certain which exact value the input will have.

More Key Points to Consider in Relation to Object Pascal

Prior to concluding today’s blog, it is necessary to highlight a few more key points. For example, if you need to use a complex condition in a form of “condition1 AND condition2” or “condition1 OR condition2”, or perform more than one action based on the condition, then there are few more syntax elements to consider in Pascal, such as:

if (a < 10) AND (b>5) then // It is needed to separate out each condition via “()” due to the hierarchy of prioritisation. As AND or OR can either be boolean operators and bitwise, and bitwise has a higher priority, so if no brackets will be used, then the compiler will try to evaluate: “a < (10 AND b) > 5 which will lead to error
begin 
ShowMessage(‘Case1’); // In contrast to single directive conditionals, here, we add a local pair of begin-end, and each directive receives its semicolon at the end
c := a + b;
end // However, the idea of no semicolon before else is still in place, as the “end” before “else” does not have a semicolon after it.
else
begin
ShowMessage(‘Case2’);
d := a-b
end; //Once again we create a local pair of begin-end, but in this case, as it is the last “end” and not before the “else” or “else if”, we have to use the semicolon to close out the conditional statement. 

Conclusion

Today, we have covered the basics of the conditional statements of Pascal. In the next blog we will jump into iteration programming differences between Python and Pascal, and things will finally get interesting, make sure to follow us on LinkedIn to not miss out! In the meantime, you can check out our other blog posts, they are sure to prove to be interesting for you!

We are using cookies on our website

We use cookies to ensure that we give you the best experience on our website. We use cookies to personalize content and to analyze our traffic. For more information on how we use cookies, please visit our Privacy Statement.
By using our website, you acknowledge this notice of our cookie practices.