Computer Applications
Choose the correct equivalent of the given if-else statement:
if(x % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
Java Conditional Stmts
3 Likes
Answer
System.out.println(x % 2 == 0 ? "Even" : "Odd");
Reason — The ternary operator must be placed inside System.out.println() for proper execution. The ternary operator condition ? value_if_true : value_if_false is used as a shorthand for if-else.
Here, x % 2 == 0 checks if x is even. If true, "Even" is printed; otherwise, "Odd" is printed. Hence, this single-line statement is equivalent to the given if-else block.
Answered By
1 Like
Related Questions
Using the switch-case statement, write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode
Letters Unicode A 65 B 66 . . . . . . Z 90 (b) Display the following pattern using iteration (looping) statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5The statement that brings the control back to the calling method is:
- break
- System.exit(0)
- continue
- return
Differentiate between if else if and switch-case statements