0

Hello , I have the following code:

count = 0
phrase = "hello, world"
for iteration in range(5):
    index = 0
    while index < len(phrase):
        count += 1
        index += 1
        print "index: " +str(index)
    print "Iteration " + str(iteration) + "; count is: " + str(count)

I can't understand why index goes until value 12.When index=11 ,the condition index<len(phrase) (11<12) is true ,
but when index becomes 12 ,the condition is false.Why then the value of index is 12?

Thanks!

5
Contributors
14
Replies
64
Views
3 Years
Discussion Span
Last Post by glao
Featured Replies
  • 1

    k = 0 while k < 10: k += 1 print(k) # print after increment ''' result ... 1 2 3 4 5 6 7 8 9 10 ''' Read More

  • 1

    k = 0 while k < 10: print(k) # print before increment k += 1 ''' result ... 0 1 2 3 4 5 6 7 8 9 ''' Read More

0

You print the index after the value is incremented:

        index += 1
        print "index: " +str(index)

So the loop is iterated with the value of "index" equal to 11, then the value of index is increased but the loop is not evaluated so the processing doesn't cease.

0

Sorry , I didn't understand..

You say

then the value of index is increased but the loop is not evaluated so the processing doesn't cease

but the loop is evaluated when index=11 shouldn't it be evaluated when index=12 ,so it will be evaluated as false and won't execute?

0

Rewrite your code a little ...

count = 0
phrase = "hello, world"
for iteration in range(5):
    index = 0
    while index < len(phrase):
        count += 1
        print "index: " + str(index)
        index += 1
    print "Iteration " + str(iteration) + "; count is: " + str(count)
0

Ok , but the print statement is inside the loop.Why does it matter if it is before or after?

0

The statements in the loop will complete unless you insert a break before some statements.

Something like this ...

count = 0
phrase = "hello, world"
for iteration in range(5):
    index = 0
    while index < len(phrase):
        count += 1
        index += 1
        if index >= len(phrase):
            break
        print "index: " + str(index)

    print "Iteration " + str(iteration) + "; count is: " + str(count)

Actually I find this a better style (the logic is clearer) ...

count = 0
phrase = "hello, world"
for iteration in range(5):
    index = 0
    while True:
        count += 1
        index += 1
        if index >= len(phrase):
            break
        print "index: " + str(index)

    print "Iteration " + str(iteration) + "; count is: " + str(count)

Edited by vegaseat

0

Ok , I understand completely the two codes.

But still I can't get it (regarding my code) sorry..

The while loop will be executed until the condition hits false.Why it is continuing?

0

If you put your exit condition in the while line, then whatever is in the while block of statements will execute during that final loop.

0

If I understood well ,

when index goes 12 , the while loop isn't executed (because its false) but any variables that are inside the loop and correlated with index are affected?

If is this the case it seems a little weird..

0

In a while loop, the increment of an index, counter, etc. should always be the last statement. At least, that's what I mostly do.

0
// while loop in C++
//

#include <iostream>

int main()
{
  int k=0;

   while (k < 10)
   {
      ++k;
      std::cout << k << std::endl;  // cout after increment
   }
  std::cin.get();  // console wait
  return 0;
}

/* result ...
1
2
3
4
5
6
7
8
9
10
*/
1
k = 0
while k < 10:
    k += 1
    print(k)  # print after increment

''' result ...
1
2
3
4
5
6
7
8
9
10
'''
1
k = 0
while k < 10:
    print(k)  # print before increment 
    k += 1

''' result ...
0
1
2
3
4
5
6
7
8
9
'''
0

Οκ , thanks for the answers.I think I understood.

The loop is been evaluated alright but the variable we have inside may increment.

Thanks!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.