Lecture 3

Repetition While and For loop

Repetition structure

Repetition structure is used to execute a block of code multiple times. In Python, we have two types of repetition structures: While and For
Indefinite Iteration : An unknown number of iterations. Ask the user to guess the lucky number. You don’t know how many attempts the user will need to guess correctly. It can be 1, 20, or maybe indefinite. In such cases, use a while loop.
Finite Iterations : Fixed number of iterations. Print the multiplication table of 2. In this case, you know how many iterations you need. Here you need 10 iterations. In such a case use for loop.

  • WHILE

    While loop is used to execute a block of code multiple times. The block of code will be executed until the condition becomes false.



  • When to use while Loop in Python

    Indefinite Iteration: The while loop will run as often as necessary to complete a particular task. When the user doesn’t know the number of iterations before execution, while loop is used instead of a for loop

Example 1


Example 2




Example 3










  • The loop with continuing forever if you forgot to increment counter in the above example

  • FOR


As opposed to while loops that execute until a condition is true, for loops are executed a fixed number of times, you need to know how many times to repeat the code.An unknown number of times: For example, Ask the user to guess the lucky number. You don’t know how many attempts the user will need to guess correctly. It can be 1, 20, or maybe indefinite. In such cases, use a while loop.

Example 1




The range() function returns a sequence of numbers starting from 0 (by default) if the initial limit is not specified and it increments by 1 (by default) until a final limit is reached. The range() function is used with a loop to specify the range (how many times) the code block will be executed. Let us see with an example.


Example 2








  • The loop runs till it reaches the last element in the sequence. If it reaches the last element in the sequence, it exits the loop. otherwise, it keeps on executing the statements present under the loop’s body


Last Example






  • CALCULATING RUNNING TOTAL

A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator. Programs that calculate the total of a series of numbers typically use two elements:


Example




  • SENTINELS

A sentinel is a special value that signals the end of a sequence of values. For example, a sentinel value of 0 could be used to indicate the end of a list of test scores. The sentinel value is not included in the list of test scores.






  • INPUT VALIDATION LOOPS


  • Input validation is the process of inspecting data that has been input to a program, to make sure it is valid before it is used in a computation. Input validation is commonly done with a loop that iterates as long as an input variable references bad data.

    For example, suppose you want to ask the user for a number between 1 and 100. If the user enters a number outside that range, you want to display an error message and ask the user to reenter the number. You can use a loop to keep asking the user for a number until the user enters a valid number.




  • Try it yourself


  • NESTED LOOPS



  • A loop that is inside another loop is called a nested loop.

  • CONTINUE

    The continue statement is used to skip the rest of the code inside a loop for the current iteration only.Loop does not terminate but continues next iteration.


  • BREAK

    The break statement is used to terminate the loop containing it. Control of the program flows to the statement immediately after the body of the loop.


  • PASS

    The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.


Thanks !

If you have any questions, please feel free to contact me.