163k views
0 votes
The ____ operator executes one of two expressions based on the results of a conditional expression. Group of answer choices

2 Answers

4 votes

Answer:

Conditional operator ?:

Step-by-step explanation:

  • The conditional operator evaluates a condition and chooses one of the two expressions which depends on whether the condition evaluated to true or false.
  • The syntax of conditional statement is given below:

conditional_expression ? expression1 : expression2;

  • Basically this operator takes three operands/expressions and if the first expression ( a conditional expression) evaluates to true then this means that the second expression is evaluated/executed and if the first expression evaluates to false then the third expression is evaluated.
  • Example:

int number = 16;

number%2 == 0 ? cout<<"even number" : cout<<"odd number";

  • This statement first checks the conditional expression number%2==0 is true or false.
  • Value of number variable is 16 and here % is the modulo operator which calculates the remainder after dividing one number by another.
  • So 16%2==0 because 16 is divisible by 2 and gives 8 as quotient and 0 as remainder.
  • So the conditional statement is true it means that the first expression will be executed.
  • Hence the output will be: even number
User Qumber
by
4.8k points
4 votes

Corrected or Complet Question

The ____ operator executes one of two expressions based on the results of a conditional expression.

a. .

b. ( )

c. ,

d. ?:

Answer:

(d) ? :

Step-by-step explanation:

The ternary operator (? :) is an operator that executes one out of two expressions based on the results of a conditional expression. It is a faster or shorthand way of writing an if ... else statement. The operator checks the conditional expression and depending on whether it is true or false, one of its two statements will be executed. For example:

String correct = (4 < 5) ? "yes" : "no" ;

In the code above,

i. the conditional statement is (4 < 5) and this will be tested for to return true or false.

ii. the first expression (the one after the ?) is "yes" which will be executed if the conditional statement returns true.

iii. and the second expression (the one after the : ) is "no" which will be executed if the conditional statement returns false.

Therefore, since the conditional expression returns true for 4 < 5, the first expression will be executed. i.e "yes". This means that the String variable correct will have a value of "yes".

User Aquanetta
by
3.4k points