LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.

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

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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: Loops and Files.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 4: Control Structures II
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 4: Loops and Files
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
The switch Statement, and Introduction to Looping
Lecture 7: Repeating a Known Number of Times
Topics Introduction to Repetition Structures
Chapter 5: Control Structures II
Loop Structures.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Java Programming: Guided Learning with Early Objects
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
LRobot Game.
CIS 16 Application Development Programming with Visual Basic
Control Statements Loops.
Chapter 6: Repetition Statements
© 2016 Pearson Education, Ltd. All rights reserved.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computing Fundamentals
Topics Introduction to Repetition Structures
ECE 103 Engineering Programming Chapter 18 Iteration
Repetition Statements (Loops) - 2
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds of loops: the while loop, the do-while loop, and the for loop. 1 1

THE while LOOP The while loop/statement has the form: while (Boolean expression) { statement(s); } The while(Boolean expression) is the loop header. It consists of the key word while and a parenthesized Boolean expression, often called the loop continuation expression. The body of the loop follows the header. The body of the loop is the statement or block of statements that are to be repeated. 2 2

THE while LOOP while (Boolean expression) { statement(s); } When execution reaches a while statement, the parenthesized loop continuation expression is evaluated, if the expression is true the statement(s) that make up the body are executed and then execution returns to the evaluation of the continuation expression. This process is repeated until the loop continuation expression is evaluated and has the value false. When this happens, execution continues with the statement following the while statement. 3 3

THE while LOOP while (Boolean expression) { statement(s); } One repetition of the body of a loop is called an iteration. The while loop is a pretest loop. The continuation expression is tested before the body of the loop is executed. If the continuation expression is false on the initial test, the body of the loop is never executed, i.e. it never iterates. 4 4

THE while LOOP Flowchart Representation of a while Loop 5 5

THE while LOOP The loop continuation expression must eventually become false or an infinite loop results. An infinite loop is a loop that does not have a way to terminate normally. 6 6

THE while LOOP Don’t put a semicolon here. The loop header is only the beginning of the while statement. while (Boolean expression) { statement(s); } The braces are not necessary if the body of the loop is a single statement. Notice that there is no semicolon after the loop continuation expression. A complete while statement includes a loop header and the loop body. 7 7

THE while LOOP while (Boolean expression) { statement(s); } It is considered good programming style to begin the body of the loop on the line below the continuation expression and indent the statement(s) that make up the body of the loop one level from the key word while. 8 8

THE while LOOP /** This program demonstrates the while loop. */ public class WhileLoop { public static void main(String [ ] args) int number = 1; while (number <= 5) System.out.println("Hello"); number++; } System.out.println("That's all!"); 9 9

THE while LOOP The loop in WhileLoop.java is a particular kind of loop known as a count-controlled loop. A count-controlled loop is a loop that iterates a specific number of times. This type of loop uses a control variable, usually called a counter, to control the number of times the loop iterates. When you construct a count-controlled loop you must be sure to: 1. Give the counter a starting value. 2. Compare the counter to a maximum (or minimum) value in the loop continuation expression. When the counter reaches this value the loop terminates. 3. Update the value of counter. 10 10

THE while LOOP In the program WhileLoop.java the variable named number is the counter used to control the number of iterations of the loop. /** Code Listing 4-3 from the text. This program demonstrates the while loop. */ public class WhileLoop { public static void main(String [ ] args) int number = 1; while (number <= 5) System.out.println("Hello"); number++; } System.out.println("That's all!"); 1.We are giving the counter, named number, a starting value, count, of 1. 2. We are comparing the value in the counter to the upperbound on the count, in this case it is 5. 3. Each iteration of the loop we are updating the value in the counter with this statement. 11 11

USING THE while LOOP FOR INPUT VALIDATION According to the good programming style, "input validation is the process of inspecting data given to a program by the user and determining if it is valid. A good program should give clear instructions about the kind of input that is acceptable, and not assume the user has followed these instructions." When the user enters invalid data the program should: 1. Display a descriptive error message 2. Ask for another input 3. Read the user's new input An input validation loop iterates until the user enters valid data. 12 12

USING THE while LOOP FOR INPUT VALIDATION The logic for the while loop version of an input validation loop is shown here. 13 13

THE do-while LOOP The do-while loop/statement has the form: { statement(s); } while (Boolean expression); The key word do is followed by the body of the loop, the key word while, the parenthesized loop continuation expression, and a semicolon. 14 14

THE do-while LOOP do { statement(s); } while (Boolean expression); When execution reaches a do-while statement, the statement or block of statements that make up the body of the loop are executed, and then the loop continuation expression is evaluated. If this expression evaluates to true, execution continues at the first statement of the loop body. This process is repeated until the loop continuation expression is evaluated and has the value false. When the continuation expression evaluates to false, execution continues with the first statement following the do-while statement. 15 15

THE do-while LOOP do { statement(s); } while (Boolean expression); The do-while loop is a posttest loop. The loop continuation expression is evaluated at the end of an iteration of the loop body. The do-while loop iterates at least once. 16 16

THE do-while LOOP Flowchart Representation of a do-while Loop 17 17

THE do-while LOOP do { statement(s); } while (Boolean expression); There must be a semicolon after the loop continuation expression of a do-while statement. The braces are not necessary if the body of the loop is a single statement. Notice there is a semicolon here, because this is the end of the do-while statement. 18 18

THE do-while LOOP do { statement(s); } while (Boolean expression); It is considered good programming style to begin the body of the loop on the line below the key word do and indent the statements that make up the loop body one level from the word do. 19 19

THE do-while LOOP The do-while loop is ideal for situations where we want the body of the loop to iterate at least once. 20 20

THE for LOOP The for loop/statement has the form: for (initialization; Boolean expression; update) { statement(s); } The for(initialization; Boolean expression; update) is the loop header. It consists of the key word for followed by three parenthesized expressions separated by semicolons. Notice there is not a semicolon after the third expression, the update. 21 21

THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The first part is the initialization. It is typically used to initialize a counter, accumulator, or other variable that must be given a starting value. The initialization is only executed before the first iteration of the loop. 22 22

THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The second expression is the loop continuation expression. This is a Boolean expression that gets evaluated before every iteration of the loop to determine if the loop should iterate. 23 23

THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The third expression is the update. The update is executed at the end of each iteration before execution returns to the continuation expression. Typically, this is a statement that updates the value of a variable that is used to control the loop. 24 24

This is the body of the loop. THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The body of the loop follows the header. The body of the loop is the statement or block of statements that are to be repeated. This is the body of the loop. 25 25

THE for LOOP for (initialization; Boolean expression; update) { statement(s); } When execution reaches a for statement, the initialization is executed. The loop continuation expression is evaluated, if this expression is true, the statement(s) that make up the body of the loop are executed and then the update is executed before returning to the evaluation of the loop continuation expression. The process described in the previous bullet is repeated until the loop continuation expression is evaluated and has the value false. When this occurs, execution continues with the statement following the for statement. 26 26

THE for LOOP for (initialization; Boolean expression; update) { statement(s); } The for loop is a pretest loop. If the continuation expression evaluates to false on the initial test, the body of the loop does not iterate (the statements in the loop body and the update are never executed). 27 27

THE for LOOP Flowchart Representation of a for Loop 28 28

THE for LOOP Don’t put a semicolon here. The loop header is only the beginning of the for statement. for (initialization; Boolean expression; update) { statement(s); } Notice that there is no semicolon after the loop header. A complete for loop/statement includes the loop body. The braces are not necessary if the body of the loop is a single statement. 29 29

THE for LOOP for (initialization; Boolean expression; update) { statement(s); } It is considered good programming style to begin the body of the loop on the line below the parenthesized expressions and indent the statements that make up the body of the loop one level from the key word for. 30 30

THE for LOOP Problem: Write the pseudocode for a program that gets 50 grades from the user and calculates and displays the average of the grades on the computer screen. 31 31

THE for LOOP An accumulator is a variable that is used to store a running total that “accumulates” over the iterations of the loop. When you use an accumulator you must be sure to give it a starting value before the first iteration of the loop. 32 32