which variable stores the sum of numbers entered during the execution of a loop ????
Answers
Introduction
In this chapter, you will learn how to make the computer execute a group of statements over and over as long as certain criterion holds. The group of statements being executed repeatedly is called a loop. There are two loop statements in Python: for and while. We will discuss the difference between these statements later in the chapter, but first let us look at an example of a loop in the real world.
A petrol attendant performs the following actions when serving a customer:
greet customer
ask for required type of petrol and amount
ask whether customer needs other services
ask for required amount of money
give money to cashier
wait for change and receipt
give change and receipt to customer
say thank you and goodbye
A petrol attendant performs these steps for each customer, but he does not follow them when there is no customer to serve. He also only performs them when it is his shift. If we were to write a computer program to simulate this behaviour, it would not be enough just to provide the steps and ask the computer to repeat them over and over. We would also need to tell it when to stop executing them.
There are two major kinds of programming loops: counting loops and event-controlled loops.
In a counting loop, the computer knows at the beginning of the loop execution how many times it needs to execute the loop. In Python, this kind of loop is defined with the for statement, which executes the loop body for every item in some list.
In an event-controlled loop, the computer stops the loop execution when a condition is no longer true. In Python, you can use the while statement for this – it executes the loop body while the condition is true. The while statement checks the condition before performing each iteration of the loop. Some languages also have a loop statement which performs the check after each iteration, so that the loop is always executed at least once. Python has no such construct, but we will see later how you can simulate one.
Counting loops are actually subset of event-control loop - the loop is repeated until the required number of iterations is reached.
If you wanted to get from Cape Town to Camps Bay, what loop algorithm would you use? If you started by putting your car on the road to Camps Bay, you could:
drive for exactly 15 minutes. After 15 minutes, stop the car and get out.
drive for exactly 8km. After 8km, stop the car and get out.
drive as long as you are not in Camps Bay. When you arrive, stop the car and get out.
The first two algorithms are based on counting – the first counts time, and the second counts distance. Neither of these algorithms guarantees that you will arrive in Camps Bay. In the first case, you might hit heavy traffic or none at all, and either fall short of or overshoot your desired destination. In the second case, you might find a detour and end up nowhere near Camps Bay.
The third algorithm is event-controlled. You carry on driving as long as you are not at the beach. The condition you keep checking is am I at the beach yet?.
Many real-life activities are event-controlled. For example, you drink as long as you are thirsty. You read the newspaper as long as you are interested. Some activities are based on multiple events – for example, a worker works as long as there is work to do and the time is not 5pm.
The while statement
Python’s event-controlled loop statement is the while statement. You should use it when you don’t know beforehand how many times you will have to execute the body of the loop. The while-body keeps repeating as long as the condition is true. Here’s a flow control diagram for the while statement:
None
The loop consists of three important parts: the initialisation, the condition, and the update. In the initialisation step, you set up the variable which you’re going to use in the condition. In the condition step, you perform a test on the variable to see whether you should terminate the loop or execute the body another time. Then, after each successfully completed execution of the loop body, you update your variable.
Note that the condition is checked before the loop body is executed for the first time – if the condition is false at the start, the loop body will never be executed at all.
Here is a simple Python example which adds the first ten integers together:
total = 0
i = 1
while i <=10:
total += i
i += 1
The variable used in the loop condition is the number i, which you use to count the integers from 1 to 10. First you initialise this number to 1.
Read more on Brainly.in - https://brainly.in/question/8390362#readmore