Using break and continue

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

Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 LOOPS. 2 Repetitions while Loops do-while Loops for Loops break and continue.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Control Statements.
Introduction to Java Programming, 4E Y. Daniel Liang.
INF 523Q Chapter 3: Program Statements (Examples).
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
CMT Programming Software Applications Dr. Xiaohong Gao Repetitions and Methods Week 4.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 4 Loops.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Starting Out with Java: From Control Structures through Objects
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Control Statements.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Iterative Statements Introduction The while statement The do/while statement The for Statement.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
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.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 06 Title: Iterative/Repetitive Control Structures Reference: MalikFarrell, chap 1, Liang Ch 4.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
補充教材. 主題 JDK 與 Tomcat 5.5.x 的安裝 迴圈 MySQL Workbench.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
using System; namespace Demo01 { class Program
Chapter 5 Control Statements
Chapter 4 Loops DDC 2133 Programming II.
Repetition-Sentinel,Flag Loop/Do_While
Counted Loops.
TK1114 Computer Programming
Chapter 4 Loops Case Studies
Chapter 4 Control structures and Loops
Chapter 5 Control Statements
Decision statements. - They can use logic to arrive at desired results
CSS161: Fundamentals of Computing
Nested Loop Review and Two-Dimensional Arrays
The for-loop and Nested loops
Chapter 3 Control Statements
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
class PrintOnetoTen { public static void main(String args[]) {
Presentation transcript:

Using break and continue Examples for using the break and continue keywords: Example 3.5: TestBreak.java Example 3.6: TestContinue.java

// TestBreak.java: Test the break keyword in the loop,15 public class TestBreak { /** Main method */ public static void main(String[] args) { int sum = 0; int item = 0; while (item < 5) { item ++; sum += item; //if (sum >= 6) break; } System.out.println("The sum is " + sum); } }

// TestContinue.java: Test the continue keyword,13 public class TestContinue { /** Main method */ public static void main(String[] args) { int sum = 0; int item = 0; while (item < 5) { item++; if (item == 2) continue; sum += item; } System.out.println("The sum is " + sum);

3.7 Finding the Sales Amount You have just started a sales job in a department store. Your pay consists of a base salary and a commission. The base salary is $5,000. The scheme shown below is used to determine the commission rate. Sales Amount Commission Rate $0.01–$5,000 8 percent $5,000.01–$10,000 10 percent $10,000.01 and above 12 percent Your goal is to earn $30,000 in a year. Write a program that will find out the minimum amount of sales you have to generate in order to make $30,000.

// FindSalesAmount.java: Find the sales amount to get the // desired commission.Base salary 5000,earn30000 a yr. import javax.swing.JOptionPane; public class FindSalesAmount { /** Main method */ public static void main(String[] args) { // The commission sought final double COMMISSION_SOUGHT = 25000; final double INITIAL_SALES_AMOUNT = 0; double commission = 0; double salesAmount = INITIAL_SALES_AMOUNT; do { // Increase salesAmount by 1 cent salesAmount += 0.01;

// Compute the commission from the current salesAmount; if (salesAmount >= 10000.01) commission = 5000 * 0.08 + 5000 * 0.1 + (salesAmount - 10000) * 0.12; else if (salesAmount >= 5000.01) commission = 5000 * 0.08 + (salesAmount - 5000) * 0.10; else commission = salesAmount * 0.08; } while (commission < COMMISSION_SOUGHT); // Display the sales amount String output = "The sales amount $" + (int)(salesAmount * 100) / 100.0 + "\nis needed to make a commission of $" + COMMISSION_SOUGHT; JOptionPane.showMessageDialog(null, output, "Example 3.7 Output", JOptionPane.INFORMATION_MESSAGE); System.exit(0); }

3.8 Displaying a Pyramid of Numbers In this example, you will use nested loops to print the following output: 1 212 32123 4321234 543212345 Your program prints five lines. Each line consists of three parts. The first part comprises the spaces before the numbers; the second part, the leading numbers, such as 3 2 1 on line 3; and the last part, the ending numbers, such as 2 3 on line 3.

// PrintPyramid.java: Print a pyramid of numbers public class PrintPyramid { /** Main method */ public static void main(String[] args) { final int NUM_OF_LINES = 5; for (int row = 1; row <= NUM_OF_LINES; row++) { // Print leading spaces for (int column = 1; column <= NUM_OF_LINES - row; column++) System.out.print(" "); // Print leading numbers for (int num = row; num >= 1; num--) System.out.print(num); // Print ending numbers for (int num = 2; num <= row; num++) // Start a new line System.out.println(); }

3.9 Displaying Prime Numbers This example displays the first 50 prime numbers in five lines, each of which contains 10 numbers. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. The problem can be broken into the following tasks: For number = 2, 3, 4, 5, 6, ..., test whether the number is prime. Determine whether a given number is prime. Count the prime numbers. Print each prime number, and print 10 numbers per line.