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.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
1 Infinite Loops  The body of a while loop eventually must make the condition false  If not, it is an infinite loop, which will execute until the user.
©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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
5.05 Apply Looping Structures
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Programming Logic and Design Fifth Edition, Comprehensive
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Chapter 5 Loops.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
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.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
CprE 185: Intro to Problem Solving (using C)
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Arrays, For loop While loop Do while loop
Looping and Repetition
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Java Programming Loops
The ‘while’ Statement September 27, 2006
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Java Programming Loops
Repetition Statements (Loops) - 2
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Outline Boolean Expressions The if Statement Comparing Data
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.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

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 might not be executed at all. The expression is evaluated again after each execution of the statement until the expression becomes false.

The while Statement The while statement has the following syntax: while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false.

Logic of a while Loop condition evaluated false statement true

The while Statement Note that if the condition of a while statement is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times A sentinel value indicates the end of the input The variable sum maintains a running sum A loop is used to validate the input, making the program more robust

Counter.java The Counter program shown here simply prints the values from 1 to 5. Each turn through the loop prints one values, then increases the counter by one. A constant called LIMIT holds the maximum value that count is allowed to reach. The condition of the while lop, means that the loop will keep going as long as count is less than or equal to LIMIT. Once count reaches the limit, the condition is false and the loop quits. Please pause the video if you need more time to study the loop code.

Average.java The Average program shown here reads integer values from the user, adds then up, and computes their average. We don’t know how many values the user may enter, so we need to have a way to show that the user is done. In this program, we pick zero to be a sentinel value, which is a value that shows the end to the input the way a sentinel stands guard the gate of a port or perimeter of an army’s camp. The while loop continues to process input values until the user enters zero. A sentinel value must always be outside the normal range of values entered. In this program a variable called sum is used to keep a running sum which means it is the total of the values entered so far. The variable sum starts at zero, and each value read is added to and stored back into sum. We also have to count the number of values that are entered so that after the loop finishes we can divide by the right number to get the average. Note that the sentinel value is not counted. Please pause the video if you need more time to study the loop code.

WinPercentage.java The WinPercentage program computes the winning percentage of a sports team based on the number of games won. We use a while loop this program to validate the input, meaning we guarantee that the user enters a value that we consider to be valid. The while loop keeps executing, repeatedly asking the user for a valid input, until the entered number is indeed valid. Validating input data, avoiding errors such as dividing by zero, and performing other actions that guarantee proper processing are important design steps. We generally want our programs to be robust, which means that they handle errors, even user errors, well. Please pause the video if you need more time to study the loop code.

Infinite Loops The body of a while loop eventually must make the condition false If not, it is an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check to ensure that your loops will terminate normally

Forever.java The programmer must make sure that the condition of a loop will eventually become false. If it doesn’t, the loop will keep going forever, or at least until the program is interrupted. The infinite loop, like the one shown above, is a common mistake. Please pause the video if you need more time to study the loop code.

Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop Each time through the outer loop, the inner loop goes through its full set of iterations

PalindromeTester.java The code here contains two loops, one inside the other. The outer loop controls how many strings are tested, and the inner loop scans through each string, character by character, until it determines whether the string is a palindrome. Please pause the video if you need more time to study the loop code.

Iterators An iterator is an object that has methods that allow you to process a collection of items one at a time The hasNext and next methods are used to loop through the collection Several classes in the Java class library define iterator objects, including Scanner while (myCollection.hasNext()) { System.out.println(myCollection.next()); }