7_pdfsam_lecture2 - Conditional expressions I The following...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Conditional expressions I The following form of if statement is very common: if(condition) ss b = expr1; else ss b = expr2; I C provides a shortcut for this kind of if statement: b = condition ? expr1 : expr 2; I Conditionals can be nested, e.g. grade = (score > 90) ? `A' : ((score > 80) ? `B' : `C'); I For clarity, it's generally best to avoid conditionals. switch statements I Another common form of if statements: I if(a == b) statement1; else if(a == c) statement2; . . . else statement0; I C provides a shortcut for this kind of if statement: I switch(a) { case b: statement1; break; case c: statement2; break; . . . default: statement0; break; } I Switch statements can be more e cient I But sometimes harder to read. Use your own judgment! More on switch statements I switch statements have a fall-through property I Execution continues until a break statement is encountered I E.g., what does this code do?...
View Full Document

Page1 / 6

7_pdfsam_lecture2 - Conditional expressions I The following...

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

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