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); } } 
1
  • 4
    Line by line? Including the import? Commented Oct 10, 2013 at 9:50

5 Answers 5

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 ); 
Sign up to request clarification or add additional context in comments.

Comments

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 

7 Comments

"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 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.
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 Have a look to that answer: stackoverflow.com/a/4176333/1787973 (in particular the part about the sequence points)
|
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 

2 Comments

Your last comment is wrong. It's the sum. And your examples of results are also wrong.
Oops, just realized that, fixing it
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.

Comments

-1

++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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.