Boolean Expressions

  • represent logic and tell whether something is true or false
  • operators used for boolean
  • logical operators
    • && means and
    • || means or
    • ! means not
  • De Morgan's Law help simplify boolean expressions
    • two laws to it
    • truth tables are a good war to visualize boolean expressions
  • to compare objects, use the == for aliases for the same objects or to see if it is null or .equals() to see if the attribute is the same for two objects

Iteration

  • simplifies code by allowing it to repeat itself
  • while loops have two portions: the boolean expression and the brackets that have some code in them
    • the boolean expression is checked before the loop starts
      • every time the loop ends and is about to start anew
    • changes conditions again and again until returns false and ends the while loop
    • iterates over numerous elements
  • for loops has three portions: initialization, test condition, and change
    for (int x = 1; x <= 5; x++) {
      System.out.println(x);
    }
  • when the loop condition is false, execution will continue at the next statement after the body of the loop.
  • strings can also be manipulated through the use of iteration ``` String name = "CodeCodeCode";

for (int i = 0; i < name.length(); i+=2) { System.out.println(name.substring(i,i+2)); }

- nested iteration is where there is a loop within a loop
    - similar to nested conditionals

for (int row = 0; row < 5; row ++) { for (int column = 0; column < 4; column++) { System.out.print('*'); } System.out.println(); }

- there are also nested while loops
    - aren't very practical though 
- for each loops are similar to for loops 
    - three portions to it
        - array, item, dataType

public class ForEachLoops {

public static void main(String[] args) {

        // create an array
    int[] data = {2, 10, 5, 12};

        // for each loop 
    for (int number: data) {
    System.out.println(number);

}

} } ```

  • known as the enhanced for loops

Writing Classes

  • class is a blueprint to create objects
  • object is the instances of a class
  • public access means anyone can access the class
  • private means that there is restricted access to the class
  • constructors purpose is to initialize instance variables when object's called
    • set an initial object state and initial instance variables
    • default constructors has no parameters
  • three steps to creating and calling a method
    • declare object of the class in the main method
    • call the method (whatever method you want)
    • write the method's header and body code
  • parameters used when creating a method
    • when method is called, pass parameters that are then saved as parameter variables