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 loopAnd 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