assn2 - MIT OpenCourseWare http/ocw.mit.edu 6.096...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.096: Introduction to C++ IAP 2009 PROBLEM SET 2 Ternary operator: The conditional operator ? : is also another way to evaluate conditions. It operates on three operands, and is thus known as a ternary operator. Here is an example of its usage: a = x < y ? x : y; In this example, if the test expression evaluates to true – i.e. if x < y – then the variable a will be assigned the value that the variable x contains. Otherwise, else it will be assigned the value of the variable y . The whole expression containing this operator (the right side of the assignment statement) is called a conditional expression , and the sub-expression before the question mark is called the test expression . If this test expression is true, the entire expression takes on the value of the expression immediately after the question mark. Otherwise, it takes on the value of the expression following the colon. Usually you only want to use this construct for choosing between two simple expressions for a particular value (both choices must be the same data type). Doing otherwise could run afoul of the syntactic rules of the operator and lead to syntax errors. You cannot, for instance, make the two options two different return statements. 1. Without running, explain what the following set of statements will do:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course DDDD h0322 taught by Professor Whoknow during the Spring '11 term at Bina Nusantara University.

Page1 / 5

assn2 - MIT OpenCourseWare http/ocw.mit.edu 6.096...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online