New Tools And Workshop Mod & For Loops
Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates to 0 evaluates to 2
Incrementing and Decrementing Often you want to increase or decrease an int value by 1 We’ve done it like this: int i = 5; i = i + 1;// now i has a value of 6 Java has a shortcut: int y = 5; y++;// now i has a value of 6 y--;// now i has a value of 5
For loops Often you want to repeatedly execute a piece of code Many uses Example: for(int i = 0; i < 10; i++){ System.out.println(i); } New Keyword! Parenthesis encompass the 3 conditions Curly braces encompass the code to be looped over
3 conditions of a for loop for(int i = 0; i < 10; i++){ System.out.println(i); } 1.declare and define a counter variable 2. boolean condition, checked every iteration 3. update counter variable every iteration
Let’s do some coding…