0

What is the output of this code and what are the levels of debugging?

#include <iostream> using namespace std; int main() { int i=5; for(;i<7;i+=4) i=i*i; cout<<i; return 0; }

3rd Oct 2021, 5:22 AM
Nariman Tajari
Nariman Tajari - avatar
4 Réponses
+ 2
The code flow ... * Define int <i> with value (5) + Entering loop (round 1) ... * Verify is <i> (5) < 7 (yes) * Enter and execute loop body, <i> is squared and became 25 * After loop body executed, <i> is incremented by 4 and became 29 + Continue loop (round 2) ... * Verify is <i> (29) < 7 (no) Loop terminated ... * Print <i> (29)
3rd Oct 2021, 5:39 AM
Ipang
+ 1
This program would return i*i+4 which is 29... But you are on sololearn, so why not just run it and see for yourself? https://code.sololearn.com/cuszC2lEHy6v/?ref=app
3rd Oct 2021, 5:29 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Okey, here is an explanation: // i initialized with value of 5 int i = 5; // loop that iterates in range of 5-7 // with step of 4 for (; i<7; i+=4) // i is squared now, making it 25 i = i*i // this is the end of the loop so I gets // incremented by 4 because of the // loop step.. // as 25+4 is more than 7, loop // does not continue // and then just print out i cout << i;
3rd Oct 2021, 5:37 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
I ve run it mr Aleksei Radchenkov but i did not understand what is the process of the last answer its vague for me somehow that in the second iteration how the loop send the output like that.
3rd Oct 2021, 5:31 AM
Nariman Tajari
Nariman Tajari - avatar