Java - Tutorials

Setting Up The Java Environment
Java Environment
Overveiw Of Java Programming
Java Data Types
Java Operators
Java Flow Control

Exam Objectives Covered


if Statement

if (boolean-expr)
  // do an action

if (boolean-expr) {
  // do an action
  // do another action
}

// remember: = is assignment, == is comparison
if (x = 5) {   // ERROR: should be x == 5
  // do something
}

if-else Statement

if (boolean-expr) {
  // do an action
  // do another action
}
else {
  // do something else
}
public static void main(String[] args) {
  String str = (args.length == 1) ? "1" : "not 1";
}
boolean-expr ? true-value : false-value
double pay(int hours, double rate) {

  double basePay = hours * rate;
  double multiplier = 0;  // why declare it here?
    
  if (hours > 50) {
    multiplier = 1.75;
  }
  else if (hours > 40) {
    multiplier = 1.5;
  }
  else {
    multiplier = 1;
  }
  return basePay * multiplier;
}

Lab 6.1 - Even or Odd


switch Statement

// NOTE: can only switch on int or smaller (byte, short, int, char) // as well as String and enums (more on enums later)
switch (variable) {
  case value1:
    // statements
    break;
  
  case value2:
    // statements
    break;
  
  default:
    // statements
}

switch Statement - Example

void determineRange(int i) {
  switch (i) {
    case 1: case 2: case 3:
      System.out.println("i is between 1 and 3");
      break;
    case 4: case 5: case 6:
      System.out.println("i is between 4 and 6");
      break;
    case 7: case 8: case 9:
      System.out.println("i is between 7 and 9");
      break;
    default:
      System.out.println("i is out of range");
  }
}

Lab 6.2 - Daily Message


while Loop

while (boolean_expr)
  // do an action

while (boolean_expr) {
  // do an action
  // do another action
}

Lab 6.3 - Soda on the Wall 1

99 bottles of soda on the wall!
99 bottles of soda!
Take one down and pass it around,
98 bottles of soda on the wall!
[and so on…]
No more bottles of soda on the wall,
no more bottles of soda!
Go to the store and buy some more,
99 bottles of soda on the wall!

do-while Loop

do 
  // do an action
while (boolean_expr);

do {
  // do an action
  // do another action
} while (boolean_expr);

Lab 6.4 - Soda on the Wall 2


for Loop

for (initialization; boolean_expr; iteration) {
  // statements
}

for Loop - Examples

// i declared outside for block, because we need it later
int i;
for (i = 0; i < 5; i++) {
  System.out.println("i is now " + i);
}
...
System.out.println("i after for loop is " + i);  // i still in scope
// i is declared in the scope of the for block (typical case)
for (int i = 0; i < 5; i++) {
  System.out.println("i is now " + i);
}
// i is now out of scope (see notes)

for Loop - Multiple Initializations

for (int s = 0, int t = 1; s + t < 30; s++, t+=3) {
  System.out.println("s is " + s + ", t is " + t);
}
s is 0, t is 1
s is 1, t is 4
s is 2, t is 7
s is 3, t is 10
s is 4, t is 13
s is 5, t is 16
s is 6, t is 19
s is 7, t is 22

Lab 6.5 - Soda on the Wall 3


break and continue


break - Example

class BreakOut {
  
  public static void main(String args[]) {
    int limit = 3;
    for (int j = 0; j < 5; j++) {
      if (j == limit)
        break;  // stop loop entirely
      System.out.println("j is " + j);
    }
  }
}

continue - Example

class ContinueOn {
  
  public static void main(String args[]) {
    int limit = 3;
    for (int j = 0; j < 5; j++) {
      if (j == limit)
        continue;  // stop current iteration and continue with next
      System.out.println("j is " + j);
    }
  }
}
Exam Objectives Covered
if Statement
if-else Statement
Lab 6.1 - Even or Odd
switch Statement
Lab 6.2 - Daily Message
while Loop
Lab 6.3 - Soda on the Wall 1
do-while Loop
Lab 6.4 - Soda on the Wall 2
for Loop
Lab 6.5 - Soda on the Wall 3
break and continue