1

I start programming about one month ago, and i have some difficulties with the operators ++a, a++ in java. Can someone explain me line by line this program?

import java.util.Scanner; 
public class Number { 
 public static void main(String[] args) { 
 Scanner keyboard = new Scanner(System.in); 
 int number = keyboard.nextInt(); 
 int division1 = (number++) % 10; 
 number = number / 10; 
 System.out.println(number % 10+division1); 
 } 
} 

5 Answers5

14
int division1 = (number++) % 10;

Is equivalent to :

int division1 = number % 10; 
number += 1;

While

int division1 = (++number) % 10;

Would be equivalent to :

number += 1;
int division1 = number % 10; 

Basically, your code is equivalent to :

int number = new java.util.Scanner(System.in).nextInt(); 
System.out.println( ((number + 1) / 10) % 10 + number % 10 ); 
zakinster
  • 10,508
  • 1
  • 41
  • 52
3

++a is a pre-incrementation. Which means that a is incremented before returning the value of a.

a++ is a post-incrementation. Which means that a is incremented after returning the value of a.

In other words, a++ gives the current value of a and then increment it. While ++a directly increment a. If a=42 then System.out.println(a++) gives 42 while System.out.println(++a) gives 43 and in both cases, a=43 now.

OP also asked for a line by line explanation of that code:

import java.util.Scanner;

public class Number { 
 public static void main(String[] args) { 
   Scanner keyboard = new Scanner(System.in); 
   int number = keyboard.nextInt(); 
   int division1 = (number++) % 10; 
   number = number / 10; 
   System.out.println(number % 10+division1); 
 } 
}

I guess, only the code inside the main function need some explanations :

   // Create a Scanner object that read from the standard input.
   Scanner keyboard = new Scanner(System.in);

   // Read an integer.
   int number = keyboard.nextInt(); 

   // put (number % 10) into variable division1 and then increment `number`.
   int division1 = (number++) % 10;

   // Divide number by 10.
   number = number / 10; 

   // Print that expression :
   System.out.println(number % 10+division1);

The line int division1 = (number++) % 10; might not be very clear. It would be simpler to read like that:

int division1 = number % 10;
number += 1;

Now, the explanation of what the function does:

If number = 142, we put 2 into variable division1, then number is incremented and divided by 10. So number gets the value 14 ((142+1) / 10). And now we print number % 10 + division1 which is 4 + 2 = 6.

Here some examples of results (I've compiled the code myself):

3 => 3
9 => 10
10 => 1
248 => 12
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • "a++ is a post-incrementation. Which means that a is incremented after the expression is evaluated." -> This is not true, or confusing. a is incremented right after the value of (a++) is returned. try to run this : "System.out.println(a++ + " " + a++);" to see what I mean. – Kaidjin Oct 16 '13 at 15:46
  • @Kaidjin that's a good point. However, an expression such as `a++ + a++` has an undefined behavior. And regarding your particular example, I could also argue that it's not really an expression but 2 expressions, 2 conversions and 2 concatenations. No one should write expressions with several identical variables with one or more of them that use a post increment. – Maxime Chéramy Oct 16 '13 at 16:02
  • Yeah, an expression is often composed of several expressions and one or more operators. That's why your sentence is probably not wrong, merely not very clear. I'm curious of why this expression has an undefined behaviour though. Care to elaborate ? – Kaidjin Oct 17 '13 at 07:45
  • @Kaidjin Have a look to that answer: http://stackoverflow.com/a/4176333/1787973 (in particular the part about the sequence points) – Maxime Chéramy Oct 17 '13 at 12:25
  • And the answers of http://stackoverflow.com/questions/5433852/when-does-postincrement-i-get-executed – Maxime Chéramy Oct 17 '13 at 12:28
  • This is very interesting, thanks. However, i'm not sure this applies here: I'm not familiar with the concept of sequence points but it seems to me that it's only useful because the order of evaluation of operands is not specified in the standard. In java however, the standard guarantees that operands are evaluated left to right (see http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7). In particular, example 15.7.1.1 is pretty much the same thing as what I showed in my first comment. – Kaidjin Oct 17 '13 at 12:54
  • Indeed, Java is maybe more specified for that kind of stuff. – Maxime Chéramy Oct 17 '13 at 13:29
1

Literally line-by-line explanation:

import java.util.Scanner; // Import Scanner
public class Number {     // Create Java class called Number
    public static void main(String[] args) {  // Define the main function
        Scanner keyboard = new Scanner(System.in); // Initialize the scanner
        int number = keyboard.nextInt();   // Read an integer from standard input
        int division1 = (number++) % 10;   // Take the unit digit of number, then increase number by 1
        number = number / 10; // Divide number by 10
        // Take the tens digit of the original number (or tens digit + 1, if the unit was 9), sum with the unit digit previously stored in division1
        System.out.println(number % 10+division1);
        // The result would sum the last two digit of number if it doesn't end with 9, or sum the last two digit +1 if the unit was 9.
    } 
}

Some cases:

f(1) = 1
f(124) = 2+4 = 6
f(39) = 4+9 = 13
f(5248) = 4+8 = 12
justhalf
  • 8,960
  • 3
  • 47
  • 74
0

++a is a pre-incrementation. Which means that a is incremented before the expression is evaluated.

a++ is a post-incrementation. Which means that a is incremented after the expression is evaluated.

One thing where beginners might be confused is what is this 'expression' after which the increment takes place It is actually the ; When you use a++, it is after the ";" when the increment actually takes place and when you use ++a; it is just as you inserted a statement before this one saying a=a+1 and then your expression comes.

The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35
0

I think, the interesting part for you are the following lines:

int number = keyboard.nextInt(); 

Here, you get an input from the user. Say, he enters 12

int division1 = (number++) % 10; 

division1 will become 2, because 12 mod 10 equals 2. After this, number will be incremented by 1, so it is now 13

Let's now assume there would be a ++number instead of number++. In this case, we would increment number before the modulo operation. So, first, number becomes 13, then we assign 3 to division1.

And that's the basic thing about a++ and ++a - the time, when the value is incremented. a++ means, all operations use a as it is and as a last step, a is incremented. ++a works in the other order.

LostAvatar
  • 795
  • 7
  • 20