4.13: The Conditional Operator

Ace your homework & exams now with Quizwiz!

Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is "freshman" or "sophomore" or "junior" or "senior" based on the value of credits. In particular: if the value of credits is less than 30 the expression 's value is "freshman"; 30-59 would be a "sophomore", 60-89 would be "junior" and 90 or more would be a "senior".

((credits < 30)?"freshman": ((credits < 60 && credits > 29)?"sophomore": ((credits < 90 && credits > 59)?"junior": ((credits > 89)?"senior": "invalid"))))

Assume that day is an int variable whose value is 1 or 2 or ... or 7. Write an expression whose value is "sun" or "mon" or ... or "sat" based on the value of day. (So, if the value of day were 4 then the value of the expression would be "wed".).

((day==1)?"sun": ((day==2)?"mon": ((day==3)?"tue": ((day==4)?"wed": ((day==5)?"thu": ((day==6)?"fri": ((day==7)?"sat": "invalid")))))))

Assume that month is an int variable whose value is 1 or 2 or 3 or 5 ... or 11 or 12. Write an expression whose value is "jan" or "feb or "mar" or "apr" or "may" or "jun" or "jul" or "aug" or "sep" or "oct" or "nov" or "dec" based on the value of month. (So, if the value of month were 4 then the value of the expression would be "apr".).

((month==1)?"jan": ((month==2)?"feb": ((month==3)?"mar": ((month==4)?"apr": ((month==5)?"may": ((month==6)?"jun": ((month==7)?"jul": ((month==8)?"aug": ((month==9)?"sep": ((month==10)?"oct": ((month==11)?"nov": ((month==12)?"dec": "invalid"))))))))))))

Write an expression using the conditional operator (? :) that compares the values of the variables x and y and results in the larger of the two.

x > y ? x : y

Write an expression using the conditional operator (? :) that compares the value of the variable x to 5 and results in: x if x is greater than or equal to 5 -x if x is less than 5

x>=5 ? x : -x


Related study sets

Varcarolis Chapter 20: Crisis and Mass Disaster practice questions

View Set

Management Consulting Midterm Exam

View Set

¿cuantos hay? (How many are there?)

View Set

SES 141 Module 2 Assignment/Labs

View Set

Ereditatea si variabilitatea lumii vii

View Set