Many methods that you write in programming will need to make some sort of decision where they will do one thing in some situations, and a different thing in other situations.
In programming, we make decisions using an if statement also known as a conditional statement. Here is an example of how we can use an if statement in java to choose between several different options.
1
2
3
4
5
6
7
8
/** Determines whether a student failed a test based on marks out of 100 */
public boolean didStudentFail(int marks) {
if (marks < 55) {
return true;
} else {
return false;
}
}
The command return
in this code represents the ANSWER that the method provides. Note the syntax - the parentheses ()
, curly brackets {}
, and semi-colons ;
are all important to the correct functioning of this code.
if statements always follow the same format:
1
2
3
4
5
if (/*test that results in true or false*/) {
//code that only runs when the test is true
} else {
//code that runs if the test is false (optional!)
}
You can stack multiple mutually exclusive if statements like so:
1
2
3
4
5
6
7
8
9
10
11
12
/** returns A, B, C, or F based on marks out of 100*/
public String getGrade(int marks) {
if (marks < 55) {
return "F";
} else if (marks < 70) {
return "C";
} else if (marks < 85) {
return "B";
} else {
return "A";
}
}
What is the name of this method?
getGrade
What are the String literals in this code?
“A”, “B”, “C”, and “F”.
If the student earns 82 marks, what will their grade be?
“B”
An operator is a symbol that is used in programming to evaluate something - for example, +
is an operator that can be used to add two numbers together. A logical operator is an operator that results in true
or false
. You have already seen this several times above, using the <
and >
operators. Here are all of the logical operators:
Operator | Code Sample | ||
---|---|---|---|
Less Than ( |
|
||
Less Than or Equal ( |
|
||
Equals ( |
|
||
Not Equal to ( |
|
||
And ( |
|
||
Or ( |
|
||
Not ( |
|
Often, you will have a variable or parameter that represents a true
or false
value and you will want to test that value with an if statement. In those situtations it is best to use the variables DIRECTLY in the if statement, without using ==
, as shown below. It works to do something like if (isSunny == true)
but it is extra work and can lead to errors that are hard to find if you forget an =
sign.
1
2
3
4
5
6
7
8
9
10
// the following method will return true if it is sunny OR if it is not a workday
public boolean inAGoodMood(boolean isSunny, boolean isWorkday) {
if (isSunny) { // if it is sunny this will be true
return true;
} else if (! isWorkday) { // the ! is "not" so this means if it is not a workday
return true;
} else {
return false;
}
}
Write a method called sumOrZero
(including the full signature!) that will take two integers as parameters, and return their sum, UNLESS the two integers are the same, in which case it will return 0. Note that you can use the test a == b
to check if two variables a
and b
are equal.
1
2
3
4
5
6
7
public int sumOrZero(int a, int b) {
if (a == b) {
return 0;
}else {
return a + b;
}
}
After the code below has finished running, what will be the value of b1, b2, and b3?
1
2
3
4
5
int a = 17;
int b = 12;
boolean b1 = (a <= 17);
boolean b2 = (b > 12);
boolean b3 = b1 || b2;
b1 is true, b2 is false, b3 is true
After the code below has finished running, what will be the value of b1, b2, and b3?
1
2
3
4
5
int a = 17;
int b = 12;
boolean b1 = (a == 17);
boolean b2 = ! (b > 12);
boolean b3 = b1 && b2;
b1 is true, b2 is true, b3 is true