ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

June 10, 2015ICS102: while & do-while1 while and do-while Statements.
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.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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
© 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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
© 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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Loop 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.
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
3.5- The while Statement The while statement has the following syntax:
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
CprE 185: Intro to Problem Solving (using C)
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Outline Boolean Expressions The if Statement Comparing Data
‘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:

ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops

ECE122 L9: While loops March 1, 2007 Outline °Problem: How can I perform the same operation again and again based on a condition? °Remembering conditional statements Same as if statements °The “while” statement Keep performing operations while the condition is true °Nested for loops Determining how many times an operation takes place

ECE122 L9: While loops March 1, 2007 Repetition Statements °Repetition statements allow us to execute a statement multiple times °Often they are referred to as loops °Like conditional statements, they are controlled by boolean expressions °  Java has three kinds of repetition statements: the while loop the do loop the for loop °  The programmer should choose the right kind of loop for the situation

ECE122 L9: While loops March 1, 2007 The while Statement °A while statement has the following syntax: while ( condition ) statement; °If the condition is true, the statement is executed °Then the condition is evaluated again, and if it is still true, the statement is executed again °The statement is executed repeatedly until the condition becomes false

ECE122 L9: While loops March 1, 2007 Logic of a while Loop statement true false condition evaluated Note: there is a ‘pretest.’ What does this mean??

ECE122 L9: While loops March 1, 2007 The while Statement °An example of a while statement: int count = 1; while (count <= 5) { System.out.println (count); count++; } °If the condition of a while loop is false initially, the statement is never executed °Therefore, the body of a while loop will execute zero or more times

ECE122 L9: While loops March 1, 2007 The while Statement °Let's look at some examples of loop processing °A loop can be used to maintain a running sum °A sentinel value is a special input value that represents the end of input °A loop can also be used for input validation, making a program more robust

ECE122 L9: While loops March 1, 2007 Infinite Loops °The body of a while loop eventually must make the condition false °If not, it is called an infinite loop, which will execute until the user interrupts the program °  This is a common logical error °Double check the logic of a program to ensure that your loops will terminate normally Many funny stories as a result of infinite loops

ECE122 L9: While loops March 1, 2007 Infinite Loops °An example of an infinite loop: int count = 1; while (count <= 25) { System.out.println (count); count = count - 1; } °This loop will continue executing until interrupted (Control-C) or until an underflow error occurs

ECE122 L9: While loops March 1, 2007 Nested Loops °Similar to nested if statements, loops can be nested as well °That is, the body of a loop can contain another loop °For each iteration of the outer loop, the inner loop iterates completely

ECE122 L9: While loops March 1, 2007 Nested Loops °How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { System.out.println ("Here"); count2++; }// end inner while count1++; } // end out while

ECE122 L9: While loops March 1, 2007 The do Statement °A do statement has the following syntax: do { statement; } while ( condition ) °The statement is executed once initially, and then the condition is evaluated °The statement is executed repeatedly until the condition becomes false

ECE122 L9: While loops March 1, 2007 Logic of a do Loop true condition evaluated statement false Note: no ‘pretest.’ Statement will at least be executed one time!

ECE122 L9: While loops March 1, 2007 The do Statement °An example of a do loop: °The body of a do loop executes at least once int count = 0; do { count++; System.out.println (count); } while (count < 5);

ECE122 L9: While loops March 1, 2007 Comparing do-while and while °Done using do-while numberOfDigits = 0; rest = number; do { rest = rest / 10; numberOfDigits++; } while (rest != 0); °Equivalent while loop numberOfDigits = 0; rest = number; if (number = 0) numberOfDigits = 1; else while (rest > 0) { rest = rest / 10; numberOfDigits++; }

ECE122 L9: While loops March 1, 2007 do can be written as while °A do loop can be easily re-written as a while loop. do statement; while (condition); statement; while (condition) statement; turns into

ECE122 L9: While loops March 1, 2007 do can be written as while °A do loop can be easily re-written as a while loop. do statement; while (condition); statement; while (condition) statement; turns into These are exactly the same statement.

ECE122 L9: While loops March 1, 2007 Comparing while and do statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop

ECE122 L9: While loops March 1, 2007 Summary °Loops form an essential part of programming Allow for repetitive actions °Pre-test loops (while) are only executed if the condition is true Can lead to “infinite loops” °Do-while loops allow the statements in the loop to execute at least once Not used very often °Next time: For loops