{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

6. Selection

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

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

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 ( ) ASAP

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

View Full Document
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
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

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

View Full Document
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
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; }

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

View Full Document
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)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 17

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

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

View Full Document
Ask a homework question - tutors are online