Вопрос

I was going through some exercises but I am confused in this one:

public static int f (int x, int y) {
  int b=y--;
  while (b>0) {
    if (x%2!=0)  {
      --x;
      y=y-2; 
    }
    else { 
      x=x/2;
      b=b-x-1; 
    }
  }
  return x+y; 
} 

What is the purpose of b=y--? So, for example, x=5 and y=5 when we first go inside of while loop (while (b>0)) will b = 4 or 5? When I am running the code in my computer b is 5. And the return is 3. It is really unclear to me. Sorry if I am unclear in my question.

Это было полезно?

Решение

int b=y--; first assignes b=y and then decrements y (y--).

Also take a look at the prefix/postfix unary increment operator.

This example (taken from the linked page) demonstrates it:

class PrePostDemo {
    public static void main(String[] args){
        int i = 3;
        i++;
        // prints 4
        System.out.println(i);
        ++i;               
        // prints 5
        System.out.println(i);
        // prints 6
        System.out.println(++i);
        // prints 6
        System.out.println(i++);
        // prints 7
        System.out.println(i);
    }
}

Другие советы

The difference between a post-increment/decrement and a pre-increment/decrement is in the evaluation of the expression.

The pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. In contrast, the post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation.

In other words:

int a = 5;
int b;
b = --a;    // the value of the expression --a is a-1. b is now 4, as is a.
b = a--;    // the value of the expression a-- is a. b is still 4, but a is 3.

Remember that a program must evaluate expressions to do everything. Everything is an expression, even just a casual mention of a variable. All of the following are expressions:

  • a
  • a-1
  • --a && ++a
  • System.out.println(a)

Of course, in the evaluation of expressions, operator precedence dictates the value of an expression just as the PEMDAS you learned in grade school. Some operators, such as increment/decrement, have side effects, which is of course great fun, and one of the reasons why functional programming was created.

I believe b would equal 5 entering the loop because

b=y--;

When the "--" is behind the variable it decrements it after the action.

It's poor coding, as it can confuse new programmers.

The function, assuming it is passing by value, like in the example above (as opposed to passing by reference) takes a copy of y, decrements it, and assigns it to b. It does not alter the argument passed to the function when it was called.

Post increment

x++; 
x += 1; 

Post decrement

x--; 
x -=1; 

Pre increment : ++x;
Pre decrement : --x;

According to the Head First Java:

Difference between x++ and ++x :

int x = 0; int z = ++x; 
Produces: x is 1, x is 1
in x = 0; int z = x++; 
Produces: x is 1, z is 0
public class Solution{
public static void main(String[] args){
int a = 10;
a = a + 1;
int b = a;`
System.out.println(b) // this will print 11.
System.out.println(b) // this will print 12.

// pre increment adds the previous value and add by 1. In this chase the previous value is 12, so the pre incremented value is 13. // to be honest I don't see the difference. I think it depends on how the nature of how you use it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top