Unformatted text preview:

COMP 14 Introduction to Programming Adrian Ilie July 6 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Homework 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 instead 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Loops 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 3 initialization set loop control variable before condition condition when to stop update change to the loop control variable body actions to repeat Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop while expression statement 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Today in COMP 14 The for loop The do while loop break and continue Nested loops 5 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The for Loop Specialized form of while loop Simplifies the writing of count controlled loops Basic Form for initialization condition update statement s 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The for Loop Execution 1 initial statement executes 2 loop condition evaluated 3 If loop condition evaluates to true execute for loop statement and execute update statement 4 Go back to step 2 and repeat until loop condition is false 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The for Loop Syntax Reserved word The initialization The loop body is is executed once executed until the before the loop begins condition becomes false for initialization condition update loop body The update portion is executed at the end of each iteration The condition loop body update cycle is executed repeatedly 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL for vs while A for loop is functionally equivalent to the following while loop structure initialization while condition loop body update 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The for Loop Example final int LIMIT 3 int count boolean condition Output 0 1 2 All done initializatio n for count 0 count LIMIT count System out println count update loop body System out println All done 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Comparing while and for While Loop For Loop final int LIMIT 3 int i 0 final int LIMIT 3 int i while i LIMIT System out println i i for i 0 i LIMIT i System out println i System out println All done System out println All done 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Watch out for statement ending in semicolon is empty does not affect program for count 0 count LIMIT count while statement ending in semicolon results in infinite loop while count LIMIT 12 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Examples for 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 13 Adrian Ilie Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello The UNIVERSITY of NORTH CAROLINA at CHAPEL In Class Exercise for vs while Loops final int MAX 20 int i for i 0 i MAX i 3 System out println i 14 Output 0 3 6 9 12 15 18 Predict the output Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL In Class Exercise for vs while Loops final int MAX 20 int i for i 0 i MAX i 3 System out println i 15 Translate this to a while loop Adrian Ilie final int MAX 20 int i 0 while i MAX System out println i i 3 The UNIVERSITY of NORTH CAROLINA at CHAPEL The do while Loop Syntax do and while are reserved words do loop body while condition The loop body is executed once initially and then the condition is evaluated The loop body is executed repeatedly until the condition becomes false 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL do while Loop Post test Loop do statement s while expression 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The 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 loop 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The do while Loop Example final int LIMIT 3 int count 0 updat initializati e do on System out println count count while count LIMIT Output 0 1 2 All done loop body System out println All done boolean condition 19 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Comparing while and do while while Loop do while Loop final int LIMIT 3 int count 0 final int LIMIT 3 int count 0 while count LIMIT System out println count count do System out println All done 20 Adrian Ilie System out println count count while count LIMIT System out println All done The UNIVERSITY of NORTH CAROLINA at CHAPEL while vs do while i 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 21 Adrian Ilie blank line 11 The UNIVERSITY of NORTH CAROLINA at CHAPEL In Class Exercise The do while Loop int x 0 y 0 do System out println x y if y x y 2 x while x 5 Predict the output of the loop 22 Adrian Ilie x y 0 0 1 2 2 3 4 4 5 Output 0 0 4 6 16 The UNIVERSITY of NORTH CAROLINA at CHAPEL break Statements Used to exit early from a loop Used to skip remainder of switch structure Can be placed within if statement of a loop If condition is met loop exited immediately 23 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL break Example double in while true in Double parseDouble keyboard readLine if in 0 0 break System out println 1 0 in 24 Adrian Ilie in Output 1 0 1 00 2 0 0 50 4 0 0 25 0 0 The UNIVERSITY of NORTH CAROLINA at CHAPEL continue Statements Used in while for and do while structures When executed in a loop the remaining statements in the loop are skipped proceeds with the next iteration of the loop When executed in a while do while structure expression evaluated immediately after continue statement In a for structure the update statement is executed after the continue statement then the loop condition executes 25 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL continue Example double in while true in Double parseDouble keyboard readLine if in 0 0 continue System …


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