IF Statements in Python

If statements can have three components. First is

if condition

statement

statement

end

The parser gets the if symbol, skips it, and expects a <condition> to follow. After that comes a sequence of statements, and if at any time an end is seen, then the statement is complete.

However, if the parser sees an elseif symbol before it sees the end, it begins to parse the elseif section of the if statement. Elseif is basically the same as the if statement:

elseif condition

statement

statement

end

There could be multiple elseif parts. Again, if the parser sees an else symbol before it sees the end, then it starts parsing the else:

else

statement

statement

end

This is just the else symbol followed by some more statements. The end must terminate the if statement after an else.

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *