+ 1

C question: Why the (condition) in for loop should be relevant to the (update statement)

For example here Int i = 0; Int x = 0; For (i = 1; i < 5; i++) Why can't i just replace the ( i++) with (x++ ) Maybe i want to add that 1 to the (x) not to the (i) for some reason i want So why can't i do that

18th Jan 2022, 10:09 AM
Abdallah Ashraf
2 Respuestas
+ 4
You can, but look again at the condition, the loop was supposed to continue repetition while i < 5. If you increment value of <x> then the condition ( i < 5 ) will remain true (as <i> was zero and never got incremented) for eternity, and your loop goes round infinitely.
18th Jan 2022, 10:22 AM
Ipang
+ 2
u can but it will be an infinite loop since every loop u dont add any i which i will always be 0. if u want to add both variables on every loop u can do it this way for (i = 0; i < 5; i++, x++)
18th Jan 2022, 10:29 PM
Shen Bapiro
Shen Bapiro - avatar