+ 2

Why the output is "Too young"?

Why is it 18 if a comparison is x < 18 and not x <= 18? Example: int age = 18; if (!(age < 18) { System.out.println("Too young"); } else { System.out.println("Welcome"); Thank you all!

30th May 2022, 9:06 AM
Kristina Ray
Kristina Ray - avatar
6 Answers
+ 5
Rough code Int age = 18; If age==18 Print(exact) Elif age>18 Print(welcome) Else Print(too young) Try this way you don't get any error in the above code there is redundancy so it show wrong output Hope it helps 💕💕💞💞
30th May 2022, 9:21 AM
Rakshit Pahel
Rakshit Pahel - avatar
+ 5
I guess it's because you're using "!" . Your condition is if age is NOT larger than 18 then print out too young .
30th May 2022, 9:21 AM
Ali
+ 4
Kristina Ray 18 < 18 = false !false = true So output would be "Too young" 18 <= 18 = true !true = false So output would be "Welcome"
30th May 2022, 9:38 AM
A͢J
A͢J - avatar
+ 3
See the logical NOT operator before the expression ( age < 18 )? the logical NOT operator inverse the evaluation result. So here ( age < 18 ) is evaluated as true, but the logical NOT operator inverse the result (true) into (false). P.S. Please add Java in post tags for language context clarity.
30th May 2022, 9:21 AM
Ipang
+ 2
The reason is very simple. First,your condition age<18 is completely false. But when you use ! Operator the condition becomes true.This is because the compiler only knows the value of age=18. And that's the reason why you are getting 'Too young' as output.
1st Jun 2022, 2:18 AM
Rock 098
0
Remove the sign "!".ok
1st Jun 2022, 3:14 AM
Abinash Saikia
Abinash Saikia - avatar