index<--

## Control Flow

Control Flow statements are part of structured programming. They are introduced to reduce the impact of GoTo statement we use to have in Basic language. The secret of control statement are the Logical expressions or conditional expressions. These can be boolean expressions or relational expressions.

## Decisions statement

The most common and maybe the most important control flow statement in programming is the decision statement. A decision is based on a conditional expression that is also known as logical expression. You must understand propositional logic to be able to make correct conditions. Check next diagram to understand the decision workflow diagram.

Decision Diagram

#### Example:

``````# Demo the "if" statement
x = 0
if x == 0:
// true block
print("x is zero") #as expected
else:
// false block
print("x is not zero") #not expected
pass #end
``````

#### Notes:

• Decision is represented in Python using the "if" keyword,
• After condition we use ":"
• Python is using mandatory indentation of 4 spaces.
• A good practice is to use "pass" statement to mark the end of block.

Output:

``x is zero``
Notes:
• I’m using assignment statement (single equal) "=" to make a new variable x;
• Variable x has initial value x == 0. Logical expression "x" is evaluated to False by "if".

The decision statement can use one or several conditions. For this case we use special keyword "elif" that is short for "else if" but when we use elif, the statement looks better and behave different than a cascading "if else" nested statements that are actually possible but discouraged.

``````# decision ladder
x = int(input("Please enter an integer: "))
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
pass #end if

# end program
``````

#### Notes:

1. This is also called multi-path decision statement because there are several blocks that can be executed in different cases. The default case else: do not have a condition and will execute if no other case condition is satisfied.
2. Python is using mandatory indentation of 4 spaces. To finalize decision blocks you can use "pass" statement. This do not do anything but can be used to decrease indentation, therefore terminate the block.

Homework: Open this program on-line and run it: decision statement

## Repetition

Repetition is a block of code containing several statements that can be executed multiple times. The number of times is determined by a control condition. Sometimes the repetition is forever and this is called infinite loop. There are 2 kind of repetition statements in Python: "while" and "for".

## While statement

The while statement is used for repeated execution as long as a conditional expression is true. The conditional expression can depend of a control variable that is modified inside the repetitive block. The expression is evaluated for each iteration. When the expression result becomes false the block is terminated.

while loop diagram

#### Pattern:

``````
while (logical_expression):
#repetitive block
else:
#non repetitive block
pass
``````

Note: This repeatedly tests the conditional expression and, if the result is true, it executes the repetitive block. When the expression is false the non repetitive block is executed and the loop terminates. The non repetitive block and keyword "else" are optional.

#### Example:

``````
#This example demonstrate an infinite loop
v = 0
while True:
print("forever young")
v+=1
if v==100: break
print("I was young forever")
``````

The previous examples will print 100 times "forever young", then will print "I was young forever" So the break statement has stopped the infinite loop.

## For statement

The for statement in Python differs from what you may be used to in Java or C or Pascal. Rather than always iterating over an arithmetic progression of numbers, Python iterates over the items of any collection of items.

``````
# Measure some strings in a list of strings:
words = ['I', 'like', 'to', 'learn', 'python']
for word in words:
print(len(word), word)
``````

Will print:

``````
1 I
4 like
2 to
5 learn
6 python``````

## Range() function

If you need to iterate over a finite sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions that can be used as a collection of numbers:

``````
for i in range(5):
print(i)
``````

Will print:

``````
0
1
2
3
4``````

## Break and continue

These two keywords are used to alter the default flow of repetitive statements. Forcing a repetition block to break or to continue is done with if statement. Without the "if" it may be a sign of wrong design.

Loop with alterations

``````#Initial Statement
for i in range(1,10):
# first block
if i < 5:
continue
# second block
if i == 7:
break
# third block
pass; # end
#next statement
print("v = ", v)
``````

This example has demonstrate the for loop, the continue and the break statements.

## Else branch

The official declaration of for loop statement have an "else" part like the while loop. This means you can write something like this:

``````# for with else branch
for i in range(10):
print('i=',i)
if i>=3: break
else:
print('finish i=',i)``````

Output:

``````
i=0
i=1
i=2
``````

Note: In this case else: branch is never executed, due to early break.However, in normal case if your statement is not early terminated, the else: branch can be useful. Try to copy the example and comment out the break.