chapter 2 building java programs

26
Chapter 2: For Loops AP Computer Science A Building Java Programs, 3rd Edition

Upload: pokequesthero

Post on 06-Apr-2017

42 views

Category:

Technology


0 download

TRANSCRIPT

Chapter 2: For Loops

AP Computer Science A

Building Java Programs, 3rd Edition

In this chapter:Primitive data types

Operators

Variables

Expressions

For loops

Primitive Data Types

Data TypesType Representationint Integers from negative to positive 2 -1

double Real numbers from negative to positive 10

char Single text characters in single quotes

boolean True/false values

String A line of text in double quotes

31

308

Primitive data types

Objects

Operators

int

Addition, subtraction, and multiplication2 + 2 is 45 - 3 is -27 * 3 is 21

Dividing with ints4 / 2 is 2

But…15 / 4 is not 3.75You need to cut off what is after the decimal point.15 / 4 is 3You are NOT rounding--just leaving out digits.

But…15.0 / 4 is 3.75This is because 15.0 is a double, not an int.

Mod (modulus)

% finds the remainder after integer divisionx % y is x - (x / y) * y

6 % 2 is 05 % 3 is 23 % 5 is 310 % 10 is 0

integer division

Mod Hacks● If x % y == 0 is true, then x is

divisible by y.● If x % 2 == 0, then x is even. If it is

1, then x is odd.● x % 10 finds the last "a" number

of digits of x● x / 10 % 10 finds the "a"th digit of

x starting from the left

a

a

double

Accurate to 16 places

Roundoff error examples (try them, they actually work):

10.0 / 3.0 is 3.33333333333333353 * 3.2 is 9.600000000000001

char

The space bar ' ' is also validA single quote (') as a char is '\''

chars, ints, and ASCII Valuesint a = ('a' + 'b') / 2; //becomes 97/*(97 + 98) / 2195 / 297*/

'a' - 98 cannot become a char

char c = 'c' - 2; //becomes 'a'//99 - 2 is 97, which is 'a'

booleanSymbol Meaning== equal to

!= not equal to

> greater than

>= greater than or equal to

< less than

<= less than or equal to

Ands && OrsTrue/False Table for && True/False Table for ||

true false

true true false

false false false

true false

true true true

false true false

Short-circuited evaluation: 1. If the first thing in an "and" statement is false, the entire "and" statement is

assumed to be false. 3 > 5 && 1 / 0 == 0 is assumed false.2. If the first thing in an "or" statement is true, the entire "or" statement is

assumed to be true. 3 < 5 || 1 / 0 == 0 is assumed true.

Variables

Initializing Variables

The naming convention for variables isTheSameAsForMethods.

The equal sign means "to get the value of" or "becomes"

int b = 4;int b = 2; //compile error

int c = 4;c = 2; //correct

int a = 5;

typename value

String Concatenation"this " + "that" becomes "this that""This statement is " + true becomes "This statement is true""I am number " + 1 becomes "I am number 1"

"myVariable" ≠ myVariable'6' ≠ 6false ≠ "false"

4 + 2 + " is my number" becomes "6 is my number""" + 4 + 2 + " is my number" becomes "42 is my number"

"You're number " + 1 + 2 becomes "You're number 12""You're number " + (1 + 2) becomes "You're number 3"

CastingImplicit Casting Explicit Casting● 5.0 / 2

2 → 2.0● 'a' * 2

'a' → 97

● 5.0 / (double) 22 → 2.0

● (int) 'a' * 2'a' → 97

● (int) 3.143.14 → 3

● (int) 3.5 * 2 + 13 * 2 + 17

● (int) (3.5 * 2 + 1)(int) (8)8

Expressions

Operator PrecedencePrecedence Rank

Operator Category

Example

1 Parentheses ( )

2 Unary minus sign in front of numbers

3 Casting (int), (char), (double), 5 → 5.0, 'a' → 97

4 Multiplicative *, /, %

5 Additive + for adding, -, + for concatenation

6 Relational <, <=, >, >=

7 Equality ==, !=

8 And &&

9 Or ||

10 Assignment =

Operation ShorthandsExpression Shorthandx = x + 3; x += 3;

x = x - 4; x -= 4;

x = x * 2; x *= 2;

x = x / 5; x /= 5;

x = x + 1; //incrementing x++; x += 1;

x = x - 1; //decrementing x--; x -= 1;

For Loops

What is a For Loop?

for (int i = 1; i <= 5; i++) { System.out.print(i);}

Initialization(statement)

Test(booleanvariable)

Update (statement)

Statement(s)for (;;) { System.out.println("Infinite loop!");}

How a For Loop Runs

for (int i = 1; i <= 5; i++) {

System.out.print(i);

}

Do the initialization

Is the test true?

Yes

No

Exit for loop

Do the statements inside

Do the update

Nested For Loopsfor (int i = 1; i <= 50; i++) {

for (int j = 1; j <= 50; j++) { System.out.print("*"); } //prints out one line

System.out.println();

} //repeats the line

i j

1 1

2

3

2 1

2

3

3 1

2

3

Class Constantspublic class ClassConstant {

public static final int SIZE = 20; //final means it cannot be changed after declaring

public static void main(String[] args) { for (int i = 1; i <= SIZE; i++) { for (int j = 1; j <= SIZE; j++) { System.out.print("*"); } System.out.println(); } }}

Class constant naming convention: ALL_CAPS_WITH_UNDERSCORES