The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.

Slides:



Advertisements
Similar presentations
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Advertisements

Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Chapter 5: Control Structures II (Repetition)
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
Control Statements Loops.
Building Java Programs
Control Statements Loops.
Building Java Programs
Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Homework Collaboration ♦ Declare it or lose points Proper naming of attached files Proper 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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 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 ♦ initialization - set loop control variable before condition ♦ condition - when to stop ♦ update - change to the loop control variable ♦ body - actions to repeat

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 The while Loop while (expression) statement

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Today in COMP 14 The for loop The do...while loop break and continue Nested loops

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 The for Loop Specialized form of while loop Simplifies the writing of count-controlled loops Basic Form: for (initialization; condition; update) { statement(s); }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 The for Loop Syntax for ( initialization; condition; update ) { loop body; } Reserved word The initialization is executed once before the loop begins The loop body is executed until the condition becomes false The update portion is executed at the end of each iteration The condition-loop body-update cycle is executed repeatedly

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 for vs. while A 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 Ilie 10 The for Loop Example final int LIMIT = 3; int count; for (count=0; count<LIMIT; count++) { System.out.println (count); } System.out.println (“All done!”); boolean condition loop body update initialization Output: All done!

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 Comparing while and for While Loop final int LIMIT=3; int i = 0; while (i < LIMIT) { System.out.println (i); i++; } System.out.println (“All done!”); For Loop final 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 Ilie 12 Watch out! for statement ending in semicolon is empty; does not affect program while statement ending in semicolon results in infinite loop for (count=0; count<LIMIT; count++); while (count<LIMIT);

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 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 ("*"); Hello * Hello * Hello * Hello * Hello * Hello * *

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 In-Class Exercise for vs. while Loops final int MAX = 20; int i; for (i = 0; i<MAX; i+=3) { System.out.println (i); } Predict the output Output:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 In-Class Exercise for vs. while Loops final 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 Ilie 16 The do...while Loop Syntax do { loop body; } while ( condition ); do and while are reserved words The loop body is executed once initially, and then the condition is evaluated The loop body is executed repeatedly until the condition becomes false

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 do…while Loop (Post-test Loop) do { statement(s); } while (expression);

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 The do...while Loop Example final int LIMIT = 3; int count = 0; do { System.out.println (count); count++; } while (count < LIMIT); System.out.println (“All done!”); boolean condition loop body update initialization Output: All done!

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 Comparing while and do...while while Loop final int LIMIT=3; int count = 0; while (count < LIMIT) { System.out.println (count); count++; } System.out.println (“All done!”); do...while Loop final 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 Ilie 21 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(); [blank line] 11

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 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 x y0 Output:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 break Example double in; while (true) { in=Double.parseDouble( keyboard.readLine()); if (in == 0.0) { break; } System.out.println (1.0 / in); } in Output

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 25 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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 26 continue Example double in; while (true) { in=Double.parseDouble( keyboard.readLine()); if (in == 0.0) { continue; } System.out.println (1.0 / in); } in Output infinite loop

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 27 Nested Control Structures Provides new power, subtlety, and complexity if, if…else, and switch structures can be placed within while loops for loops can be found within other for loops ♦ each time through the outer loop, the inner loop goes through its full set of iterations

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 28 Nested Control Structures Example for (int row = 1; row <= 5; row++) { for (int star = 1; star <= row; star++) { System.out.print(“*”); } System.out.println(); } Output: * ** *** **** ***** Can't use the variable row outside the outer for loop. Can't use the variable star outside the inner for loop.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 29 Exercise Printing Patterns Use nested for loops * * *

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 30 To do Assignment 4 Read ch. 6