cs246-s11-001-may19

cs246-s11-001-may19 - CS 246(Spring 2011 Section 1 Thursday...

This preview shows pages 1–7. Sign up to view the full content.

CS 246 (Spring 2011) – Section 1 Thursday 19 May Structured Programming

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

View Full Document
CS 246 (Spring 2011) - Section 001 - Thursday 19 May 2 Today's Agenda 2.4.1 / 2.4.2 Conversion and Coercion Some wording changes in the course notes 2.5 Control Structures Mostly review Will see some new examples 2.6 Structured Programming
CS 246 (Spring 2011) - Section 001 - Thursday 19 May 3 2.4.1 Conversion Conversion : Transforming a value having type X to a value having type Y Two types: widening and narrowing Widening/promotion : no loss of information bool → char → short int → int → double Narrowing : possible information loss double → int → short int → char → bool 77777.7. ..7 → 77777 → 12241 → 209 → 1 (true)

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

View Full Document
CS 246 (Spring 2011) - Section 001 - Thursday 19 May 4 Conversion (ctd.) Widening or narrowing can be implicit Implicit narrowing can be problematic int i; double r; i = r = 3.5; r = i = 3.5; Narrowing should be done explicitly ( casting ) int i; double x = 7.2; double y = 3.5; i = (int) x; i = (int) x / (int) y; i = static_cast<int>(x / y);
CS 246 (Spring 2011) - Section 001 - Thursday 19 May 5 2.4.2 Coercion Coercion : Transforming a value having type X to a value having type Y, where the result is not meaningful in the new representation char 'a' → true long int 77777 → short int 12241 char ch = 'z' - 'a'; Two interpretations of char Coercion with pointers To be covered later

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

View Full Document
CS 246 (Spring 2011) - Section 001 - Thursday 19 May 6 2.5 Control Structures This section is mostly review from CS 136 Two examples illustrating pitfalls Exiting a loop The switch statement Select alternatives for an integral value Example Additional syntax is introduced in the next section on structured programming e.g. goto statements
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 21

cs246-s11-001-may19 - CS 246(Spring 2011 Section 1 Thursday...

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

View Full Document
Ask a homework question - tutors are online