Page 149 - Information_Practice_Fliipbook_Class11
P. 149

for c in 'ROSE':                        Good Code            For each value of the control variable c in the
                 if c == 'O':                        O                    string  'ROSE',  the  if-else  statement  is
                     print(c)                        Good Code            executed.
                 else:                               Good Code
                     print('Good Code')
             for c in 'ROSE':                        O                    For each value of the control variable c in the
                 if c == 'O':                        Good Code            string 'ROSE' the if statement is executed.
                     print(c)                                             The else clause is part of the for statement,
             else:                                                        and it is executed after all iterations of the for
                 print('Good Code')                                       statement have been completed.

            6.4 while Statement

            The while statement is used to execute a sequence of statements over and over again as long as the condition
            specified in the while statement is True. The sequence of statements that is executed repeatedly is called body of
            the while statement. The while statement has the following syntax:
            while test condition:
                       Body of while loop
            [else:
                       Statements]
            In the above syntax description:

            •   while is a keyword.
            •   test condition is the expression that will evaluate to either True or False.
            •     body of while loop constitutes the statement(s) that will be executed if the test condition is True. These are
                determined through indentation. The first un-indented line marks the end of the loop.
            •     else (optional) is a keyword and the statements in else block (optional) will be executed after all the possible
                iterations of the while loop are executed.
            We first discuss the while statement, ignoring the optional else clause. Fig 6.2 shows a flowchart that depicts
            the execution of such a while statement. Note that the Boolean expression (also called a conditional expression,
            test expression, or just a condition) in the while statement is evaluated before the body of the while statement
            is executed. If the Boolean expression yields False, the loop's body is skipped, and the execution of the while
            statement ends. However, if the Boolean expression yields True, the body of the while statement is executed. On
            executing the loop's body, the Boolean expression is evaluated again. If the Boolean expression yields True, the

            loop's body is executed again. This process of evaluating the Boolean expression and executing the body of the while
            statement is repeated until the Boolean expression becomes False.


                                                              Enter while loop


                                                            Test     False
                                                          Expression


                                                               True

                                                           Body of
                                                            while

                                                                 Exit loop
                                                     Fig 6.2: while statement


                                                                                              Looping in Python  135
   144   145   146   147   148   149   150   151   152   153   154