Need help with mock exam question

• 04-11-2009
Need help with mock exam question
Hi,

I have an exam coming up and I need to complete this mock exam. If i post the code, can anyone tell me if my understanding is correct and where to look at next:

What will this code output:
Code:

```int i=1, j=1 for (i=0; i>0; i--) {       j += i*(i-1); }; printf("%d",j+1);```
What I understand is this. First i and j are defined as intergers, 1 value for both. Then there is a for loop, with the starting condition as i=0, theloop continues while i is more than zero and 1 is taken away from i each loop.

The problem I have up to this point is initially i is set as 1, but then in the loop x is set at 0? Also what does += mean?
• 04-11-2009
MK27
This is a trick question; the loop won't iterate so the answer is 2.

Code:

`i=0; i>0;  // hmmm`
• 04-11-2009
Hi,

Thank you for the help. Im going to need to know how to answer these questions though so i still need a little help getting the answer.

It seems like i is defined twice with two different values (0 and 1). Is this correct? If so, what does this mean? Which value is followed?

Also does += mean more than or equal to? Much like >=.
• 04-11-2009
ಠ_ಠ
x+=y means x = x + y
• 04-11-2009
King Mir
x += y is the same as x = x + y, except that x is evaluated only once. This matters if x is an operation with side effects, like "++ptr".
• 04-11-2009
MK27
I went completely through one of these the other day with transgalactic2 and at the very end, after I manage to demonstrate that the 60-70 of obfuscationism by his prof didn't do anything at all, s/he tells me the real question now is "what is the return value of what2"?

So in the Original Post,
Quote:

What will this code output:
This code will output "2". Now:
Quote:

It seems like i is defined twice with two different values (0 and 1). Is this correct? If so, what does this mean? Which value is followed?
This code does nothing. This code does not mean anything. You will never, ever, ever, ever, ever see this code in a real program. Ever. So I don't know what you are suppose to do with it, analysis wise, but there is no point in asking "what does this mean".

But that is correct, i is defined twice [no, I take that back, see below]. The first definition is superfluous, but part of the declaration, which is necessary.
• 04-11-2009
King Mir
Quote:

It seems like i is defined twice with two different values (0 and 1). Is this correct? If so, what does this mean? Which value is followed?

No, look again. i is define once. The second time it is simply assigned a new value. Redefining i would require "int i=0". This is valid only in C99.
• 04-11-2009
MK27
I assent to King Mir on this one. Unless you want to distinguish between redefinition (i=) and redeclaration (int i=).
• 04-11-2009
Hi guys, thank you so much for the help. So that I understand this, i is being declared at the top and assigned the value 1. Below in the code it is then reassigned (but not redeclared) a different value, in this case 0.

So it would make no difference if the code at the top was instead:
Code:

```int i int j=1```
Therefore not giving i a value, since it will be set a value later on in the loop.

To check my understanding of the next part, if x=1 and y=2, would the code:
Code:

`x += y`
Mean that the new value of X would be 3?
(3[x] = 1[x] + 2[y])

Finally printf("%d",j+1); would just simply add one to the value of j and then print its value?
• 04-11-2009
MK27
Quote:

Mean that the new value of X would be 3?

Finally printf("%d",j+1); would just simply add one to the value of j and then print its value?

1) Yes, "+=" adds the right side to the left side.
2) Yes.