I wrote some code I wrote to print the powers of 2 up to like 39 or 40 idk but it dm. Anyway, I wrote it and rather than run the code and it not working because of a logic error, i ran the code and found that it works, and then spotted some logic errors showing that the code shouldn't work. Here is the code:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
int j = 1;
int k = 1;
while (i < 40)
{
while (k < i)
{
j = j * 2;
cout << j <<"\n";
k++;
}
i++;
}
}
The output of this code is the powers of 2 up to about 2^40.
Why it shouldn't work: the second while loop shouldn't run because k = 1 and i = 1 so (k < i) is false. Also after each time the second while loop is completed, j and k should be reset to 1 in order for the logic to work however I don't resent them. Can someone please explain why although all these logic errors, the code still works?
Also I tried this in python and got the same result.