Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loops More loops Off-by-one errors Infinite loops Nested Loops.
INF 523Q Chapter 3: Program Statements (Examples).
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Looping Yong Choi School of Business CSU, Bakersfield.
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Lecture 4 Loops.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Chapter 5 Loops.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
1 Conditionals Instructor: Mainak Chaudhuri
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Control Structures II
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Building java programs, chapter 5 Program logic and indefinite loops.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Computer Programming with Java
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
TK1114 Computer Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Loops/Iteration Used to repeat an action Must have a STOP condition
Building Java Programs
Building Java Programs
Object Oriented Programming
Building Java Programs
PROGRAM FLOWCHART Selection Statements.
Building Java Programs
Repetition Statements
Building Java Programs
Building Java Programs
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chap 7. Advanced Control Statements in Java
Building Java Programs
Building Java Programs
Building Java Programs
Control Statements:.
Presentation transcript:

Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write the program using a do loop you could also write the program using a for loop

A simple while loop example public class Sum { public static void main( String [] args) { final int MIN = 1, MAX = 100; int count = MIN, sum = 0; while (count <= MAX) { sum += count; count++; } System.out.println("the sum of the number between " + MIN + " and " + MAX + " is " + sum); }

Equivalent for loop public class Sum { public static void main( String [] args) { final int MAX = 100, MIN = 1; int sum = 0; for (int count = MIN; count <= MAX; count++) { sum += count; } System.out.println("the sum of the number between " + MIN + " and " + MAX + " is " + sum); }

A do loop example import cs1.Keyboard; public class Sum { public static void main( String [] args) { int min, max, sum = 0; do { System.out.print(“enter limits for the sum"); System.out.println(" both must > 0:"); min=Keyboard.readInt(); max=Keyboard.readInt(); } while (min <= 0 || max < =0); for (int count = min; count <= max; count++) sum += count; }

Choosing a looping structure --- “rule of thumb” guidelines Use a do loop or a while loop when the number of iterations can not be known in advance Use a for loop for “counting” Use a do loop when the body of the loop must be executed before evaluating the “loop test”

A silly game version 1 import cs1.Keyboard; public class Guess { public static void main( String [] args) { final int MYNUMBER = 3; System.out.print("guess a number between 1 & 10: "); int guess = Keyboard.readInt(); if (guess == MYNUMBER) System.out.println("You got it!!!"); else System.out.println("Sorry, that's wrong"); }

A silly game version 2 import cs1.Keyboard; public class Guess { public static void main( String [] args) { final int MYNUMBER = 3; int guess; do { System.out.print("guess # between 1 & 10: "); guess = Keyboard.readInt(); if (guess == MYNUMBER) System.out.println("You got it!!!"); else System.out.println("Sorry, that's wong"); } while (guess != MYNUMBER); }

A silly game version 3 Add a loop to repeat the game in class