Page 131 - ComputerGenius_V2.1_Class8
P. 131

print("Zero")

                   else :
                     print("Negative number")
                 Output:
                   Positive number.
                 Description:

                 In the above program, 2 conditions are being tested using if and elif statement, if both the conditions
                 are False then else statement is executed.






                    l   Only one block among the several ‘if-elif-else’ blocks is executed according to the condition.
                    l   The ‘if’ block can have only one ‘else’ block. But it can have multiple ‘elif’ blocks.


                 Iterative Statements


                 Iterative statements or looping statements allow us to execute a set of code repeatedly as long
                 as a specified condition is true. These are mainly used when we need to run the same code again
                 and again.

                 The for Loop

                 The for loop executes a simple or compound statement for a fixed number of times i.e looping a
                 set of instructions for a specified number of times. For loop first initialises the counter variable

                 once, then it checks the range, if within range it executes the statement within for loop and then
                 increments or decrements the counter variable as specified by step_size. The syntax of the for
                 statement is given below:

                 for <counter variable> in range(start, stop, step_size):

                     Statements

                 The range( ) function is an in-built function in Python. This function generates a sequence of numbers.
                 A sequence is a succession of values bound together by a single name. The range( ) function is
                 generally used in the for loop to iterate over the sequence of numbers.

                 The general syntax of using the range( ) function is given below:

                    range(start, stop, step_size):  The  start  specifies  the  starting  point  for  generating  numbers.
                    By default, it starts from 0. The stop specifies the position up to which the numbers are to be
                    generated (last number is not included). The step_size specifies the increment or decrement.
                    By default, the value of the step_size = 1 and numbers are generated with an increment of
                    1. However, we can generate numbers by specifying the value of step_size according to our

                    requirement.




                                                                                    Control Flow Statements   129
   126   127   128   129   130   131   132   133   134   135   136