Chapter 2:
For Loops
AP Computer Science A
Building Java Programs, 3rd Edition
In this chapter:
Primitive data types
Operators
Variables
Expressions
Primitive
Data Types
Data Types
Type Representation
int 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 multiplication
2 + 2 is 4
5 - 3 is -2
7 * 3 is 21
Dividing with ints
4 / 2 is 2
But…
15 / 4 is not 3.75
You need to cut off what is after the decimal point.
15 / 4 is 3
You are NOT rounding--just leaving out digits.
But…
15.0 / 4 is 3.75
This is because 15.0 is a double, not an int.
Mod (modulus)
% finds the remainder after integer division
x % y is x - (x / y) * y
6 % 2 is 0
5 % 3 is 2
3 % 5 is 3
10 % 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.3333333333333335
3 * 3.2 is 9.600000000000001
char
The space bar ' ' is also valid
A single quote (') as a char is '''
chars, ints, and ASCII Values
int a = ('a' + 'b') / 2; //becomes 97
/*
(97 + 98) / 2
195 / 2
97
*/
'a' - 98 cannot become a char
char c = 'c' - 2; //becomes 'a'
//99 - 2 is 97, which is 'a'
boolean
Symbol Meaning
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Ands && Ors
True/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;
int a = 5;
type
name
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' ≠ 6
false ≠ "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"
Casting
Implicit Casting Explicit Casting
● 5.0 / 2
2 → 2.0
● 'a' * 2
'a' → 97
● 5.0 / (double) 2
2 → 2.0
● (int) 'a' * 2
'a' → 97
● (int) 3.14
3.14 → 3
● (int) 3.5 * 2 + 1
3 * 2 + 1
7
● (int) (3.5 * 2 + 1)
(int) (8)
8
Expressions
Operator Precedence
PrecedenceRank 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 Shorthands
Expression Shorthand
x = 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
(boolean
variable)
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 Loops
for (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 Constants
public 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

For Loops and Variables in Java

  • 1.
    Chapter 2: For Loops APComputer Science A Building Java Programs, 3rd Edition
  • 2.
    In this chapter: Primitivedata types Operators Variables Expressions
  • 3.
  • 4.
    Data Types Type Representation intIntegers 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
  • 5.
  • 6.
    int Addition, subtraction, andmultiplication 2 + 2 is 4 5 - 3 is -2 7 * 3 is 21
  • 7.
    Dividing with ints 4/ 2 is 2 But… 15 / 4 is not 3.75 You need to cut off what is after the decimal point. 15 / 4 is 3 You are NOT rounding--just leaving out digits. But… 15.0 / 4 is 3.75 This is because 15.0 is a double, not an int.
  • 8.
    Mod (modulus) % findsthe remainder after integer division x % y is x - (x / y) * y 6 % 2 is 0 5 % 3 is 2 3 % 5 is 3 10 % 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
  • 9.
    double Accurate to 16places Roundoff error examples (try them, they actually work): 10.0 / 3.0 is 3.3333333333333335 3 * 3.2 is 9.600000000000001
  • 10.
    char The space bar' ' is also valid A single quote (') as a char is '''
  • 12.
    chars, ints, andASCII Values int a = ('a' + 'b') / 2; //becomes 97 /* (97 + 98) / 2 195 / 2 97 */ 'a' - 98 cannot become a char char c = 'c' - 2; //becomes 'a' //99 - 2 is 97, which is 'a'
  • 13.
    boolean Symbol Meaning == equalto != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
  • 14.
    Ands && Ors True/FalseTable 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.
  • 15.
  • 16.
    Initializing Variables The namingconvention 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; int a = 5; type name value
  • 17.
    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' ≠ 6 false ≠ "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"
  • 18.
    Casting Implicit Casting ExplicitCasting ● 5.0 / 2 2 → 2.0 ● 'a' * 2 'a' → 97 ● 5.0 / (double) 2 2 → 2.0 ● (int) 'a' * 2 'a' → 97 ● (int) 3.14 3.14 → 3 ● (int) 3.5 * 2 + 1 3 * 2 + 1 7 ● (int) (3.5 * 2 + 1) (int) (8) 8
  • 19.
  • 20.
    Operator Precedence PrecedenceRank OperatorCategory 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 =
  • 21.
    Operation Shorthands Expression Shorthand x= 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;
  • 22.
  • 23.
    What is aFor Loop? for (int i = 1; i <= 5; i++) { System.out.print(i); } Initialization (statement) Test (boolean variable) Update (statement) Statement(s) for (;;) { System.out.println("Infinite loop!"); }
  • 24.
    How a ForLoop 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
  • 25.
    Nested For Loops for(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
  • 26.
    Class Constants public classClassConstant { 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