0
public static void main(String args[]){

    Scanner s = new Scanner(System.in);
    int x;
    int y;
    int z = 0;
    x = s.nextInt();
    y = s.nextInt();

    while(y != 0){
        z += x;
        y--;
    }
    System.out.println(z);
}

This is all. This code calculates x*y but doesn't use *. It was just a (task ?) somebody told me. And my question is why this is working with negative numbers. That x can be negative is obvious but why can y be.

Edit: I wrote this code on my own so I know why it works without *. That's not the question. I can input 5 for x and -5 for y and i get -25. But why? Everytime he adds one more time x to z y goes 1 down. But after the 5th time it stops.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Konschi01
  • 23
  • 3
  • 5
    flash news: `2*3 = 2+2+2` ;) – Nir Alfasi Aug 25 '17 at 14:55
  • cuz `x*y` -> `x times y`; like 2*3 === 2+2+2 – DanilGholtsman Aug 25 '17 at 14:55
  • it would greatly improve you're question if you told us what imputs you are using for x, y and z. It would greatly improve your code if you gave your variables meaningful names – Austin_Anderson Aug 25 '17 at 14:56
  • 1
    It wraps around. That's why it takes so long for a negative y. – azurefrog Aug 25 '17 at 14:57
  • Re: edit. Follow Alejandro's suggestion and move the print inside the while loop, and you'll see it's not stopping after 5 times through the loop. Instead, `z` will overflow and `y` will underflow and a few million loops later they'll end up at the correct value. – azurefrog Aug 25 '17 at 15:02
  • Possible duplicate of [How does Java handle integer underflows and overflows and how would you check for it?](https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo) – azurefrog Aug 25 '17 at 15:06

2 Answers2

1

If you put the "z" print inside the while you will understand.

This code makes overflow. Reach the maximum value that the int permit and decrease again from there

  • I did it. I see what you mean. But why does my loop stops at the right answer? Y never reachs 0. – Konschi01 Aug 25 '17 at 15:03
  • @Konschi01 `y` will eventually reach `0`, but the long way around. Once it gets to the minimum possible value, it'll underflow back up to max int, and keep decrementing until it reaches `0`. – azurefrog Aug 25 '17 at 15:05
  • See https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo – azurefrog Aug 25 '17 at 15:06
  • I think i know now what u meant. y and z become so great that they reset? I'm new to coding so i didn't understood overflow. – Konschi01 Aug 25 '17 at 15:07
  • @Konschi01 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html here you can see the int possibles values, when an int reach the maximun and you add one more, the value is the min. For example. Imagine the int posible values are between {-10, 9}. if your int value is 9, and you add 1, the value after the operation is -10, and if you decrement this value in one unit it will be 9 again. Do you understand? – Alejandroppir Aug 25 '17 at 15:24
0

This code makes multiple sums. So it can sum the number 2, 3 times, and thats how it does 2*3.

The y can be negative because the if is seeing != than 0, not < than 0.

David Dutra
  • 391
  • 7
  • 21