Theoretical question about while and indentation

2
count = 0
while (count < 10):
    # Ponto A
    print ("Olá...", count)
    count =  count + 1
    # Ponto B
# Ponto C

Why

  

count < 10 is always true at point B.

is not true if "Point B" belongs to the same indentation of "Point A" within the while?

Another question:

i = 6
while (i > 0):
    i = i - 3
    print (i)

Why is the output 3 and 0 and not only 3?

Thank you!

    
asked by anonymous 09.06.2017 / 14:27

2 answers

6

You asked why in the code below:

(1) i = 6
(2) while (i > 0):
(3)   i = i - 3;
(4)   print (i)

The output is 3 and 0 and not only 3. The answer is simple, but it requires you to consider how Python interprets each command line. For ease, I listed the lines in the above code, okay?

The first line executed is (1). It simply assigns the value 6 to the variable i. The second line executed is (2). It checks if i > 0, and since i is 6 (at this time), this is true and Python allows execution to enter the loop. Thus, the next line executed is (3), which causes i to receive i - 3. Since at this point i is 6, the result is i = 6 - 3, then i = 3. This value is printed on the next line , as expected.

Execution then returns to row (2). The current value of i is 3 (which, incidentally, was the last printed). Since i > 0 (because 3 > 0) is true, the processing re-enters the loop. Line (3) subtracts again, so that i becomes 0 (since it does i = 3 - 3). Line (4) then prints the current value of i, which is 0.

Finally the execution returns to the line (2), but this time the processing does not enter the loop because the condition is no longer true.

Try running this code:

i = 6
while True:
  i = i - 3
  if(i > 0):
    print (i)
  else:
    break

Note that in this code the condition of the loop is actually always true (after all, it is fixed True ), and the correct condition (its original condition) was moved into the loop in a if . Analyze with this new code how the computer processes line by line in a sequential way, following the current value of the variable in each step (this procedure usually - or used to be called " table test "). It will help you understand how "logic" influences problem solving. :)

    
09.06.2017 / 14:40
1
  

count < 10 is always true at point B.

In fact it is not. At point B the count will be the previous count +1. Your maximum will be 9, as the while stops if the count equals 10. Test your script.

i = 6
while (i > 0):
  i = i - 3;
  print (i)

First : The indentation used is out of the recommendations. Within a loop, function, class or whatever, the recommended and commonly used are 4 spaces. This is important for maintaining standardization and good visual organization of the code, so if too many people use the code, they will be familiar with the formatting. Second : A semicolon is unnecessary < br> Third and answer the question : Think like the script:
The i enters the loop as 6, becomes 3, and is printed. The loop resumes with i as 3, becomes 0, and is printed again. Only then will i be tested again (this time being 0) and will not enter the loop.

    
09.06.2017 / 14:37