loops
play

Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while - PowerPoint PPT Presentation

Loops! Flow of Control: Loops (Savitch, Chapter 4) TOPICS while Loops do while Loops for Loops break Statement continue Statement CS 160, Fall Semester 2012 2 An Example While Loop


  1. Loops! ¡ Flow of Control: Loops (Savitch, Chapter 4) TOPICS • while Loops • do while Loops • for Loops • break Statement • continue Statement CS 160, Fall Semester 2012 2 An ¡Example ¡ While ¡Loop ¡ Step-­‑by-­‑step ¡ Code begins int count = 1; int ¡count ¡= ¡1; ¡ int sum = 0; int ¡sum ¡= ¡0; ¡ while (count < 5) while ¡(count ¡< ¡5) ¡ count = 1, sum = 0 { True again, False, exit loop true, enter loop True again, True again, { ¡ sum += count; (count 5, sum 10) re-enter loop re-enter loop re-enter loop ¡ ¡sum ¡+= ¡count; ¡ count++; ¡ ¡count++; ¡ } } ¡ ¡ What exactly does this code do? Bottom of loop: Bottom of loop: Bottom of loop: Bottom of loop: count = 2, sum = 1 count = 5, sum = 10 count = 3, sum = 3 count = 4, sum = 6 3 4 CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 1

  2. Echo ¡Example ¡ More ¡formally: while ¡Loops ¡ import ¡java.u3l.Scanner; ¡ while ( condition ) ¡ body public ¡class ¡Foo ¡{ ¡ ¡ ¡ ¡ ¡public ¡sta3c ¡void ¡main(String[] ¡args) ¡{ ¡ • Repeatedly ¡executes ¡as ¡long ¡as ¡the ¡ condition ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Scanner ¡in_str ¡= ¡new ¡Scanner(System. in ); ¡ evaluates ¡to ¡ true ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡user_string ¡= ¡in_str.next(); ¡ • body ¡ of ¡the ¡loop ¡ is ¡a ¡single ¡statement or ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(!user_string.equals("quit")) ¡{ ¡ mulIple ¡statements ¡within ¡{ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .println(user_string); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡user_string ¡= ¡in_str.next(); ¡ • The ¡condiIon ¡is ¡tested ¡before ¡the ¡body ¡is ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ executed, ¡so ¡loop ¡may ¡execute ¡zero ¡Imes ¡ ¡ ¡ ¡ ¡} ¡ – This ¡is ¡called ¡a ¡ pre-­‑test ¡loop ¡ } ¡ CS 160, Fall Semester 2012 5 CS 160, Fall Semester 2012 6 Echo ¡Example: ¡Notes ¡ Echo ¡Example: ¡QuesIons ¡ • “ import java.util.Scanner; ” is ¡ • How ¡many ¡Imes ¡will ¡the ¡loop ¡body ¡ necessary ¡to ¡use ¡a ¡Scanner. ¡ execute? ¡ – Problem: ¡Without ¡it, ¡Eclipse ¡will ¡tell ¡you ¡it ¡cannot ¡ – Undetermined: ¡it ¡will ¡keep ¡execuIng ¡unIl ¡the ¡ resolve ¡the ¡Scanner ¡class. ¡ user ¡types ¡ “ quit ” ¡ – SoluIon: ¡ctrl-­‑shiT-­‑o ¡will ¡import ¡needed ¡classes. ¡ • What ¡is ¡the ¡fewest ¡number ¡of ¡Imes ¡the ¡ • Remember ¡that ¡ “ ! ” ¡means ¡ “ not ” ¡in ¡Java. ¡ loop ¡body ¡could ¡execute? ¡ • Note ¡the ¡indentaIon: ¡the ¡body ¡of ¡the ¡while ¡loop ¡is ¡ – Zero ¡ indented ¡relaIve ¡to ¡the ¡surrounding ¡code. ¡ 7 8 CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 2

  3. Warning! ¡ What ¡if ¡my ¡program ¡gets ¡caught ¡ ¡in ¡an ¡infinite ¡loop? ¡ • You ¡will ¡need ¡to ¡kill ¡your ¡program ¡ – Unfortunately, ¡how ¡you ¡do ¡this ¡is ¡operaIng ¡system ¡specific ¡ • An ¡infinite ¡loop ¡will ¡occur ¡if ¡the ¡condiIon ¡never ¡ • Make ¡your ¡life ¡easier: ¡run ¡your ¡program ¡in ¡the ¡debugger! ¡ becomes ¡false. ¡ – In ¡eclipse, ¡select ¡ “ debug ” ¡instead ¡of ¡ “ run ” ¡from ¡the ¡run ¡menu. ¡ • Example: ¡ – It ¡will ¡offer ¡to ¡take ¡you ¡to ¡the ¡debug ¡view. ¡I ¡recommend ¡selecIng ¡ int count = 1; “ yes ” . ¡ ¡ int sum = 0; count – The ¡red ¡terminate ¡buaon ¡can ¡then ¡be ¡used ¡to ¡kill ¡a ¡program ¡in ¡an ¡ never gets infinite ¡loop. ¡ while (count <= 5) updated – – Side ¡benefit: ¡F5 ¡can ¡then ¡walk ¡through ¡your ¡program ¡step-­‑by-­‑ { always = 1 step, ¡and ¡the ¡variables ¡window ¡will ¡show ¡you ¡the ¡values ¡of ¡ sum += count; variables ¡at ¡each ¡step! ¡ } CS 160, Fall Semester 2012 9 CS 160, Fall Semester 2012 10 Another ¡example: ¡find ¡divisors ¡ Notes ¡on ¡divisor ¡example ¡ public ¡ class ¡foo ¡{ ¡ ¡ ¡ ¡public ¡ sta3c ¡ void ¡main(String[] ¡args) ¡{ ¡ • The ¡main ¡method ¡takes ¡an ¡array ¡of ¡strings ¡(called ¡args). ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡number ¡= ¡Integer. parseInt (args[0]); ¡ – args[0] ¡is ¡the ¡first ¡string ¡passed ¡to ¡the ¡method ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡divisor ¡= ¡2; ¡ – args[1] ¡would ¡be ¡the ¡second ¡string ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(divisor ¡< ¡number ¡) ¡{ ¡ – args.length ¡tells ¡you ¡how ¡many ¡strings ¡there ¡are ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡((number ¡% ¡divisor) ¡== ¡0) ¡{ ¡ – More ¡about ¡arrays ¡later… ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .print(divisor ¡+ ¡" ¡"); ¡ • Integer ¡is ¡an ¡object ¡class ¡in ¡Java. ¡It ¡has ¡a ¡method ¡that ¡reads ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ a ¡string ¡and ¡returns ¡the ¡integer ¡it ¡contains. ¡Hence ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡divisor ¡= ¡divisor ¡+ ¡1; ¡ “ Integer.parseInt(args[0]); ” ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ • We ¡append ¡a ¡space ¡to ¡the ¡number ¡when ¡prinIng, ¡so ¡that ¡ the ¡numbers ¡are ¡separated ¡in ¡the ¡output. ¡ ¡ ¡ ¡} ¡ } ¡ ¡ 11 12 CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 3

  4. Divisor ¡example ¡quesIons ¡ Boxscore ¡example ¡(removes ¡vowels) ¡ public ¡ class ¡Foo ¡{ ¡ ¡ ¡ ¡public ¡ sta3c ¡ void ¡main(String[] ¡args) ¡{ ¡ • If ¡the ¡argument ¡is ¡ ’ 32 ’ , ¡how ¡many ¡Imes ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡str ¡= ¡args[0]; ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡ctr ¡= ¡0; ¡ will ¡the ¡loop ¡body ¡be ¡executed? ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(ctr ¡< ¡str.length()) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡switch (str.charAt(ctr)) ¡{ ¡ – 30 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡'a' ¡: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡'e' ¡: ¡ • If ¡the ¡argument ¡is ¡ ‘ 2 ’ , ¡how ¡many ¡Imes ¡will ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡'i' ¡: ¡ the ¡loop ¡body ¡be ¡executed? ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡'o' ¡: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡'u' ¡: ¡ break ; ¡ – 0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡default ¡: ¡System. out .print(str.charAt(ctr)); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ • If ¡the ¡argument ¡is ¡ ‘ -­‑5 ’ , ¡what ¡will ¡happen? ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ctr ¡= ¡ctr ¡+ ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ – The ¡loop ¡body ¡will ¡run ¡0 ¡Imes ¡ ¡ ¡ ¡} ¡ } ¡ CS 160, Fall Semester 2012 13 CS 160, Fall Semester 2012 14 Box ¡score ¡example ¡notes ¡ Box ¡score ¡example ¡quesIons ¡ • If ¡the ¡input ¡is ¡ “ Ghosh ” : ¡ • The ¡charAt(i) ¡method ¡of ¡String ¡returns ¡the ¡ – How ¡many ¡Imes ¡will ¡the ¡loop ¡body ¡execute? ¡ ith ¡character. ¡ • 5 ¡ – Zero-­‑based: ¡0, ¡1, ¡2, ¡… ¡ – What ¡will ¡the ¡output ¡be? ¡ • Ghsh ¡ • ¡The ¡length() ¡method ¡of ¡String ¡returns ¡the ¡ • If ¡the ¡input ¡is ¡ “ Bartlea ” : ¡ number ¡of ¡characters ¡in ¡the ¡string. ¡ – How ¡many ¡Imes ¡will ¡the ¡loop ¡body ¡execute? ¡ • Exercise ¡ – What ¡will ¡the ¡output ¡be? ¡ • Exercise ¡ 15 16 CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend