Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

For Loops and Variables in Java

225 views

Published on

Based on chapter 2 of the textbook "Building Java Programs", 3rd edition. Covers primitive data types, variables, operators, ASCII values for chars, operator precedence, String concatenation, casting, for loops, nested for loops, and class constants.
See a video presentation of this slideshow on my YouTube channel JavaGoddess, at https://www.youtube.com/watch?v=N7SBkMY65gc&t=4s

Published in: Technology
  • Be the first to comment

  • Be the first to like this

For Loops and Variables in Java

  1. 1. Chapter 2: For Loops AP Computer Science A Building Java Programs, 3rd Edition
  2. 2. In this chapter: Primitive data types Operators Variables Expressions
  3. 3. Primitive Data Types
  4. 4. 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
  5. 5. Operators
  6. 6. int Addition, subtraction, and multiplication 2 + 2 is 4 5 - 3 is -2 7 * 3 is 21
  7. 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. 8. 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
  9. 9. 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
  10. 10. char The space bar ' ' is also valid A single quote (') as a char is '''
  11. 11. 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'
  12. 12. boolean Symbol Meaning == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
  13. 13. 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.
  14. 14. Variables
  15. 15. 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
  16. 16. 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"
  17. 17. 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
  18. 18. Expressions
  19. 19. 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 =
  20. 20. 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;
  21. 21. For Loops
  22. 22. 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!"); }
  23. 23. 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
  24. 24. 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
  25. 25. 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

×