You seem to be thinking that “j will be 2” because you see two “+” in output in that line.
But that only means that the inner loop is executed twice. Which is because (quoting Barmars comment):
When
i == 4
, the inner loop will only loop twice, withj == 5
andj == 4
.
You analysed the detailed behaviour of your code based on output, which is good. But if something puzzles you then you either need more output on exactly the detail which puzzles you; e.g. by actually outputting the value in question for verifying your assumptions. Or you could use a debugger.
For example. I changed your code in a way which probably makes things very obvious:
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 5; i++)
{
printf("%d: ", i);
for (int j = 5; j >= i; j--)
{
printf("%d", j);
}
printf("\n");
}
}
It gets you an output (e.g. here https://www.onlinegdb.com/online_c_compiler ) of:
0: 543210
1: 54321
2: 5432
3: 543
4: 54
5: 5
Where the line 4: 54
indicates two inner loop iterations, with values for j
of 5 and 4, while i
is 4.
0
solved Nesting For loop in c , at i = 4 ; j will be 2 how does it satisfy the condition? [closed]