cs 6

Chapter 6: More Conditionals and Loops

Overview

  • Focuses on:

    • The switch statement

    • The conditional operator

    • The do loop

    • The for loop

    • Using conditionals and loops with graphics

    • Graphic transformations


The Switch Statement

  • The switch statement allows decision-making based on the evaluation of an expression, matching its result with predefined cases:

    • General syntax:

      switch (expression) {
      case value1:
      statement-list1
      case value2:
      statement-list2
      case value3:
      statement-list3
      // ...
      }
    • It evaluates the expression and transfers control to the statements of the matching case.

    • A default case can be added using the reserved word default, executed if no cases match.

Break Statement

  • A break statement is often used within case statements:

    • Causes control to exit the switch structure.

    • If omitted, control continues to the next case (fall-through behavior).

Example Usage of Switch

  • Example switch statement handling grade categories:

    switch (category) {
    case 10:
    System.out.println("a perfect score. Well done.");
    break;
    // Additional cases...
    default:
    System.out.println("not passing.");
    }

Valid Data Types for Switch

  • Allowed types for a switch expression include:

    • Integers

    • Characters

    • Enumerated types

    • Strings

  • Float types are not allowed, and only equality checks are valid (no relational checks).


The Conditional Operator

  • Evaluates to one of two expressions based on a boolean condition:

    • Syntax:

      condition ? expression1 : expression2;
  • Similar to an if-else, but returns a value based on the condition's truth.

  • Example:

    larger = (num1 > num2) ? num1 : num2;
  • Can simplify printing based on conditions:

    System.out.println("Your change is " + count + ((count == 1) ? "Dime" : "Dimes"));

Quick Check Example

  • The logic:

    if (val <= 10)
    System.out.println("It is not greater than 10.");
    else
    System.out.println("It is greater than 10.");
  • Can be simplified using the conditional operator:

    System.out.println("It is" + ((val <= 10) ? " not" : "") + " greater than 10.");

The do Statement

  • Syntax:

    do {
    statement-list;
    } while (condition);
  • Executes the statement-list once before evaluating the condition, continuing while it's true.

Example of do Loop

  • Example demonstrating a typical do loop for counting:

    int count = 0;
    do {
    count++;
    System.out.println(count);
    } while (count < 5);

The for Statement

  • Syntax:

    for (initialization; condition; increment) 
    statement;
  • Initialization occurs once before the loop, executing until the condition fails.

Example of for Loop

  • Example for counting from 1 to 5:

    for (int count = 1; count <= 5; count++)
    System.out.println(count);
  • Demonstrates control based on calculated conditions, performing increments at the end of each iteration.


Using Loops and Conditionals with Graphics

  • Graphics can be enhanced by combining conditionals and loops:

    • Example applications include Bullseye.java and Boxes.java.


Graphic Transformations

  • JavaFX transforms provide visual changes to nodes, such as:

    • Translation: Changes position along axes.

    • Scaling: Adjusts size of nodes.

    • Rotation: Modifies orientation around a pivot point.

    • Shearing: Alters angles between axes.

Shape Transformation Examples

  • Translation Example:

    Rectangle rec2 = new Rectangle(x, y, width, height);
    rec2.setTranslateX(70);
    rec2.setTranslateY(10);
  • Scaling Example:

    imgView2.setScaleX(0.7);
    imgView2.setScaleY(0.7);
  • Rotation Example:

    rec.setRotate(40);
  • Shear Example:

    imgView.getTransforms().add(new Shear(0.4, 0.2));
  • Transformations on Groups: When applied to a group, all child nodes inherit the transformation, impacting collective behavior significantly. --

Conclusion

  • Chapter 6 emphasized:

    • The switch statement

    • The conditional operator

    • The do loop

    • The for loop

    • Using conditionals and loops with graphics

    • Graphic transformations

By mastering these concepts, improved program design and visual rendering techniques can be implemented.

robot