DOC PREVIEW
WUSTL CSE 131 - sp14_2

This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1A Foundation for ProgrammingA Foundation for ProgrammingSlide 4If StatementIf StatementIf StatementIf Statement ExamplesSlide 9While Loop: Powers of TwoWhile LoopPowers of TwoWhile Loop ChallengeWhile Loop ChallengeSlide 15For LoopsAnatomy of a For LoopWhich to use, while or for loops?Loop ExamplesSlide 20Nested If StatementsNested If StatementsNested If StatementsNested If Statement ChallengeSlide 25Gambler's RuinDigression: Simulation and AnalysisGambler's RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler’s RuinGambler's Ruin1.3 Conditionals and LoopsIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/14/19 03:29:18 PM2A Foundation for Programmingobjectsfunctions and modulesgraphics, sound, and image I/Oarraysconditionals and loopsMath text I/Oassignment statementsprimitive data typeslast lecture:equivalentto a calculatorany program you might want to write3A Foundation for Programmingobjectsfunctions and modulesgraphics, sound, and image I/Oarraysany program you might want to writeto infinityand beyond!conditionals and loopsMath text I/Oassignment statementsprimitive data typesConditionals5If StatementThe if statement. A common branching structure.Evaluate a boolean expression.If true, execute some statements.If false, execute other statements.if (boolean expression) { statement T;}else { statement F;}can be any sequenceof statementsstatement Ttruefalseboolean expressionstatement FThe else{} part is optional6If StatementThe if statement. A common branching structure.Evaluate a boolean expression.If true, execute some statements.If false, execute other statements.7If StatementEx. Take different action depending on value of variable. public class Flip { public static void main(String[] args) { if (Math.random() < 0.5) System.out.println("Heads"); else System.out.println("Tails"); }}8If Statement Examples9The While Loop10While Loop: Powers of TwoEx. Print powers of 2 that are  2N.int i = 0;int v = 1;while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v;} N=6:0 11 22 43 84 165 326 6411While LoopThe while loop. A common repetition structure.Evaluate a boolean expression.If true, execute some statements.Repeat.while (boolean expression) { statement 1; statement 2;}statement 1truefalseboolean expressionstatement 2loop bodyloop continuation condition12Powers of Twopublic class PowersOfTwo { public static void main(String[] args) { // last power of two to print int N =6; int i = 0; // loop control counter int v = 1; // current power of two while (i <= N) { System.out.println(i + " " + v); i = i + 1; v = 2 * v; } }}13While Loop ChallengeQ. Anything wrong with the following code for printing powers of 2?int i = 0;int v = 1;while (i <= N) System.out.println(i + " " + v); i = i + 1; v = 2 * v;14While Loop ChallengeQ. Anything wrong with the following code for printing powers of 2?A. Need curly braces around statements in while loop;otherwise it enters an infinite loop, printing "0 1".Moment of panic. How to stop infinite loop?int i = 0;int v = 1;while (i <= N) System.out.println(i + " " + v); i = i + 1; v = 2 * v;When your program is running, eclipse shows a red square button. Pushing it will terminate your application, even if it is running in an infinite loop.The while loop affects only this statement, even though the indentation makes us think otherwise.15The For LoopCopyright 2004, FoxTrot by Bill Amendwww.ucomics.com/foxtrot/2003/10/0316For LoopsThe for loop. Another common repetition structure.Execute initialization statement.Evaluate a boolean expression.If true, execute some statements; otherwise, exit the loopAnd then the increment statement.Repeat.for (init; boolean expression; increment) { statement 1; statement 2;}statement 1truefalseboolean expressionstatement 2initincrementbodyloop continuation condition17Anatomy of a For Loopshorthand for i = i +1Which to use, while or for loops?18The “for” looop packages the initialization, termination, and iteration of i all in one statement. The “while” loop spreads this around and makes the gesture harder to see.19Loop ExamplesShorthand for sum = sum + iShorthand forproduct = product * iIt’s bad practice and bad style to leave out the curly braces.When you use elipse, type the opening one and it will complete the block for you.20Nesting21Nested If StatementsEx. Pay a certain tax rate depending on income level.0 - 47,450 22%Income Rate47,450 – 114,650 25%114,650 – 174,700 28%174,700 – 311,950 33%311,950 - 35%5 mutually exclusivealternatives22Nested If StatementsUse nested if statements to handle multiple alternatives.if (income < 47450) rate = 0.22;else { if (income < 114650) rate = 0.25; else { if (income < 174700) rate = 0.28; else { if (income < 311950) rate = 0.33; else rate = 0.35; } }}23Nested If StatementsEx. Pay a certain tax rate depending on income level.double rate;if (income < 47450) rate = 0.22;else if (income < 114650) rate = 0.25;else if (income < 174700) rate = 0.28;else if (income < 311950) rate = 0.33;else rate = 0.35;0 - 47,450 22%Income Rate47,450 – 114,650 25%114,650 – 174,700 28%174,700 – 311,950 33%311,950 - 35%24Nested If Statement ChallengeQ. What's wrong with the following for income tax calculation?double rate = 0.35;if (income < 47450) rate = 0.22;if (income < 114650) rate = 0.25;if (income < 174700) rate = 0.28;if (income < 311950) rate = 0.33;0 - 47,450 22%Income Rate47,450 – 114,650 25%114,650 – 174,700 28%174,700 – 311,950 33%311,950 - 35%Do you see a clever way to fix this without resorting to “else”?25Monte Carlo Simulation26Gambler's RuinGambler's ruin. Gambler starts with $stake and places $1 fair bets (win/lose= 50/50) until going broke or reaching $goal.What are the chances of winning?How many bets will it take?One approach. Monte Carlo simulation.Flip digital coins and see what happens.Repeat and compute statistics.27Digression: Simulation and AnalysisFact. Probability of winning = stake  goal.Fact. Expected number of bets = stake  desired gain.Ex.


View Full Document

WUSTL CSE 131 - sp14_2

Documents in this Course
sp14_4

sp14_4

28 pages

sp14_3

sp14_3

29 pages

sp14_10

sp14_10

19 pages

sp14_9

sp14_9

16 pages

sp14_8

sp14_8

22 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

sp14_5

sp14_5

55 pages

lecture1

lecture1

33 pages

lab0

lab0

6 pages

Load more
Download sp14_2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view sp14_2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sp14_2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?