20. Flow control

 


Flow control:

            The flow control means which order to move forward in executing time is decided by flow control

flow controls are 3 statement types

1.     Conditional Statements 

2.    Iterative Statements 

3.    Transfer Statements


1.   Conditional Statements 

    1)  if

    2)  if-elif

    3)  if-elif-else

 2.    Iterative Statements 

    1) for

    2) while

3.    Transfer Statements

    1)  break

    2)  continue

    3)   pass


and some are there like return and all we will update it later.

some valid points on flow control

There is no switch statement in python

do-while not in python

goto statement not in python.

before going to flow control we must focus on indentation. 

Indentation means proper gap under the condition.

the indentation will help with simple readability and the code looks good.

Ex:

if condition:
    body
else:
    post-code

check the above example if the condition below the body is moving some tab gap and else under post-code with some tab gap. it means the body is under control on the if statement.

if the body starts with exactly under if then it's considered that it's not under control on the if statement.

nowadays smart IDE's are easily arranged in the indentation mode. but sometimes it may trouble.

if condition:

if condition: statement

OR

if condition : 

    statement-1 

    statement-2 

    statement-3

If..... the condition is true then statements will be executed. 

Ex:

x = int(input("Enter number:"))

if x != 10:
    print("Yes your guess is correct") # it's on if condition

print("try one more time") # it's not if condition

Enter number:400  #result

Yes your guess is correct

try one more time

2) if-else:

if condition: 

    Action-1

else:

    Action-2

if the condition is true then Action-1 will be executed otherwise Action-2 will be executed.

Ex:

x = int(input("Enter number:"))

if x != 10:
    print("Yes your guess is correct")

else:
    print("your enter equal number")

print("try one more time") # it's not condition mode

Result:

Enter number:10

your enter equal number

try one more time

if-elif-else:

if condition1: 

    Action-1

elif condition2: 

    Action-2

elif condition3:

    Action-3 

elif condition4:

    Action-4

... else:

Default Action

Based on conditions the corresponding action will be executed.

Ex:

number = int(input("Enter the number on 0-9 only: "))
if number == 1:
    print("you just started")
elif number == 5:
    print("you are in middle stage")
elif number == 9:
    print("you are reached end point")
else:
    print("please try again")

print("try one more time") # it's not condition mode

Enter the number on 0-9 only: 6   # Result

please try again

try one more time

Note:

1)   else part is always optional. Hence the following are various possible syntaxes.

1)   If

2)   if – else

3)   if-elif-else

4)   if-elif

some examples of the above conditions


find the positive number or negative number?

num = int(input('enter the number: '))
if num > 0:
    print(num, "is a positive number")

elif num < 0:
    print(num, "is a negative number")

print("one more try")

enter the number: -567    # Result

-567 is a negative number

one more try


find the biggest numbers in 2 given numbers?

a = int(input('enter the number: '))
b = int(input('enter the number: '))

if a > b:
    print('biggest number is', a)

else:
    print('biggest number is', b)

# Result

enter the number: 3

enter the number: -2

biggest number is 3


find the smallest numbers in 2 given numbers?

a = int(input('enter the number: '))
b = int(input('enter the number: '))

if a < b:
    print('smallest number is', a)

else:
    print('smallest number is', b)

# Result

enter the number: 3

enter the number: -2

smallest number is -2


find the biggest numbers in 3 given numbers?

a = int(input('enter the number: '))
b = int(input('enter the number: '))
c = int(input('enter the number: '))

if a > b and a > c:
    print('biggest number is', a)

elif b > c:
    print('biggest number  is', b)

else:
    print('biggest number  is', c)

# Result

enter the number: 234

enter the number: 345

enter the number: 349

biggest number  is 349


find the smallest numbers in 3 given numbers?

a = int(input('enter the number: '))
b = int(input('enter the number: '))
c = int(input('enter the number: '))

if a < b and a < c:
    print('smallest number is', a)

elif b < c:
    print('smallest number  is', b)

else:
    print('smallest number  is', c)

# Result

enter the number: 235

enter the number: 456

enter the number: 1234

smallest number is 235

Check the number in between (1-100) or not?

a = int(input('enter the number: '))

if a >= 1 and a <= 100:
    print('number {} is in between 1-100'.format(a))
else:
    print('number {} is not in between 1-100'.format(a))

# Result

enter the number: 67

number 67 is in between 1-100

enter the number: 122

number 122 is not in between 1-100

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post