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!

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.

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?

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)

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

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)

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?

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.

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..

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.

Write the while loop in C or C++ and it will behave the same way.

// 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
*/
k = 0
while k < 10:
    k += 1
    print(k)  # print after increment

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

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

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

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

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.