1 Class 13. 2 2 Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 5 Loops.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Repetition Statements while and do while loops
Chapter 5: Control Structures II
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
Java Review if Online Time For loop Quiz on Thursday.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Introduction to Computers and Programming Lecture 7:
Computer Programming1 Computer Science 1 Computer Programming.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
John Hurley Spring 2011 Cal State LA CS 201 Lecture 6:
Chapter 9 Repetition.
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
2.5 Another Java Application: Adding Integers
Chapter 5: Control Structures II
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Counted Loops.
Chapter 5: Control Structures II
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Arrays Declare the Array of 100 elements Notes
Chapter 5 Repetition.
Flow Control in Java.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Chapter 9 Control Structures.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 4: Loops and Files
LOOPS BY: LAUREN & ROMEO.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
Learning Plan 4 Looping.
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.
The while Looping Structure
Presentation transcript:

1 Class 13

2 2 Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking to user input

3 while (expression) statement; while (expression) { statement; statement; // Place as many statements here // as necessary. }

4 1. Test this expression. 2. If the expression is true, perform the input statement. while (number ! = 99) {response = JOptionPane.showInputDialog( “Enter a number”); number = Integer.parseInt(response); } String response; int number =0;

5 1. Test this expression. 2. If the expression is true, perform the code within the { } while (number < 0) {response = JOptionPane.showInputDialog( “Enter a number”); number = Integer.parseInt(response); if (number < 0) JOptionPane.showMessageDialog(null, “error..do not enter numbers less than zero”, ”Error Message”,JOptionPane.ERROR_MESSAGE); } String response; int number =-1;

6 // infinite loop int test = 0; while (test < 10) { System.out.println(“Hello”); } int test = 0; while (test < 10) { System.out.println(“Hello”); test = test +1; } Loop

7 // infinite loop (ended here) int test = 0; while (test < 10); { System.out.println(“Hello”); test = test +1; } Loop

8 int num = 0; while (num <10) { num=num +1; System.out.println(num + “ “ + (num*num)); } Num is compared to 10. If it is less than 10, the code within the { } is executed. When the output statement executes, num is 1 greater than it was in the relational test.

9 Program Output

10 public static void main(String a[]) {int numScores, count =0 ; String answer1; int total =0; double average; JOptionPane.showMessageDialog(null, “This program will give you the \n“ + “average of test scores. entered”); answer1=JOptionPane.showInputDialog(null, “How many test scores do you have ? “); numScores = Integer.parseInt(answer1); Counter Controlled Loop

11

12

13 while (count <=numScores) { String scoreEntered; int score; count = count +1; scoreEntered = JOptionPane.showinputDialog( null, “Enter score ” + count); score = Integer.parseInt(scoreEntered); total = total + score; } average = (double) total/count; JOptionPane.showMessageDialog(null, “The” + “average of the test scores is “ + average); } Counter Controlled Loop

14

15

16 Conclusion of Class 13