Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while loop requires a specific order to work?

Tags:

python

When I use the following code I get a correct answer of 28.

#want to find sum of only the positive numbers in the list
#numbers = [1,3,9,10,-1,-2,-9,-5]
numbers = [4,6,2,7,9]
numbers.sort(reverse = True) # sets to greatest to smallest numbers
total = 0
_index = 0
#while numbers[_index] > 0 and _index < len(numbers):
while _index < len(numbers) and numbers[_index] > 0:
    total += numbers[_index]
    _index += 1
print(total)

When I try the following code I get "IndexError: list index out of range", I only changed the while loop. Why does one condition have to be first, does it not check each one before running this loop?

#want to find sum of only the positive numbers in the list
#numbers = [1,3,9,10,-1,-2,-9,-5]
numbers = [4,6,2,7,9]
numbers.sort(reverse = True) # sets to greatest to smallest numbers
total = 0
_index = 0
while numbers[_index] > 0 and _index < len(numbers):
#while _index < len(numbers) and numbers[_index] > 0:
    total += numbers[_index]
    _index += 1
print(total)
like image 206
ewe Avatar asked Jun 12 '20 06:06

ewe


People also ask

How does a while loop work?

A while loop is a loop that continues to run and execute a while statement as long as a predetermined condition holds true. After each iteration, the loop checks that the condition remains true. If the condition is now false, the loop terminates.

What is the structure of while loop?

Overview. The while construct consists of a block of code and a condition/expression. The condition/expression is evaluated, and if the condition/expression is true, the code within all of their following in the block is executed. This repeats until the condition/expression becomes false.

How does a while loop start?

Syntax. The while loop starts by evaluating condition . If condition evaluates to true , the code in the code block gets executed. If condition evaluates to false , the code in the code block is not executed and the loop ends.

In which sequence the while loop is executed?

The DO-WHILE-END sequence. The DO-WHILE-END sequence creates a loop that executes while a specified condition is true. If the condition is not true, the loop does not execute. The condition must be either a comparative expression or a variable containing a comparative expression.


2 Answers

The answer to your question is called short-circuit evaluation.

If the first part of a compound conditional AND statement evaluates to False then the second is not being evaluated in the case of an AND conditional.

If the conditional is an OR conditional, if the first part evaluates to True then there is no point in checking the second part of the conditional.

In your case with the indexes. If the length is already past the length of the array and you check to see that this is the case first - you will never go out of bounds:

# here if the index is out of bounds we won't procede to access the elements
while _index < len(numbers) and numbers[_index] > 0:

If you first try to access the element in the array you risk going out of bounds.

like image 108
bhristov Avatar answered Sep 26 '22 00:09

bhristov


It's pretty simple if you think about it, this applies to if statements as well. If you have the following scenario:

if False and True:

It doesn't even check the second part, it doesn't matter, because the first part isn't True.

Same applies to

if True or False:

The second part doesn't matter because the code inside the if statement is going to run regardless.

like image 28
Mandera Avatar answered Sep 23 '22 00:09

Mandera