building java programs chapter 2

12
Building Java Programs Chapter 2 PRIMITIVE DATA AND DEFINITE LOOPS

Upload: dalton

Post on 07-Jan-2016

29 views

Category:

Documents


2 download

DESCRIPTION

Building Java Programs Chapter 2. Primitive Data and Definite Loops. Review. Primitive data types: boolean , char, double, int Expressions and literals A rithmetic operators: +, -, *, /, % Precedence and casting Variables String concatenation and mixing types - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Building Java Programs Chapter 2

Building Java ProgramsChapter 2PRIMITIVE DATA AND DEFINITE LOOPS

Page 2: Building Java Programs Chapter 2

2

Review• Primitive data types: boolean, char, double, int

• Expressions and literals

• Arithmetic operators: +, -, *, /, %

• Precedence and casting

• Variables

• String concatenation and mixing types

• Increment/decrement operators

• For loops

Page 3: Building Java Programs Chapter 2

3

Review What output by the following set of statements? int x = 12; double y = 24; System.out.println("x divided by y = " + x / y);

A. x divided by y = 0 B. x divided by y = 12 / 24 C. x divided by y = 0.5 D. x divided by y = + 12 / 24 E. Nothing – this does not compile.

Page 4: Building Java Programs Chapter 2

4

for loop syntaxfor (initialization; test; update) { statement; statement; ... statement;}

Perform initialization once.

Repeat the following:◦ Check if the test is true. If not, stop.

◦ Execute the statements.

◦ Perform the update.

body

header

Page 5: Building Java Programs Chapter 2
Page 6: Building Java Programs Chapter 2

6

Repetition with for loops

We see redundancy when trying to perform a task more than once:

System.out.println(“Let’s go…");System.out.println(“Seahawks!");System.out.println(“Seahawks!");System.out.println(“Seahawks!");System.out.println(“Go home, Peyton!");

Java's for loop statement performs a task many times.

System.out.println(“Let’s go…");

// Repeat 3 timesfor (int i = 0; i <= 2; i++) {System.out.println(“Seahawks!");}

System.out.println(“Go home, Peyton!");

Page 7: Building Java Programs Chapter 2

7

Volunteers

Initializer Condition tester Console UpdaterVariable

Page 8: Building Java Programs Chapter 2

for (int candies = 0; candies < 5; candies++) { System.out.println("I ate " + candies + " candies");}

Page 9: Building Java Programs Chapter 2

9

Practice-It Self-check: 2.12, 2.15, 2.16, 2.18, 2.20

Page 10: Building Java Programs Chapter 2

10

Nested for loopsOne for loop can be nested inside another.

for (int outerLoop = 0; outerLoop < 2; outerLoop++) {

System.out.println("OuterLoop variable = " + outerLoop);

for (int innerLoop = 5; innerLoop > 0; innerLoop = innerLoop / 2) {

System.out.println("InnerLoop variable = " + innerLoop);

}

}

Page 11: Building Java Programs Chapter 2

11

Nested for loopsfor (int outerLoop = 0; outerLoop < 2; outerLoop++) {

System.out.println("OuterLoop variable = " + outerLoop);

for (int innerLoop = 5; innerLoop > 0; innerLoop = innerLoop / 2) {

System.out.println("InnerLoop variable = " + innerLoop);

}

}

OuterLoop variable = 0◦ InnerLoop variable = 5◦ InnerLoop variable = 2◦ InnerLoop variable = 1

OuterLoop variable = 1◦ InnerLoop variable = 5◦ InnerLoop variable = 2◦ InnerLoop variable = 1

Page 12: Building Java Programs Chapter 2

12

Homework Self-check: 2.12, 2.15, 2.16, 2.18, 2.20, 2.26, 2.27

Exercise: 2.2, 2.3

If you finish the homework before class is over:

• Review for your quiz on Wednesday.

• Read BJP 2.4.

• Do extra Practice-Its.