0
Hi I played same challenges and when something like this was written: for(int i=0;i<100;i++) than it moved on towards with i=100
Isn't it i=99? Cuz I missed a lot of answers because of this. Thanks in advance :) Edit:just now I had this: int i=0; for(;i<4;i+=2) i=i*i; cout<<i%4 Isn't it 0? It said it's 2
3 Respuestas
+ 4
Cycle #1: i = 0, i * i = 0, (i += 2) == 2, (final value of i @ first cycle is 2)
Cycle #2: i = 2, i * i = 4, (i += 2) == 6, (final value of i @ second cycle is 6 and for the the third cycle, the loop breaks since 6 < 4)
cout << 6 % 4; ==> 2
+ 3
Each iteration in a for loop starts with checking the looping condition. If it's true the loop happens and at the end of each iteration the increment/decrement happens. If the condition is not satisfied and the loop exited it means that the counting variable has reached the first value that makes the condition false. In i<100; i++ it is 100 since 99 is still a valid value.
In the second case is more "complex" as the counter variable is further modified during each iteration. Upon entering the i is 0 (meeting the condition) and is multiplied by itself resulting again in 0. Then incremented by 2 before entering the next iteration. Here happens the check again, still valid since 2<4. I is again multiplied by itself becoming 4 and is then incremented by 2 becoming 6. The check happens again and now is not satisfied so the loop ends. The last value of i is 6 so i%4 is 2.
0
Ohhh thank you very much now I understand