Thread: Java help
View Single Post
Old 09-20-05, 10:14 PM   #6 (permalink)
JonMisurda
Guest
 
Posts: n/a
*puts on java instructor hat* You should check with your instructor to make sure outside help like this is appropriate *takes hat off*

Now, as was said before n-=3 is an instance of what's called an "assignment operator"

expressions of the form:

variable = variable operator expression;

can be rewritten in a shortcut form as:

variable operator= expression;

One thing to remember is that precedence rules may seem non-intuitive. The expression is done before the variable operator part. An example:

a = a * 4 - b;

written in this way, a * 4 is done first, then b is subtracted. You may be tempted to rewrite that as:

a *= 4-b;

however this is not the same. This is actually equivalent to:

a = a * (4 - b);

whose value may be considerably different.

the System.out.print(n+10) is doing addition, but not modifying the value of n. The only time a variable is modified is when it is the left-hand side of an assignment (to the left of the =) or when ++ or -- is used on it (if you've covered pre- and post-increment/decrement yet.)

You're not taking into account the addition when doing your output, so that column is entirely incorrect. Also you have an ordering problem, the n-=3 is done after the print, so make sure you have the correct value at the correct place. Remember the program is done in sequential order unless there is a control structure, usually indicated by the curly brace.

Also you need to be careful, in the code you posted the condition on the loop is strictly greater than, but in your spreadsheet you say greater than or equal to, which is it?

To double check your answers, why not just make a program on your own and see what happens. It will be good practice.

Jon

Last edited by JonMisurda; 09-20-05 at 10:17 PM.
  Reply With Quote