6. Selection - ENGR 101, Sec 100, Fall 10 Selection ENGR...

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

View Full Document Right Arrow Icon
ENGR 101, Sec 100, Fall 10 M. Wellman 1 Selection ENGR 101, Lecture 6: 27 Sep 10 Announcements Project 3 due Wed 9PM (29 Sep) Exam #1 Tue 5 Oct if you have a potential conflict, contact Nader Jawad ( njawad@umich.edu ) ASAP
Background image of page 1

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

View Full DocumentRight Arrow Icon
ENGR 101, Sec 100, Fall 10 M. Wellman 2 Fundamental Algorithmic Control Constructs Sequence Selection Iteration Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder( a , c ) 0 OR remainder( b , c ) 0) 6: subtract one from c 7: return c 8: end Selection Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder( a , c ) 0 OR remainder( b , c ) 0) 6: subtract one from c 7: return c 8: end Selective execution of steps based on specified condition
Background image of page 2
ENGR 101, Sec 100, Fall 10 M. Wellman 3 if Statement Allows the selective execution of a portion of your code depending on some condition. Basic if , syntax in C++: if ( condition ) statement_t expression evaluates to true or false like condition in while stmt (compound) stmt, executed if condition is true if-else Statement Can optionally add else clause to if statement. if ( condition ) statement_t else statement_f (compound) stmt, executed if condition is false
Background image of page 3

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

View Full DocumentRight Arrow Icon
ENGR 101, Sec 100, Fall 10 M. Wellman 4 else if The statement following else can also be an if . if ( condition ) statement_t else statement_f if ( condition2 ) statement_t2 else statement_f2 else if (common indenting) The statement following else can also be an if . if ( condition ) statement_t else if ( condition2 ) statement_t2 else statement_f2
Background image of page 4
ENGR 101, Sec 100, Fall 10 M. Wellman 5 Simple Example int x; cin >> x; if ( x % 2 == 0 ) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; From Project 3 int toggle (int allele) { if (allele == 0) return 1; else return 0; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
M. Wellman 6 Calculating Income Tax Tax brackets (single filers 2010) If your taxable income is over but not over Your tax is plus of the amount over $0 $8,375 0 10% $0 $8,375 $34,000 $838 15% $8,375 $34,000 $82,400 $4,681 25% $34,000 $82,400 $171,850 $16,781 28% $82,400 $171,850 $373,650 $41,827 33% $171,850 $373,650 $108,421 35% $373,650 Tax Calculation Function double taxes(double income) { double tax; if (income <= 8375.0) tax = 0.1 * income; else if (income <= 33950.0) tax = 837.5 + 0.15 * (income – 8375); else if (income <= 82250.0)
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/25/2011 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Page1 / 17

6. Selection - ENGR 101, Sec 100, Fall 10 Selection ENGR...

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

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