c++ - For loop returns ** value. Please explain -
can explain please why output of next code **
?
#include <iostream> using namespace std; int main() { (float val = -10.0; val < 100; val = -val * 2) { if (val < 0 && -val >= 40) break; cout << "*"; } homecoming 0; }
would please go bit bit in explanation? much
iteration 1 val -10
if ((-10 < 0) && (+10 >= 40))
false so doesn't break out of loop *
printed loop condition: (-10 < 100)
true so loop continues val updated: val = -val * 2
val = +10*2 = 20
iteration 2 val 20
if
statement short-circuits false due (20 < 0)
beingness false so doesn't break out of loop *
printed loop condition: (20 < 100)
true so loop continues val updated val = -20*2 = -40
iteration 3 val -40
if((-40 < 0) && (+40 >= 40))
true so breaks out of loop before printing again
c++
No comments:
Post a Comment