Computer Science, asked by aspara, 1 month ago

What is ternary operator? How can it be used instead of if-else statement? Show it with the help of an example.​

Answers

Answered by kajalpal1975
4

Answer:

Ternary operator is a shorthand form of the if else statement. It shortens the program of if else to a single line of program but sometimes it is challenging to understand.

Syntax:- condition? statement1: statement2;

If condition is true statement1 will be evaluated else statement2 will. e evaluated.

For example, to find max between two no.s

if (a > b) {

max = a;

} else {

max = b;

}

can be converted to

max = a > b? a: b;

Another example, to find to sign of a number

if (n > 0) {

sign = 1;

} else if (n < 0) {

sign = -1;

} else {

sign = 0;

}

can be converted to

sign = n>0? 1: (n<0? -1: 0);

Please mark me as the brainliest.

Similar questions