If Statements

If statements can be used if you want to run a code that follows a certain condition, in simpler terms a code will (or will not) run IF it follows certain conditions. When a condition evaluates to true then the code can be interpreted however when the condition no longer evaluates to being true then the code will not be interpreted.

[Console]

>>> if 1 < 2:

print(“1 is less than 2”)

>>> print(“programme ends”)

=====

[Interpreter]

1 is less than 2

programme ends

When using this operation it is key that a colon (:) and indentation of the condition are used. Both the colon and Indentation are mandatory and programmes won’t work without it.

So referring back to the example above, the condition tries to see if 1 is less than 2. With the use of the colon and the indentation, the statement can be used to evaluate the condition. Such that is the condition were to be correct then the statement will be carried forward. But if the condition were to be incorrect then the statement would not be carried forward.

In addition, for clarification, the function of printing “programme ends” illustrates the fact that this statement is not indented therefore does not follow under the if statement and is ultimately interpreted as a regular line of code. An contract example to the example above would follow as:

[Console]

>>> if 1 > 2:

print(“1 is greater than 2”)

>>> print(“programme ends”)

=====

[Interpreter]

programme ends

 

To delve a little deeper into this function, if statements can be nested. Nested statements makes this function more complex as the statements can be put one after each other. This concept is most suitably described via an example:

[Console]

>>>num = 10

>>> if num > 4:

print(“Bigger than 4”)

if num <= 20

print(“Between 4 and 20)

=====

[Interpreter]

Bigger than 4

Between 4 and 20

So to clarify the example above, we originally chose one number (in this case 10) and then this number is then evaluated with the conditions below. So if the number is greater than 4 the statement is printed; which it is. However since the statement below is also indented, the statement is evaluated under the same if statement (nested). This nested condition evaluates if the number is less than or equal to 20; which is it. therefore the double indented statement is evaluated to be true. Therefore both of the statements are printed since they evaluated to be true.

Another example of nested statement:

[Console]

>>> Num = 7

>>> if num > 3:

print(“3”)

if num < 5:

print(“5”)

if num == 7:

print(“7”’)

=====

[Interpreter]

3

*When writing a code like this, it truly demonstrates why careful use of colons and indentation are important.

When investigating this example you might think why the “7” was not printed even though the number we chose was equal to 7. However due to the nested nature of the statements, since the statements regarding the 5 did not evaluate to be true, all of the statements that are found after that statement will not be interpreted. As a result, only the first statement regarding the “3” is printed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s