More loops Linag, Chpt 3, pp 82-99. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
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)
Chapter 5: Loops and Files.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
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.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The switch Statement, DecimalFormat, and Introduction to Looping
Python Control of Flow.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Decision Making and Branching (cont.)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
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 Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 5: Control Structures II
Loop Structures.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
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 Control structures and Loops
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
Outline Altering flow of control Boolean expressions
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Repetition Statements
Chapter 4: Loops and Iteration
Presentation transcript:

More loops Linag, Chpt 3, pp 82-99

The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body statements next statement false true DO-LOOP

Using a do-loop Use this form when you want the loop body to be executed at least once int data; int sum = 0; String dataString; do { dataString = JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); sum += data; } while (data != 0); JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);

Things to know about do-loops The loop body will always be executed one The first execution is done before the guard is checked The do-loop is nothing more than a small variant of the while-loop It is often useful for checking user input

When to use the do-loop? int sum; int data; String dataString; dataString=JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); while(data != 0) { dataString = JOptionPane.showInputMessage(null,”enter number:”); data = Integer.parseInt(dataString); sum = sum + data; //could have used: sum += data; } JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);

A better way int data; int sum = 0; String dataString; do { dataString=JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); sum += data; } while (data != 0); JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum); We don’t have to get the first piece of data, add it to the sum variable, and then start the loop.

The for-loop The for-loop is another variant of the while loop Every for-loop can be written as a while loop Not every while-loop can be written as a for-loop Use a for-loop when you know exactly how many times the loop body should be executed

The for loop: Three things in one line for ( ; ; ) {... } 1.Initialization: (declare and) set a counter, to 0. This is done before everything else. 2.Guard: loop ends when this is false. This is tested before each pass through the loop. 3.Adjustment: e.g. increment a counter. This is done at the end of each pass through the loop.

Examples for (int i=0; i<10; i++) { JOptionPane.showMessageDialog(null, “ ”+i); } int i; for (i=10; i>0; i--) { JOptionPane.showMessageDialog(null, “ ”+i); }

From while to for int i = startValue; while (i < endValue) {..... i++; } for (int i=startValue; i<endValue; i++) {... }

Flow of control in a for-loop initialization test guard loop bodynext statement adjust true false

Rules and guidelines for “for” Counters can be declared and initialized in one go Never (never) change the value of the counter inside the loop body I mean it. Never do that! If the loop body is one statement only, you can omit the braces—but please don’t! Indent the code within the loop body..

Examples // Compute sum = … + 1;... double sum = 0; // Keep adding 0.01 to sum for (double i=0.01; i <= 1.0 ; i = i+0.01) { sum += i; } JOptionPane.showMessageDialog("The sum is " + sum);

Common error The 3 elements of the for-loop header are sparated by semi-colons, not commas! Do this: for (int i=0; i<10; i++) Not this: for (int i=0, i<10, i++) Keep your loops as simple as possible

Nesting for-loops Inside the loop body of a for-loop, we can put another for-loop Each time through the 1 st for-loop, we execute the 2 nd loop until its guard is false Handy for printing tables like this:

Simple example for (int i=0; i<5; i++) { for (int j=0; j<3; j++) { System.out.print(i+“ ”); } System.out.println(); }

Sample problem, p. 91 Multiplication table | | | | | |

Nested for-loop example p. 91 // Print table body for (int i=1; i<=9; i++) { output += i + " | "; for (int j=1; j<=9; j++) { // Display the product and align properly if (i*j < 10) output += " " + i*j; else output += " " + i*j; } output += “\n”; // the special character “\n” // represents the end of a line JOptionPane.showMessageDialog(null, output); }

break-ing out of a loop Inside a loop body, the statement break; causes execution to leave the loop immediately. This is usually unnecessary, and shows that not enough though has gone into the development of a guard.

Example of break int sum = 0; int item = 0; while (item < 5) { item ++; sum += item; if (sum >= 6) break; } System.out.println("The sum is " + sum);

continue This keyword also stops interation of the loop body But...the program then starts the next iteration of the loop (testing the loop guard first) This is also usually unnecessary, and can be replaced by an if-statement.

Example of continue int sum = 0; int item = 0; while (item < 5) { item++; if (item == 2) continue; sum += item; } System.out.println("The sum is " + sum);

Case studies Study cases 3.7, 3.8 and 3.9, Liang, pp Compile and run the programs Study the logic of flow control Then go outside and do something else