[an error occurred while processing this directive]
[an error occurred while processing this directive]
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:
- use
if
, else if
, and else
to have
different branches of execution
- Where you see this icon, you can click it to check the problem in
Practice-It!
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
else
statements
else if
statements
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);
}
System.out.println("yay!")
Example:
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!");
}
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 {
statement
}
Example:
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!");
} else {
System.out.println("Please apply again soon.");
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
} else if (test2) {
statement
}
Example:
if (gpa >= 2.0) {
System.out.println("Welcome to Mars University!");
} else if (gpa < 0.7)
System.out.println(":*(");
}
[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]