Page 298 - Computer Science V2.0 Class 11
P. 298

11                                              LOOPING IN PYTHON















                Chapter Outline


                11.1 Need for Repeated Execution of Statements     11.2 range()
                11.3 for Statement                                 11.4 while statement
                11.5 Jump Statements                               11.6 Nested Loops
                11.7 Printing Patterns                             11.8 Infinite Loop
                11.9 pass Statement





              Introduction

              In our day-to-day lives, many tasks are performed repeatedly. For example, food is prepared three times a day, plants
              are watered daily, and bills are paid monthly. Similarly, in a program, parts of code are often required to be executed
              repeatedly. For example, we may be required to compute the percentage of marks for each student in a class or compute
              total wages for each employee in the company. Computers can be programmed to execute such tasks repeatedly. The
              repeated execution of statements in a program is called iteration, or loop. Python provides for and while statements
              to perform repeated execution of statements.

              11.1 Need for Repeated Execution of Statements

              Let's say we want to find out what the average marks were for a group of 10 students. From what we've learned so far,
              we know that the task can be easily done by Program 11.1

               Program 11.1 Computation of average marks of a group of 10 students in an examination.

                01 '''
                02 objective: To find the average marks of a group of 10 students in an examination.
                03 Inputs: Accept data interactively
                04 Output: Average marks
                05 '''
                06 sumMarks = 0
                07 nStudents = 10
                08 marks = int(input('Enter marks: '))
                09 sumMarks = sumMarks + marks
                10 marks = int(input('Enter marks: '))
                11 sumMarks = sumMarks + marks
                12 marks = int(input('Enter marks: '))
                13 sumMarks = sumMarks + marks

               284   Touchpad Computer Science (Ver. 2.0)-XI
   293   294   295   296   297   298   299   300   301   302   303