Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | vequa |
I want to skip an iteration of a for loop (but only sometimes so just increasing ‘i’ by 2 every time won’t work).
I have something like this:
for i in range(5):
print(i)
if i == 2:
i += 1
and I would want it to print:
0
1
2
4
but this doesn’t work this way and it will just print: 0, 1, 2, 3, 4.
I guess this is because you are iterating through an array and of course it makes sense why it doesn’t work then.
But in other languages like Java or JavaScript this would work perfectly fine so I don’t know if I am doing something completly wrong here.
Here is an example of what I would write in JavaScript and what it would print:
for(let i = 0; i < 5; i++)
{
console.log(i);
if(i === 2)
{
i++;
}
}
Console:
0
1
2
4