-6

Executing the following program is producing the following result, please explain the reason why the value of sum is 120 and of price is 100.

double sum=10, price =100;
sum+=price>=100?price * 1.1 : price;
Syso(sum);
Syso(price);

The output is 120 100

MD Farog
  • 1
  • 1
  • 1
    Syso? Do you mean System.out.println? – Robin-Hoodie Oct 10 '17 at 11:59
  • 1
    (1) This reads like a homework question. (2) Syso doesn't exist. (3) Please show your own progress first. – Konrad Höffner Oct 10 '17 at 12:00
  • condition is price either equal to or greater than 100 then multiply price by 1.1.here condition is true so sum=10(old sum)+110(price after multiply) so result is sum=120 but multiply price is not assign to price so it not change and show price=100 – Mostch Romi Oct 10 '17 at 12:07

1 Answers1

0
expression ? value 1 : value 2

if expression is true, result is value 1 (first condition)

if expression is false ,result is value 2(second condition)

Example:

x = 1, x > 10 ? x : 10

expression: x > 10 => false

value 1: x value 2: 10

result is 10

demo
  • 5,376
  • 12
  • 55
  • 130
陈小叶
  • 64
  • 5