Reputation: 25
This program (in C) doesn't output what I'd expect:
int main()
{
int i, j ;
for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
continue ;
printf ( "\n%d %d\n", i, j ) ;
}
}
}
I think it should be
1 2 1 3 2 1 2 3
But the program outputs
1 2 2 1
Why is this?
Upvotes: 0
Views: 147
Reputation: 60681
The values of i
and j
go through this sequence:
i j
---
1 1
1 2
2 1
2 2
Note that the i++
and j++
increments happen after each iteration of the loop body.
The only cases where your printf
is called are where i
and j
are different. That means you get:
1 2
2 1
Upvotes: 1