Flow control

By | April 30, 2018

Programming is about making decisions depending the results obtained. Its about checking and verifying whether certain conditions are met or not. In any programming language we have loops which serves this purpose.

Before going to start with loops I would like to revisit comparison operators.

These operators will result in either true or false . Depending on the result we will make a decision and move.  Example is here:

Remember that indentation is very important when you are writing blocks of codes especially loops. If indentation increases you are making it as sub block. If indentation is decreasing you are returning to main block.

Flow control statements:

if else:

Piece of code included in if block will execute if the given condition is true. The clause is skipped if the condition is False. Syntax structure is:

1.if keyword followed by the condition

2.colon

3.Give indentation and write piece code to be executed.

#if else Example
age=input(“Enter your age:”)
if int(age)<=18:
       print(“You are minor”)
else:
      print(“You are major”)

output:

Enter your age:27
You are major

if elif:

#if elif Example
age=input(“Enter your age:”)
if int(age)<18:
             print(“You are minor”)
elif int(age)==18:
            print(“You have just entered into 18, eligible to be major”)
else:
            print(“You are already major”)

output:

Enter your age:18
You have just entered into 18, eligible to be major

It is pretty much similar to any other language, Major difference is of indentation.

  1. If you want to provide a single line in if block, you can directly mention it beside to if statement like below:

Loops: What if I have to execute a piece of code several number of times until the specified condition was met. Here come loops concept. There are different kinds of loops that Python 3 offers. Lets have a look at those.

  1. while
  2. for

1.while: syntax will consist of

a. while keyword followed by condition.

b. A colon.

c. Next write block of code with indentation.

#while loop Example
i=0
while i<4:
      print(i)
      i=i+1

Output:

0
1
2
3

Let us write a loop which will terminate after user gave termination input: Observe that until user gives yes as an input it will continue asking for input.

Well there is another way to escape from loop. There are control statements which are offering these.

  1. break
  2. continue

1.break:

break statement will abruptly ends the execution and comes out of the loop.  Control will go to next statement after the block of loop code. It is quite interesting, isn’t it?.

2.continue:

If program reaches condition statement execution will skip the next block of code and jumps to the starting of loop.  Let us see the example code snippet for this.

So execution will always jump to starting of the loop if user name is not John. This is how continue works. So break will bring execution out of the loop whereas Continue will pass execution to starting of the loop. You can use these statements only inside the loops but not else where.

for loop:

Before checking out the concepts of for loop let us look at range() function. You will understand why range() function is required in later half.

range() :

range() function will accept parameter count of one,two and three.

range() function with one parameter:

range(Ending value) – Results in list of values from 0 to EndingValue-1

So range(5) will result in 0,1,2,3,4

range(0)  will result in no values

Try range(-3) and comment out the output.

range() function with two parameters:

range(StartingValue,EndingValue)- Results in list of values from StartingValue to EndingValue-1.

So range(2,6) will result in 2,4,5

range(-7,-2) will result in -7,-6,-5,-4,-3

range() function with three parameters:

range(StartingValue,EndingValue, StepCount)-Results in list of values from StartingValue to EndingValue-1 and with interval of mentioned step count.

So range(2,12,2) will result in [2,4,6,8,10]

We can give negative step count as well.

So range(8,1,-3) will result in [8,5,2]

Lets watch for loop movie now.

syntax:

  1. for keyword
  2. variable name
  3. in keyword
  4. range() function followed by :
  5. block of code to be executed in for loop with indentation

Do try some basic programs with other formats of range() function in for loop.

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.