Home > Back-end >  I don't understand the logic of code that is given below
I don't understand the logic of code that is given below

Time:06-22

I have run this code in compiler and getting the output as 1 but I don't understand how it will be 1. Please explain with answer.

public class Main {
    public static void main(String args[]) {
        int a = 10;
        int b = 2;
        System.out.println((a < b) ? a   : --b);
    }
}

CodePudding user response:

The ternary operator ?: will check the condition (a < b), since it is false, it will execute the expression after :, which is --b.

--b will subtract b by 1, and return the value after subtraction, which is 1.

CodePudding user response:

The ternary operator is equivalent to a function that returns a value based on a condition. So let's write that function:

int ternary(int a, int b) {
  if(a < b) {
    int temp = a;
    temp = temp   1;
    return a;
  } else {
    b = b - 1;
    return b;
  }
}

CodePudding user response:

    int a = 10;
    int b = 2;
    System.out.println((a < b) ? a   : --b);

In the above code (a < b) is checking if a is less than b and if its true a will be executed, and if it is false (which is the case in this situation) --b will be executed, and as the value of b is initialized as 2 it will equal to 1 after doing --b operation

because as per the turnery operator in java

Expression1 ? Expression2: Expression3;

If Expression1 is evaluated at true, Expression2 will be executed otherwise Expression3

CodePudding user response:

There are several great answers already. This took a bit to edit for formatting, but hopefully useful to illustrate what's happening.

The interesting part of your program is in the println() statement:

System.out.println((a < b) ? a   : --b)
                   ^^^^^^^^^^^^^^^^^^^

The whole thing inside the () is an expression with several things going on. At a high level, there is a boolean condition (a < b) followed by two other expressions: first is a , second is --b. Below is a breakdown.

(a < b) ? a   : --b
 -----    ---   ---
   ^       ^     ^ 
   |       |     |__ second expression, used if condition is "false"
   |       |
   |       |__ first expression, used if condition is "true"
   |
   |__ this is the boolean condition to check; whatever is inside must return either true or false

This is how the entire expression is evaluated:

  1. Is the boolean condtion true or false? To answer that, we need to know: what does a < b evaluate to? Since a = 10, and b = 2, we can substitute the values (10 for a, 2 for b). So the question becomes: what does 10 < 2 evaluate to? Since 10 is not less than 2, the result of the boolean condition is false.

  2. Which of the of two expressions should we go to? Since the result from #1 was false, we skip the first expression (a ), and go to the second one (--b).

  3. What is --b? That's a fancy way of saying: b = b - 1 and also use the new value of b as the result of the expression. Since b = 2, it's equivalent to: b = 2 - 1, and use the new b value (1) as the result of the expression. So this expression evaluates to "1", and that's what is printed out. This part is tricky though. A variation would be b-- instead of --b – if you did that, it would mean: set the new value of b to the result of "2 - 1" (so, new value is "1" like with --b), but: for the expression result, use the old value. So --b ends up printing "1" in your example, and b-- would end up printing "2".

  • Related