Can someone explain this to me?

int i, j, k;
for(i = 0, j = 100; i<=j; i++, j--) k = i;

This is a C++ question I'm having difficulties with. Can someone please explain to me how I can determine the values of i,j and k? The solutions are 51,49,50 for i,j,k respectively. How did answers in that range come about?

Thanks.
Last edited on
i will count up to 50
j will count down to 50
k always = i

or did you mean something else?
so i and j would count up to 50 because i<=j. okay. so how is k determined? why do we say k=i when k is 50 and i is 51?

sorry i'm a beginner so i can't quite grasp the concept of it. thanks
closed account (48bpfSEw)
you can understand the for loop as a while loop:

1
2
3
4
5
6
7
8
9
10
11

int i=0;
int j=100;
int k=0;

while (i<=j) {
   
   k=i;
   
   i++;
   j--);
for (set value (s); condition (when does the loop stop); what happens after each loop (increment or dexcrement){

other statements;
}


so you set values of i and j to 0 and 100, you say that you want the loop to keep looping until
i <= j and with every run of the loop i is incremented by 1 and j is decremented by 1

and k is always the same as i

if you output i, j and k with every run of the loop you'll see how it works

1
2
3
4
for ( i = 0, j = 100; i <= j; i++, j--){
        k = i;
        cout << "\ti: " << i << "\tj: " << j << "\tk: " << k << endl;
        }
closed account (E0p9LyTq)
so i and j would count up to 50 because i<=j. okay. so how is k determined? why do we say k=i when k is 50 and i is 51?


The difference between the values of k and i is due to how the statements in a for loop are evaluated/executed (simplified explanation to follow):

1st - the initial value(s) are set, i = 0 and j = 100. This happens only once, the first time through the for loop.

2nd - the variable are tested for truth, i <= j. If the condition is true go to step 3. If false exit the loop.

3rd - the body of the for loop is executed, k = i.

4th - the loop variables are incremented, i++ and j--.

5th - go to step two.

So when i & j are both 50 the test condition is true and k is set to the value of i. Now i is incremented to 51 and j decremented to 49. Going back through the for loop statement the test fails and the for loop terminates. The value in k is unchanged!

The only time k & i are the same value is when the for loop's test condition is true.
If you're merely going to monitor the status of each variable from within the for loop as it does its job, you'll see everything seems to end up set to 50. Reasonable enough. Don't forget to output once more after the loop terminates to show the values the variables are left with. This will show the false condition that ultimately allowed completion of the loop ie. where i is no longer less than or equal to j.

1
2
3
4
5
6
7
8
9
10
 int i, j ,k;
    for (i=0, j=100; i<=j; i++, j--) {
      k = i;
      cout << "i= " << i << ", j= " << j << ", k= " << k << endl;
    }

    /* print values once more- this time outside of the loop to show
    the values the variables are set to and left at upon exiting */

        cout << endl << "i= " << i << ", j= " << j << ", k= " << k << endl;
Last edited on
Topic archived. No new replies allowed.