Conditional Flow & Loops

 Based on how the expressions evaluate, the program can decide to skip instructions, repeat them, or choose one of several instructions to run. Flow control statements often start with a part called the condition, and all are followed by a block of code called the clause.

Condition is just a more specific name in the context of flow control statements. Conditions always evaluate down to a Boolean value (True/False). A flow control statement decides what to do based on whether its condition is True or False.

IF Statements:

The most common type of flow control statement. An if statement's clause will execute if the statement's condition is True. The clause is skipped if the condition is False.

ELSE:

An if clause can optionally be followed by an else statement. The else clause is executed only when the if statement's condition is False.

          if  100 % 2 ==0:                                                                if  101 % 2 ==0:

                   return 'Even'                                                                          return 'Even'

           else:                                                                                  else:

                   return 'Odd'                                                                            return 'Odd'

ELIF Statement:

While only one of the if or else clauses will execute, you may have a case where you want one of many possible clauses to execute. The elif statement is an " else if " statement that always follows an if or another elif  statement.

Ex: 

x=100

if  x > 100:

     print( " x is greater than 100 ")

elif x < 100:

     print(" x is less than 100 ")

else:

     print (" x is equal to 100 ")

WHILE LOOP STATEMENT:

You can make a block of code execute over and over again with a while statement. The code in a while clause will be executed as long as the while statement's condition is True.

Ex: i = 1

      while i < 6:

            print( i )

            i = i+1

O/P: 1

        2

        3

        4

        5

Break:

There is a shortcut to getting the program execution to break out of a while loop's clause early. If the execution reaches a break statement, it immediately exits the while loop's  clause.

Ex: i = 1

      while i < 6:

            print( i )

           if i = = 3:

               break

            i = i+1

O/P: 1

         2

         3

Continue:

Like break statements, continue statements are used inside loops. When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop and reevaluates the loop's condition.

Ex: i = 0

      while i < 6:

              i = i+1

              if i = = 3:

                    continue,

             print( i )  

O/P: 1

        2

        4

        5

        6

Note: Number 3 is missing.

FOR LOOP:

The while loop keeps looping while its condition is True, but what if you want to execute a block of code only a certain number of times? you can do this with a for loop statement. A for loop is used for  iterating over sequence ( i.e a List, a Tuple, a Set, a Dictionary, and a String )

fruits = ['Apple' , 'Banana' , 'Cherry' ]

for  x  in fruits:

      print(x)

O/P: Apple

       Banana

       Cherry

for  x  in range (10):

          print(x)

O/P:

0

1

2

3

4

5

6

7

8

9

for  x  in range (1,11,2):

       print(x)

O/P:

1

3

5

7

9

Comments