Unformatted text preview:

COMP 14 Introduction to ProgrammingHomeworkLoopsThe while LoopToday in COMP 14The for LoopThe for Loop ExecutionThe for Loop Syntaxfor vs. whileThe for Loop ExampleComparing while and forWatch out!ExamplesIn-Class Exercise for vs. while LoopsSlide 15The do...while Loop Syntaxdo…while Loop (Post-test Loop)The do...while LoopThe do...while Loop ExampleComparing while and do...whilewhile vs. do...whileIn-Class Exercise The do...while Loopbreak Statementsbreak Examplecontinue Statementscontinue ExampleNested Control StructuresNested Control Structures ExampleExercise Printing PatternsTo doThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14Introduction to ProgrammingAdrian IlieJuly 6, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Homework•Collaboration♦Declare it or lose points•Proper naming of attached files•Proper email subject•Algorithm first, program later♦I will not help you with a program if you can’t show me at least a tentative algorithm first•Be easy on my eyes, use proper indentation•Don’t submit it if it doesn’t work, come talk to me insteadThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Loops•Allow us to repeat statements some number of times•Must use a loop control variable♦controls how many times to loop•4 Parts to Every Loop♦initialization - set loop control variable before condition♦condition - when to stop♦update - change to the loop control variable♦body - actions to repeatThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie4The while Loopwhile (expression)statementThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5Today in COMP 14•The for loop•The do...while loop•break and continue•Nested loopsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie6The for Loop•Specialized form of while loop•Simplifies the writing of count-controlled loopsBasic Form:for (initialization; condition; update){statement(s);}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7The for LoopExecution1. initial statement executes 2. loop condition evaluated3. If loop condition evaluates to true, execute for loop statement and execute update statement4. Go back to step 2 and repeat until loop condition is falseThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie8The for LoopSyntaxfor ( initialization; condition; update ){ loop body;}ReservedwordThe initializationis executed oncebefore the loop beginsThe loop body isexecuted until thecondition becomes falseThe update portion is executed at the end of each iterationThe condition-loop body-update cycle is executed repeatedlyThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie9for vs. whileA for loop is functionally equivalent to the following while loop structure:initialization;while ( condition ){ loop body; update;}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie10The for LoopExamplefinal int LIMIT = 3;int count;for (count=0; count<LIMIT; count++){System.out.println (count);}System.out.println (“All done!”);booleanconditionloop bodyupdateinitializationOutput:012All done!The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11Comparing while and forWhile Loopfinal int LIMIT=3;int i = 0;while (i < LIMIT){System.out.println (i);i++;}System.out.println (“All done!”);For Loopfinal int LIMIT=3;int i;for (i=0; i<LIMIT; i++){System.out.println (i);}System.out.println("All done!");The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12Watch out!•for statement ending in semicolon is empty; does not affect program•while statement ending in semicolon results in infinite loopfor (count=0; count<LIMIT; count++);while (count<LIMIT);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Examplesfor (i=1; i<=5; i++){System.out.println ("Hello");System.out.println ("*");}for (i=1; i<=5; i++)System.out.println ("Hello");System.out.println ("*");for (i=1; i<=5; i++);System.out.println ("*");Hello*Hello*Hello*Hello*Hello*HelloHelloHelloHelloHello**The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie14In-Class Exercisefor vs. while Loopsfinal int MAX = 20;int i;for (i = 0; i<MAX; i+=3){System.out.println (i);}•Predict the outputOutput:0369121518The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie15In-Class Exercisefor vs. while Loopsfinal int MAX = 20;int i;for (i = 0; i<MAX; i+=3){System.out.println (i);}•Translate this to a while loop. final int MAX = 20;int i = 0;while (i < MAX){System.out.println (i);i += 3;}The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16The do...while LoopSyntaxdo{ loop body;}while ( condition );do andwhile arereservedwordsThe loop body is executed once initially,and then the condition is evaluatedThe loop body is executed repeatedlyuntil the condition becomes falseThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie17do…while Loop(Post-test Loop)do { statement(s);} while (expression);The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18The do...while Loop•Like a while loop, but its condition is at the end of the loop•Loop body always executes at least once•Must also be checked for termination (not an infinite loop)•Anything you can do with a do...while loop, you can do with a while loopThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie19The do...while LoopExamplefinal int LIMIT = 3;int count = 0;do {System.out.println (count);count++;} while (count < LIMIT);System.out.println (“All done!”);boolean conditionloopbodyupdateinitializationOutput:012All done!The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie20Comparing while and do...whilewhile Loopfinal int LIMIT=3;int count = 0;while (count < LIMIT){System.out.println (count);count++;}System.out.println (“All done!”);do...while Loopfinal int LIMIT=3;int count = 0;do{System.out.println (count);count++;}while (count < LIMIT);System.out.println("All done!");The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie21while vs. do...whilei = 11;while (i <= 10) {System.out.print (i);i += 5;}System.out.println();i = 11;do {System.out.print (i);i += 5;} while (i <= 10);System.out.println();[blank line]11The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie22In-Class ExerciseThe do...while Loopint x = 0, y = 0;do {System.out.println (x*y);if (y < x){y += 2; }x++;} while (x < 5);•Predict the output of the loopx y0 0Output:1200461623445The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie23break Statements•Used to exit


View Full Document

UNC-Chapel Hill COMP 14 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?