[an error occurred while processing this directive] [an error occurred while processing this directive] AP/CS A Lab 4: if else

University of Washington, AP/CS A

Lab 4: if else

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Today's lab

Goals for this lab:

Conditional Statements

Conditional statements lets your program choose to do different things based on the results of tests, which evaluate certain parts of a program. There are 3 structures that we can use:

if statements

if statements are composed of a test, and some code to execute if that test is true (ex. 2 + 2 = 4 is true).

if (test) {
    statement(s);  // executes if test is true
} 
System.out.println("yay!")  // prints yay, business as usual outside the if  statement

Example:

if (gpa >= 2.0) { 
    System.out.println("Welcome to Mars University!");  // perform this code is gpa >= 2.0
}

Exercise : if statements

What does the following code output?
1
2
3
4
5
x = 5;
if (x < 0) {
    System.out.println("inside if branch!");
} 
System.out.println("outside if branch!");

Output:

outside if branch!

Exercise : if statements

What does the following code execute?
1
2
3
4
5
x = -2;
if (x < 0) {
    System.out.println("inside if branch!");
} 
System.out.println("outside if branch!");

Output:

inside if branch!
outside if branch!

Exercise : if statements

What does the following code execute?
1
2
3
4
5
6
7
8
x = -2;
if (x < 0) {
    System.out.println("inside if branch!");
} 
if (x < -1) {
    System.out.println("inside this branch!");
}
System.out.println("outside if branch!");

Output:

inside if branch!
inside this branch!
outside if branch!

Note: it's possible for any if statement to execute, or not. If you have a couple if statements next to each other, 0 of them, 1 of them, or both of them could execute.

if/else statements

if/else statements are statements that include two branches, one of which is always executed. They allow us to control our program's behavior when the if statement's test evaluates to false.

if (test) {
    statement
} else {              //  implicitly, test is false
    statement
}

Example:

if (gpa >= 2.0) {
    System.out.println("Welcome to Mars University!");  // perform this code is gpa >= 2.0
} else { 
    System.out.println("Please apply again soon.");     // perform this code is gpa < 2.0
}

Exercise : if-else practice

What does the following program output?
1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
   int x = 5;
   if (x < 5) {
      System.out.println("abc");
   } else {
      System.out.println("def");
   }
   System.out.println("ghi");
}

Output:

def
ghi

Exercise : if-else practice

What does the following program output?
1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
   int x = 2;
   if (x < 5) {
      System.out.println("abc");
   } else {
      System.out.println("def");
   }
   System.out.println("ghi");
}

Output:

abc
ghi

Note: With if-else structures, exactly one branch will execute. It is impossible for neither the if branch or the else branch to execute. It is also impossible for both to execute.

nested if/else statements

Nested if/else statements allow you to write code that executes if its test is met, but only in the case that an if statement before it has already evaluated to false.

if (test1) {
   statement  // executes if test1 == true
} else if (test2) {
   statement  // executes if test1 -= false AND test 2 == true
}
Example:
if (gpa >= 2.0) { 
    System.out.println("Welcome to Mars University!");  // perform this code is gpa >= 2.0
} else if (gpa < 0.7) 
    System.out.println(":*(");  // perform this code when gpa < 2.0 and < 0.7 
}
[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]

if/else factoring

Recall that with if/else, exactly 1 branch executes. This make a new kind of redundancy possible!
if (x < 30) {
    a = 2;
    x++;
    System.out.println("CSE 142 TAs are awesome! " + x);
} else {
    a = 2;
    x--;
    System.out.println("CSE 142 TAs are awesome! " + x);
}
Because the red code will happen no matter what (in the if and the else case), it can be factored out:
a = 2;
if (x < 30) {
    x++;
} else {
    x--;
}
System.out.println("CSE 142 TAs are awesome! " + x);
[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]