{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1402 22 - COMP1002/1402 Chapter22:OddsandEnds IfElse...

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

View Full Document Right Arrow Icon
3/28/2010 1 COMP 1002/1402 Introduction to Systems Programming Chapter 22: Odds and Ends Conditional Expressions if (expression) statement1 else statement2 If Else: Al expression ? expression1 : expression2 ; COMP1002/1402 - Chapter 22: Odds and Ends 2 Alternate two way logic:
Image of page 1

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

View Full Document Right Arrow Icon
3/28/2010 2 Conditional Expression Example If else method: Conditional expression: If(a<10) a++; else a--; a<10 ? a++ : a--; COMP1002/1402 - Chapter 22: Odds and Ends 3 Conditional Expressions Although you can nest conditional i it i NOT d d expressions, it is NOT recommended. A good alternative to one if else statement (often shortens code). COMP1002/1402 - Chapter 22: Odds and Ends 4
Image of page 2
3/28/2010 3 Conditional Expression Example 2 Suppose you have a flag to decide if your program will output to a file or to the monitor (called fileFlag ) ). When printing to the monitor, you want to output 20 numbers per line. When printing to a file, you want to output 30 numbers per line. A i bl P Li ill i h A variable numPerLine will contain how many numbers to print or output per line. COMP1002/1402 - Chapter 22: Odds and Ends 5 Conditional Expression Example 2 If else solution: if( fil Fl ‘M’) fileFlag == ‘M’) numPerLine = 20; else
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.