How do I read this exoression?

I'm struggling to figure out this expression.

 
  Int I =o;j=5, k=I=--j;


I is equal to zero and j=5. Last expression, k and I are equal to 4.

Would this be correct?

My other question is that when k=I, is the last exoression "--j" same as j=--j
split this into lines;
int I = 0;
j = 5, k = I = --j;


First I is set to 0.
Then j is set to 5;
Then --j (j gets set to j-1). J becomes 4;
I is set to j (4)
k is set to I (4)

--j is short for j = j - 1;
so k = I = --j is:
k = I = j = j - 1;
Last edited on
I guess I got this now :) but in Boolean expressions like (i*j%k==0 || j--<k++i got the first expression but I can't seem to grasp the second expression.

Would j-- be same as j=j-- meaning saving the value of j and then decrementing. Also in this Boolean expression since it's a if statement, this would not change the original value of j or k Correct?
Not attempting to bring my post all the way up, but can anyone explain this to me please? :)
Anyone :(
j -- < k++ is saying:
j = j -1;
k = k + 1;
Last edited on
Thank you! :D but since this is in a else if(Boolean exoression) statement, would the value of j change from the original value. For instance. Let's say the value of j=2. After this Boolean exoression, would the value of j be changed to whatever that is?
Run this and it will explain to you exactly what is happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
int main()
{
	int j = 10;

	if(j-- == 9) // Checks to see if j is equivalent to 9. It isn't. Regardless it subtracts 1.
		std::cout << "First expression: " << j; //Outputs 9
	else if(j-- == 9) // Checks to see if j is equivalent to 9. It is. Regardless it subtracts 1 again.
		std::cout << "Second expression: " << j; //Outputs 8.

	while(true)
	{
	}

	return 0;
}


It checks to see if j is equivalent to whatever you're comparing it to before it does the operations on j.
Last edited on
I did run it and in fCt was playing around it with on visual studio. I do get it now but k=j-- I don't understand how the value of 3 is being stored for j. Can you explain to me how this expression is working step by step please. The original value of k and j are 4.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// postfix operator
int operator --(int) {
    int temp = j;
    j -= 1;
    return temp; // return original value
}

// prefix operator
int operator --() {
    j -= 1;
    return j; // return new value
}

k = 4
j = 4

k = j-- // postfix operator called for j
// k = 4, j = 3

k = --j // prefix operator called for j
// k = 2, j = 2 

Ok here we go.

j = 4
k = 4

k = j--

= gets evaluated before --

k gets set to j

k = j which is 4

j gets decremented

If we change j-- to --j then this is what happens.

k = --j

j gets decremented

j = 3

k = j

k = 3

Did that help at all?
Last edited on
Topic archived. No new replies allowed.