Python : Programming Language

Control Flow

See, the written software code are expected to execute in a sequence but we don't want all the code to run in a sequence always because there are conditions where one set of instructions or code is expected to run while in other condition we want another piece of code to run. Lets take a real life example. We are writing code for an elevator/lift. Now if the elevator is going upward it should keep checking which all floors it has to stop and which all it has to avoid. So the code will execute till the elevator has completed its all jobs and to do that it will repeat some set of instruction to check whether it has to stop at next floor or move on.

Assume a building of 30 story and all people get onboard in the elevator at ground floor and they pressed their destination floors. Say floor 1, 6, 9, 13 and 24 are selected. Let us assume, for simplicity, that elevator does not take input once it leave ground floor.

//Initial condition
floorInput =  [1, 6, 9, 13, 24]
currentFloor = 0
Repeat:
          if currentFloor is Equal To Any Floor In Floor Input List then make a stoppage at floor
          else if currentFloor is equal To 29 [maximum it could go] then move to ground floor and stop
          else move to nextFloor and make currentFloor = currentFloor + 1

This code above is  just a pseudo-code to understand how a sequence of code could execute. See we have words like Repeat, if and else. Repeat is asking the computer/cpu to execute certain piece of code again and again till it meets certain condition if found and job is supposed to be done. Inside Repeat block there are some conditions based on which either the elevator will stop at any floor or not. In our case to simply the code, the elevator will stop at a floor given as input.

This is how computer programmes needs to behave. So now lets check how swift helps us in achieving this goal. In swift we have followings control flow mechanisms:

Conditional Branching Flow

Repetitional Flow

Execution Break / Termination Flow

IF

if boolean condition :

statements

Example:
a = 80
grade = 'F'
if a > 75 :
grade = 'A'


IF-ELSE

if condition :

statements

else if condition :

statements

else

statements



Example:

a = 80
grade = 'F'
if a > 75 :
grade = 'A'
else if a > 60 :
grade = 'B'
else if a > 40 :
grade = 'C'
else
grade = 'F'


FOR-IN

Unlike C/C++/Java or other programming language, Python does not have For loop block with three components initialization, conditions and increment. Rather it selects an object from an array/list/range.

for object list/array/range :

statements
}

Example:
for i in range (1, 10) :
print(i)

WHILE

while condition :

statements


Example:
i = 0
while ( i < 10 ) :
print(i)
i++

Practice:

  1. Remember the keyword, if, else, for, in, while, break, return etc.
  2. Experiment with different combination of loops, if-else, break
  3. Experiment removing braces and adding extra braces and see what error you get.


References:

  1. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html