One of the most basic concepts in any programming language is called branching. In other words, make a decision based on some data; do this or do that (or do nothing). The most common implementation of branching is the if statement.
Simple IF Decisions
The simplest form of the if statement is a two lines. The English version looks like this:
First, note that comparing two values to see if they are equal uses the double equal sign ("=="). Attempting to use a single equal sign ("=") will result in an error.
The coded version of the if statement looks like this:
An if statement includes a comparison between at least two values, a comparison operator such as equals (=), not equals (!=), greater than (>), less than (<), etc., and ends with a colon (:). ANY statement that ends in a colon must have another statement below it that is indented. The absence of a second statement is an error. Incorrect indenting is also an error. More on indentation in Python below.
The comparison part of the if statement is called the "condition" or conditional clause. It must evaluate to be true or false. In the code below, for example, the condition compares the value of the variable count with the literal 5. They are not equal; the result of the conditional comparison is not true, or false. The print statement will not be executed.
Indentation in Python
Python uses indentation to identity blocks of code that belong together and statements that are contained within or under other statements. Grasping this concept is critical to understanding how Python interprets the flow of statements. In a simple Python program, the first set of statements are not indented; they are aligned against the left edge of the editor.
Any statements ending in a colon, such as an if statement, must have an indented statement below it and "under it", meaning part of and related to the if statement. All the statements under the if statement must have the same amount of indentation (and the same type of indentation). If the first statement under if is indented 4 spaces, any other statements meant to be part of the if block must also be indented using 4 spaces. Some statements cannot use 4 spaces and some use 2 spaces or 5 spaces. They must be the same. If some statements use a tab for spacing, they must all use a tab for spacing.
The end of a statement block is not obvious. It ends when another statement exists at the same indentation level of the statement that started a block. It can be any statement, such as another if statement, a print() statement, or just a variable declaration. Look at the example below:
There are 3 initials statements with no indentation. The fourth statement (line 6), is an if statement that starts a new block. There will always be a block of at least two statements because a statement ending in a colon (:) requires at least one statement under it. In this example, the block is made up of four statements, including the if statement and the three statement under it (all with the same indentation). The block ends with the if statement at line 11 because that statement has no indentation (it is at the same indentation as the statement on line 6.
This same scenario plays out with the second if block that covers lines 11 through 14. The block ends with the print() statement on line 16.
Notice that each indented statement is indented in the same way (using a single tab in this case). If any of the indented statements used spaces (even 4 spaces), the program would not run due to invalid indentation (a tab on some statements and spaces on other statements). If the print() statement had a space in front of it, the program would fail for the same reason.
But understanding how indentation is more than just avoiding errors. Consider this very similar example. Can you detect why the final print() statement was not executed?
Look at the indentation. the final print() in this example is indented like the block above. As such, it is part of the block above!. The blank line is ignored. Until another statement is left-aligned in this case, with no indentation, the if statement does not end. Because the condition of the second if statement is false, the print statement does not get executed.
Nested IF statements
Nesting refers to a block of code under another block of code. Consider the following example:
When a statement with ending in a colon is under a previous statement with a colon, the second conditional statement (and its block) is said to be nested below the first conditional statement. Line 13 above starts a new block (Block 3, 1 indent). Because it is already part of a conditional block, it is a nested block.
Conditional statements end when a following statement exists at the same indentation level or a lower indentation level. In this example, Block 2 starts at line 11 (0 indent) and ends before line 17 (because line 17 is at the same indentation level - 0 indents). Block 3 starts at line 13 (1 indent) and also ends before line 17 (because line 17 is at a lower indentation level (0 indents)
There can be a large number of nesting blocks (theoretically over 100), although a program with such code probably needs to be rewritten. If is common, however, for beginner programs to have 3 or more levels of nesting (branching). As program requirements get more complex, the level decision making often increases,, and it is important to be able to follow and direct the flow of the program to achieve its objectives.