#### Exam Objectives Covered

• Describe the looping statements

• Use a "for" loop including an enhanced "for" loop*

• Use a "while" loop

• Use a "do-while" loop

• Compare and contrast the "for," "while," and "do-while" loops

• Develop code that uses "break" and "continue" statements

• Use the decision-making statement (if-then and if-then-else)

• Use the "switch" statement

#### if Statement

• Braces not required

• Recommend always using braces
``````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";
}``````
• Compact alternative: ternary operator (3 operands)
``boolean-expr ? true-value : false-value``
• You can nest them
``````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

• Create a class called "EvenOrOdd"

• Declare an int called "myNumber" and assign it to a value of your choosing

• Inside of an if statement, evaluate whether "myNumber" is even using modulo and comparison
• HINT: This was covered in Section 4
• If true, print to the console, "myNumber is even!"

• Inside of an else statement, print to the console, "myNumber is odd!"

• Test your program with different values for "myNumber" to ensure that it functions correctly

• CHALLENGE: Use a ternary operator instead of an if-else statement

#### switch Statement

• Like if-else, but can make multiple choices

• Execution begins at the first case that matches the variable
• And continues until it reaches a break

• If no case matches, executes the default case (if present)
``````// 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

• Notice how the cases are used here
``````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

• Create a class called "DailyMessage"

• Declare a String called "dayOfTheWeek" and assign the value to the day of the week today

• Create a switch statement that takes the "dayOfTheWeek" String as an argument
• Print "Class day" if the day of the week is Monday, Wednesday, or Friday
• Print "Gym day" if the day of the week is Tuesday or Thursday
• Print "Weekend" if the day of the week is Saturday or Sunday
• Remember that Java is case-sensitive!

#### while Loop

• Braces not required

• Recommend always using braces
``````while (boolean_expr)
// do an action

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

#### Lab 6.3 - Soda on the Wall 1

• Create a class called "SodaOnTheWall1"

• Declare an int called "counter" and assign it to the value of 99

• Use a while loop and "counter" to print this song:
``````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…]``````
• If counter equals 0, print an alternative message:
``````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!``````
• CHALLENGE: How could we get an infinite loop and continue the song correctly?

#### do-while Loop

• Braces not required

• Recommend always using braces

• Will always execute at least once

• Easy to miss that trailing semicolon
``````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

• Create a class called "SodaOnTheWall2"

• Rewrite the "SodaOnTheWall1" class but use a do-while loop instead of a while loop
• HINT: Copy and paste!

• Try running this version while counter is set to an initial value of -1
• Did it behave the way you expected?

#### for Loop

• Most commonly used loop in Java

• Initialization
• Sets initial values, can also include a variable

• Boolean expression
• Loop repeats until false

• Iteration/increment
• Fires just after body of the loop (just before the closing })
• Prior to the next test on the boolean_expr
``````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

• Initialization or iteration can have multiple expressions
• Use commas to separate
``````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

• Create a class called "SodaOnTheWall3"

• Rewrite the "SodaOnTheWall1" class but use a for loop instead of a while loop
• HINT: Copy and paste!

• This version should have the least total lines of code
• Does yours? Why is this?

#### break and continue

• Alter the flow of a loop

• Often used for efficiency reasons
• If you're looping to search for a value, once you find it, stop looping

• Sometimes stand alone, sometimes used with labels

• break – stop executing loop entirely

• continue – stop current iteration and continue with the next

#### 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);
}
}
}``````
• What does this output?

#### 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);
}
}
}``````
• What does this output?