1
for i in range(5):
    if (i==2):
        i =i -1
    print(i)

I am getting 0,1,1,3,4 (a finite result) and when I am using C++

#include<iostream>
using namespace std;

int main(){
    for (int i=0;i<10;i++){
        if(i==2){
            i = i-1;
        }
        cout<<(i)<<endl;
    }
}

I am getting infinite output, The Python code should also output infinite after reaching the value 2. Not able to figure out why I am getting different result?

Gaurav
  • 21
  • 5
  • possible duplicate ? https://stackoverflow.com/questions/15363138/scope-of-python-variable-in-for-loop seems to be the same problem – WARhead Jul 12 '20 at 07:21

5 Answers5

6

In Python, you create range(5), i.e. a set of 0, 1, 2, 3, 4. When you iterate through it, it does exactly that - takes i=0 for first loop and after finishing the iteration, it takes the next element i=1 and so on. Changing the value of i inside loop doesn't change the next item.

In C/C++ however, you act directly on i variable. You aren't creating a predefined set of values it's supposed to take. You can modify it however you want.

You could achieve a "Python-style" for-loop in C++ using ranged for-loop:

for (int i: { 0, 1, 2, 3, 4 } ) {
    if(i==2) {
        i = i-1;
    }
    std::cout<< i << std::endl;
}
Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
2

In python :

you only get something like a "copy", that you can change in the way you like without changing the loop counter.

In C++:

you change the loop counter itself which leads to an endless loop

Note:

If you want to test it, just put a print statement print('--', i) at the start of the for loop, you can clearly understand what's happening.

Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
2

The equivalent for the python for-loop in C++ would be something like this:

#include<iostream>
using namespace std;

int main(){
    int foo[5] = {0, 1, 2, 3, 4};
    int i;
    for (i=0; i<5; i++){
        int val = foo[i];
        if(val == 2){
            val = val - 1;
        }
        cout<<(val)<<endl;
    }
}

Which yields:

0
1
1
3
4

The range() function give your an iterator which is like foo above (in function), i takes values from that iterator & that is used in the body of the for loop. If you reassign i in the body, that change is lost as soon as the next time the loop runs - since i is reassigned at the beginning of the loop. So unlike in C++, changing the variable used in the loop does not modify the state of the loop.

rdas
  • 20,604
  • 6
  • 33
  • 46
1

In c++ you are incrementing a value stored in i at the beginning of a loop.

In python you are assigning a value to i at the beginning and then you are subtracting one from it if it is equal to 2.

The equivalent of the python code in c++ would be

for(int i : {1, 2, 3, 4, 5})
{
    if(i==2)
        i = i-1;
    std::cout<<i<<std::endl;
}
 
SzymonO
  • 432
  • 3
  • 15
0

Let's split down the loops to help you better understand why this is happening.

First, it is important to understand range(...). In python range(5) or list(range(5)) returns [0, 1, 2, 3, 4] which is a list. range(...) always returns a list so when you use for loops in python, you are not actually incrementing the actual values, rather you are looping on the list of values generated using range(...), and even if you modify the values, the change would be temporary because as soon as the iteration ends, the looping variable would be assigned the next value in the list generated using range(...)

This is not the case with c++. In c++ for loops works differently where it loops as long as the loop variable (i) < 10 (in your case). No list or array is generated, you are working with integer values, any change you apply to i doesn't go away because at the end of iteration i would become i=i+1 .

This is the equivalent of your python code:

for i in [0, 1, 2, 3, 4]:
    if (i==2):
        i =i -1 # temporary assignment to i-1=2-1 = 1
        # temporary because after the loop ends i would become the next value in the list i.e 3
    print(i)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51