Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Chapter 31 Flow of Control Chapter 3. 2 Reminders Project 1 was due last night Project 2 released: due Sept 10:30 pm - No Late Submissions Follow.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 4: Control Structures II
Flow of Control Chapter 3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter 31 Flow of Control Chapter 3. 2 Objectives learn about Java branching statements learn about loops learn about the type boolean (optional) learn.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 4 Repetition Statements (loops)
Loops in Java.
Control Structures.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Loop Control Structure.
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Flow of Control Chapter 3 Chapter 3.
Outline Altering flow of control Boolean expressions
Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is.
Lab5 PROGRAMMING 1 Loop chapter4.
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

Flow of Control Loops – Chapter 3.2

Java Loop Statements: Outline the while Statement the do-while Statement the for Statement

Java Loop Statements, cont. A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is called the body of the loop. A loop could be used to compute grades for each student in a class. There must be a means of exiting the loop.

the while Statement also called a while loop A while statement repeats until a controlling boolean expression becomes false. –If the controlling boolean expression is false initially, the while loop is not executed. The loop body typically contains a statement that ultimately causes the controlling boolean expression to become false.

the while Statement, cont. syntax while (Boolean_Expression) Body_Statement or while (Boolean_Expression) { First_Statement Second_Statement … }

Example int count=0; while (count<5) { System.out.println(count); count++ } System.out.println(″count after loop = ″ + count);

The do-while Statement also called a do-while loop similar to a while statement, except that the loop body is executed at least once syntax do Body_Statement while (Boolean_Expression); –don’t forget the semicolon!

The do-while Statement, cont. First, the loop body is executed. Then the boolean expression is checked. –As long as it is true, the loop is executed again. –If it is false, the loop is exited. equivalent while statement Statement(s)_S1 while (Boolean_Condition) Statement(s)_S1

Example int count = 0; do { System.out.println(count) count++; } while (count < 0); System.out.println(″count after loop = ″ + count);

Infinite Loops A loop which repeats without ever ending is called an infinite loop. If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending.

The for Statement A for statement executes the body of a loop a fixed number of times. example for (count = 1; count < 3; count++) System.out.println(count); System.out.println(“Done”);

Exercise What output is produced by the following code? int n for (n=1; n<=4; n++) System.out.println(n); int n for (n=1; n>4; n++) System.out.println(n);

Exercise What output is produced by the following code? double n; for (n=0; n<3; n = n + 0.5) System.out.println(n); Write a for statement that writes out even numbers 2,4,6,8,10.

Beware the Empty for Statement What is printed by int product = 1, number; for (number = 1; number <= 10; number++); product = product * number; System.out.println(product); ? The last semicolon produces an empty for statement.

The Beware the Empty while Statement int product = 1, number = 1; while (number <= 10); { product = product * number; number++ } System.out.println(product); The last semicolon in while (number <= 10); produces an empty while loop body.

Choosing a Loop Statement If you know how many times the loop will be iterated, use a for loop. If you don’t know how many times the loop will be iterated, but –it could be zero, use a while loop –it will be at least once, use a do-while loop. Generally, a while loop is a safe choice.

The break Statement in Loops A break statement can be used to end a loop immediately. The break statement ends only the innermost loop or switch statement that contains the break statement. break statements make loops more difficult to understand. Use break statements sparingly (if ever).

Loop Bugs common loop bugs –unintended infinite loops –off-by-one errors –testing equality of floating-point numbers subtle infinite loops –The loop may terminate for some input values, but not for others. –For example, you can’t get out of debt when the monthly penalty exceeds the monthly payment.

Programming Exercise Write a program that reads a list of positive numbers terminated by a negative number and then prints the total and average.