CS1150 Principles f Cmputer Science Lps Yanyan Zhuang Department f Cmputer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Clrad Springs
Review Blean variables Assume x=3, y=1, true r false?!(x<2) y>3 If statement Be careful: multiple/nested if else By default: else is mathced with if? Switch statement Be careful: where t use break CS4500/5500 UC. Clrad Springs
Overview While lp D while lp Fr lp CS1150 UC. Clrad Springs
Opening Prblem: Why Lps? Prblem: 100 times System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); System.ut.println("Welcme t Java!"); 4
Intrducing while Lps int cunt = 0; while (cunt < 100) { System.ut.println("Welcme t Java"); cunt++; 5
Intrducing while Lps int cunt = 0; while (cunt < 100) { System.ut.println("Welcme t Java"); cunt++; while (lp-cntinuatin-cnditin) { // lp-bdy; Statement(s); Hw It Wrks The lp cntinuatin cnditin - blean expressin - is evaluated If the cnditin is true, the statements in the lp bdy are executed When executin f lp bdy statements is cmplete, cntrl returns t the lp cnditin The lp cntinuatin cnditin is evaluated again When the lp cnditin is false, cntrl ges t statements fllwing the lp Nte: if the lp cntinuatin cnditin evaluates t false the first time, the entire while lp is skipped 6
while Lp Flw Chart while (lp-cntinuatin-cnditin) { // lp-bdy; Statement(s); int cunt = 0; while (cunt < 100) { System.ut.println("Welcme t Java!"); cunt++; 7
Rules fr While Lps The lp cnditin must be a blean expressin Blean expressin must be in parentheses Blean expressins are frmed using relatinal r lgical peratrs Lp cnditin Usually a statement befre while lp "initializes" lp cnditin t true Sme statement within lp bdy eventually change the cnditin t false If the cnditin is never changed t false, the prgram is frever in the lp This is called an "infinite lp" Curly braces are nt necessary if nly ne statement in lp But best practice is t always include curly braces CS4500/5500 UC. Clrad Springs
Trace while Lp int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Initialize cunt (which we ften call cntrl variable) 9
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; (cunt < 2) is true 10
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Print Welcme t Java 11
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Increase cunt by 1 cunt is 1 nw 12
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; (cunt < 2) is still true since cunt is 1 13
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Print Welcme t Java 14
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; Increase cunt by 1 cunt is 2 nw 15
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; (cunt < 2) is false since cunt is 2 nw 16
Trace while Lp, cnt. int cunt = 0; while (cunt < 2) { System.ut.println("Welcme t Java!"); cunt++; The lp exits. Execute the next statement after the lp. Let s lk at the first example PrintNTimes.java 17
Infinite lp example In this example, nthing in the lp bdy changes the value f the cntrl variable cunt = 1; // Initializes the lp cntrl variable while (cunt <= 5) { System.ut.println("The value f cunt is " + cunt); This is an infinite lp because (cunt <= 5) is always true Nthing changes the value f cunt in the lp bdy If yu accidentally create an infinite lp, use terminate buttn (red square) in Eclipse t make it stp CS4500/5500 UC. Clrad Springs
Placing a semicln at the end f the while-clause creates an infinite lp - be careful! int iteratin = 1; while (iteratin <= 10);{ System.ut.println("Iteratin = " + iteratin); iteratin++; CS4500/5500 UC. Clrad Springs
Off-by-ne Errr Cmmn issue with lps: Lp bdy executes ne mre/less than expected Example: System.ut.println("I'm ging t cunt t three, ready set..."); cunt = 1; while (cunt < 3) { System.ut.println(cunt); cunt++; CS4500/5500 UC. Clrad Springs
Off-by-ne Errr Cmmn issue with lps: Lp bdy executes ne mre/less than expected Example: System.ut.println("I'm ging t cunt t three, ready set..."); cunt = 1; while (cunt < 3) { Output: System.ut.println(cunt); cunt++; I'm ging t cunt t three, ready set... 1 2 CS4500/5500 UC. Clrad Springs
Prblem: Repeat Additin Until Crrect See RepeatAdditinQuiz.java. 22
Ending a Lp with a Sentinel Value Often the number f times a lp is executed is nt predetermined. Yu may use an input value t signify the end f the lp. Such a value is knwn as a sentinel value. Write a prgram that reads and calculates the sum f an unspecified number f integers (e.g., the sum f 2, 3, 5, 7, 11 ). The input 0 signifies the end f the input. See SentinelValue.java. 23
d-while Lp d { // Lp bdy; Statement(s); while (lp-cntinuatin-cnditin); Example: TestDWhile.java The lp bdy is executed The lp cnditin - blean expressin - is evaluated If the lp cnditin is true, then lp bdy is executed again If the lp cnditin is false, cntrl is transferred t the statement fllwing the lp 24
D While Lp Rules (same as while lp) The lp cnditin must be a blean expressin Blean expressin must be in parentheses Blean expressin is frmed using relatinal and lgical peratrs Lp cnditin Generally, sme statement befre the while lp "initializes" the lp cnditin t true Sme statement within the lp bdy must eventually change the cnditin t false If the cnditin is never changed t false, the prgram will be frever stuck in the lp This is called an "infinite lp" Curly braces are nt necessary if nly ne statement in lp but best practice is t always include curly braces CS4500/5500 UC. Clrad Springs
Nte Recall hw placing a semicln at the end f the while-clause creates an infinite lp int iteratin = 1; while (iteratin <= 10); { // Unnecessary semicln System.ut.println("Iteratin = " + iteratin); iteratin++; CS4500/5500 UC. Clrad Springs
Nte In the case f d-while yu must include the semicln since it ends the lp! int iteratin = 1; d { iteratin++; System.ut.println("Iteratin = " + iteratin); while (iteratin <= 5); // Necessary semicln CS4500/5500 UC. Clrad Springs
Lp Design Strategies Fur steps when writing a lp. Step 1: Identify what statements need t be repeated Step 2: Wrap these statements in a lp using while r d while: while (true) { Statements; Step 3: Determine what cnditin the cde shuld check (replace true) Step 4: Add cde in the bdy that eventually causes the cnditin t becme false while (lp-cntinuatin-cnditin) { Statements; Additinal statements fr cntrlling the lp; CS4500/5500 Example: Pwers.java UC. Clrad Springs
Summary While lp D while lp CS1150 UC. Clrad Springs