Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

9-Jun-14 Enum s (and a review of switch statements)
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
Chapter 6  Iteration 1 Chapter 6 Iteration. Chapter 6  Iteration 2 Chapter Goals  To be able to program loops with while and for (sometimes do ) statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. 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,
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 1 – Introduction ( ปรับปรุง )
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
If Statement if (amount
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Decisions Bush decision making.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Classes - Intermediate
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 7 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 9 Repetition.
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Selected Topics From Chapter 6 Iteration
LOOPS.
Chapter 5 Repetition.
Chapter Three - Implementing Classes
Chapter Six - Iteration
Chapter 9 Control Structures.
QUIZ 5 – RESULT.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Indefinite loop variations
Building Java Programs
Chapter 5 – Decisions Big Java by Cay Horstmann
6.2 for Loops Example: for ( int i = 1; i
Presentation transcript:

Random Number Generation public class Hand { private int card1; private int card2; private int card3; private Random rand; public static final int NO_CARD = 0; … private int getNewCard() { return rand.nextInt(13) + 1; } public Hand() { rand = new Random(); card1 = getNewCard(); card2 = getNewCard(); card3 = NO_CARD; }

Switch/Case Statement public String getCardName(int cardValue) { String cardName = "”; switch(cardValue) { case 1: cardName = "Ace"; break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: cardName = cardValue + ""; break; case 11: cardName = "Jack"; break; case 12: cardName = "Queen"; break; default: cardName = "King"; } return cardName; }

Chapter 6 Iteration/Looping

A while statement executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement Most commonly, the statement is a block statement (set of statements delimited by { } ) while Loops

while Loop Flowchart

while Loop Examples

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 6.1 The while Statement

Example: int years = 0; while (years < 20) { double interest = balance * rate / 100; balance = balance + interest; } Loop runs forever — must kill program Common Error: Infinite Loops

Example: int years = 20; while (years > 0) { years++; // Oops, should have been years-- double interest = balance * rate / 100; balance = balance + interest; } Loop runs forever — must kill program Common Error: Infinite Loops

Example: for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop when a variable runs from a starting value to an ending value with a constant increment or decrement for Loops

Syntax 6.2 The for Statement

for Loop Flowchart

for Loop Examples

Execution of a for Loop

Rewrite as a for loop. Answer: int i = 1; while (i <= n) { double interest = balance * rate / 100; balance = balance + interest; i++; } Question

How many times does the following for loop execute? for (i = 0; i <= 10; i++) System.out.println(i * i); Answer: Question

A missing semicolon: sum = 0; for (i = 1; (i <= 10) i++); sum = sum + i; System.out.println(sum); A semicolon that shouldn’t be there: sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System.out.println(sum); Common Errors: Semicolons