0

This is an example practice. I try to figure out, but I always get the wrong answers. Assume that the user enters 1 3 5 at the first prompt and 1.5 2.0 2.5 at the second prompt.

// Problem6
import.java import java.util.Scanner;
  class problem2{
   public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a, b, c;
    double x, y, z;
    System.out.print("Enter three integers: ");
    a = sc.nextInt();
    b = sc.nextInt();
    c = sc.nextInt();
    System.out.print("Enter three doubles: ");       
    x = sc.nextDouble(); 
    y = sc.nextDouble();
    z = sc.nextDouble();
    a = (++c)-(b++);
    x += (z-y);
    b *= b;
    y = c*z;
    c = (int)(b-x);
    System.out.println("a=" + a + ", b=" + b + ", c=" + c);
    System.out.println("x=" + x + ", y=" + y + ", z=" + z);
    System.out.println("c/a=" + c/a);
    System.out.println("c%a=" + c%a);
   }
  }

Output :

  • a=3, b=16, c=14

  • x=2.0, y=15.0, z=2.5

  • c/a =4

  • c%a = 2

    please help! I really need to Specifically I don't understand how a = 3 and b = 16. I don't understand the a = (++c) - (b++)

Community
  • 1
  • 1
intropella
  • 21
  • 5

8 Answers8

6

This

 a = (++c)-(b++);

is basically the same as

 c = c + 1; // pre-increment.
 a = c - b;
 b = b + 1; // post increment.
jpw
  • 44,361
  • 6
  • 66
  • 86
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

a = (++c)-(b++); means a equals increased c (which is 6) minus b (increase after this line) which is 3 in this line, but will be 4 in next. So a = 6-3 = 3 and b = 4 * 4 = 16.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
1
a = (++c) - (b++)

means a = (c=c+1)-b; b = b+1;

Prefix (++) means that we want change the variable first and then use it, postfix means that first we add b , and then change it's value . What is the difference between prefix and postfix operators?

Community
  • 1
  • 1
ig-melnyk
  • 2,769
  • 2
  • 25
  • 35
1

For expression a = (++c) - (b++): First, c is incremented by 1 and then used in an expression while b is used in an expression and then incremented. So a is evaluated as 6-3=3.

peterremec
  • 488
  • 1
  • 15
  • 46
1

The unary operator ++ can be put before or after a variable.

Putting it before means "increment by 1 BEFORE you evaluate this expression" and putting it after means "increment by 1 AFTER you evaluate this expression".

So if I said (psuedo code):

a = 5
print ++a

That would produce:

6

Because the incrementation happens before it's passed to print. However, if I did this:

a = 5
print a++
print a

The output would be:

5
6

As the incrementation happens after it's passed to print.

So ++c evaluates to 5+1=6 and b++ just evaluates to 3 (the initial value of b). Meaning (++c) - (b++) evaluates to 3.

splidje
  • 199
  • 1
  • 9
0

a = (++c) - (b++)

++c is a preincrement, which means you add 1 to it before doing the calculation b++ is a postincrement, so you add 1 to it after doing the calculation

So the formula means:

a equals (c plus 1) minus b. Then add 1 to b.

Barnaby Golden
  • 4,176
  • 1
  • 23
  • 28
0

As per your input, a = 1, b = 3, c=5

a = (++c)-(b++) is evaluated as:

  1. ++c, set c as 5 + 1 and return value as 6
  2. b++, return value 3 and now set b as 3 + 1
  3. set a as 6 - 3 gives 3

b *= b, here is already 4 so 4 * 4 is 16

0

Post Increment(n++) : First execute the statement then increase the value by one.

Pre Increment (++n) : First increase the value by one then execute the statement.

for example

int a = 8,i;

i= ++a + ++a + a++; // Ans: i = 9 + 10 + 10 = 20 then a = 8

all the Pre Increment will be done and then mathematical expression will be executed and at last Post Increment will be done.

a = (++c)-(b++);

so here also first "c" will be incremented then "b" will be subtracted and result will be saved in "a" and then "b" will be incremented.

Rahul Gulwani
  • 198
  • 4
  • 18