c++ - Understanding a loop output -
can explain me why output follows?
why loop runs second time after (int i) gets value 9, not less 5?
it seems if (int i) greater 5 still adds 3 body won't run second time. why?
#include <iostream> using namespace std; int i=0; int main() { for(;i<5;i+=3){ i=i*i; } cout << << endl; //output: 12(i)
it works way, more or less:
i = 0 < 5 ? yes, keep on i*i = 0 i+=3 => ==3 < 5 ? yes, keep on i*i = 9 i+=3 => 12 < 5 ? no, exit loop
you write for(initialization; condition; excuteattheendofcycle)
: initialization
executed once @ beginning, condition
evaluated before each cycle, excuteattheendofcycle
(i+=3
, in case), it's executed @ end of each cycle, before further evaluation of condition
Comments
Post a Comment