Chapter 6 Conditional Statements
6.1 if, else, elif
Keyword | Action |
---|---|
if, else, elif | conditional statement |
You remember that a Python program runs by executing the code in each sentence, starting from the top. Sometimes that standard flow is not desirable. For example, you may want to execute a block of lines, only if certain condition is satisfied. The keywords ‘if,’ ‘else,’ ‘elif’ help in building such conditional code.
Here is an example. You can interchange the values i and j to see how the flow changes.
Try -
condition=True
if condition:
print("condition is", condition, ". Line 1 is running")
else:
print("condition is", condition, ". Line 2 is running")
Try -
i=3
j=200
if (i==2):
print("number is 2")
elif (i==1):
print("number is 1")
else:
print(i,j)
6.2 Loop (‘while’)
Try -
i=0
while i<10:
i=i+1
print("5 times", i, "is", 5*i)
The keyword ‘while’ is used to loop over the same code many times. However, how many times the loop will run is not pre-determined. Instead, the ‘while’ statement checks for a condition or logical variable. The loop continues to run as long as the condition remains True.
6.3 Keywords ‘break’ and ‘continue’
Sometimes it is necessary to break out of ‘for’ loop, if certain conditions are satisfied. The keywords ‘break’ and ‘continue’ help in interrupting the standard flow. They are explained in the following section with respect to ‘while’ loops, but work for ‘for’ in a similar manner.
‘While’ loops become even more powerful, when they are customized using an internal condition (‘if’). The keywords ‘break’ and ‘continue’ come handy in that situation.
Code -
i=0
while i<10:
i=i+1
if(i==4):
break
print("5 times", i, "is", 5*i)
The keyword ‘break’ halts further execution of the ‘while’ block, and moves to the subsequent unidented lines in the code.
Try -
i=0
while True:
i=i+1
if i==4:
break
print("5 times", i, "is", 5*i)
In the above code, the condition for ‘while’ is always True. Therefore, it is expected to run infinite times. That does not happen, because the loop is terminated using ‘break,’ when i reaches 4.
Try -
i=0
while i<10:
i=i+1
if i==4:
continue
print( "5 times", i, "is", 5*i)
The keyword ‘continue’ skips over the remaining lines of the ‘while’ block and starts the following run of the ‘while’ loop.
6.4 Summary
Python keyword ‘for’ executes the same block of code many times. The block of code needs to be written immediately after the ‘for’ statement with indentation. After completion of the ‘for’ block, Python continues with subsequent non-indented code.
The ‘for’ statement can state a changing variable given based on a list, dictionary or string. The block of code executing in each ‘for’ run uses the new value of the variable. Later we will learn that the ‘for’ statement can be extended for other kinds of custom variables known as ‘iterables.’
One weakness of ‘for’ is that the values of the looping variable need to be know a-priori. There are situations, where the next value of the looping variable is determined based on the execution of the loop. Such codes are written using the Python keyword ‘while,’ as explained in the following section.
Python keyword ‘while’ combines the power of looping with the ability to check a condition. Therefore, it is used to create loops, where the number of times the loop is run is not easy to pre-determine.
Overall, the keywords ‘if,’ ‘else,’ ‘elif,’ ‘for,’ ‘while,’ ‘break,’ ‘continue’ covered over the last three chapters give a programmer immense flexibility to write wide variety of programs. However, often it is necessary to read the input from the keyboard or a file and save the output in a file as well. We explore that in the followng section.