-2

This code was in a quiz of Dart course that I'm taking, please help me solve it. I want to know what it should display. I solved it as 6, but the answer was 1, but I don't know why.

    int var1 = 5;
    int var2 = 6;
    if ((var2 = 1) == var1)
    print(var2);
    else
    print(var2++);
Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45

1 Answers1

0

In the if query, a new value is assigned to the var2 variable, so var2 is not equal to var1 with both its old value and its new value.

Then, when var2 value is wanted to be printed, there is var2++, which means use the var2 value as it is, then increase it. If you want to see this add a print(var2) after the last print and see the output is 2

rasityilmaz
  • 764
  • 1
  • 6
  • 11