Math, asked by abcdef123416, 7 months ago

rewrite the following using ternary operator
int age=20;
boolean res;
if(age>=18)
res=true;
else
res=false​

Answers

Answered by TanikaWaddle
4

Answer:

   res = age>=18?true:false;

Step-by-step explanation:

Let us learn about ternary operator first.

A ternary operator consists of 3 parts:

1. Condition: First of all, a condition is checked whether it is TRUE or FALSE according to which the further execution will take place.

2. Result on True condition: The 2nd part is the result when the condition written earlier is TRUE.

3. Result on False condition: The 3rd part is the result when the condition written earlier is FALSE.

Now, let us consider our answer:

   res = age>=18?true:false;

1. The first part here, is the condition i.e. (age>=18) which was written in the if() part earlier.

2. After that there is a '?' following which there is the result on condition true.

i.e. Assignment of true to variable res.

3. After that there is a ':' following which there is the result on condition false.

i.e. Assignment of false to variable res.

The executable c++ file is hereby attached in the answer area.

Attachments:
Similar questions