some revision. today, we will do some revision on: - ◦ booleans, and ◦ if statements
TRANSCRIPT
Lecture 3.2Some revision
Today, we will do some revision on: -◦ booleans, and ◦ if statements
In today’s lecture
What is stored in the variable v after this code is run?
boolean v = !(20 == 20);
What is stored in the variable q after this code is run?
int d = 18;int f = 20;boolean q = d == f;
What is stored in the variable w after this code is run?
int d = 16;int g = 18;boolean w = d != g;
false
false
true
What is stored in the variable w after this code is run?
boolean d = true;boolean h = true;boolean w = d || h;
What is stored in the variable w after this code is run?
boolean d = true;boolean f = false;boolean w = d && f;
false
true
What is stored in the variable n after this code is run?
boolean c = true;boolean h = false;boolean w = true;boolean n = c || h || w;
What is stored in the variable x after this code is run?
int b = 5;int g = 3;boolean x = (b < 7) || (g > 8);
true
true
T T
T F
What is stored in the variable r after this code is run?
int b = 9;int e = 5;boolean r = (b >= 10) && (e <= 7);
What is stored in the variable s after this code is run?
int a = 9;int h = 2;boolean s = !(a >= 7) && !(h <= 1);
false
false
F T
T F
This is the basic structure of an if....else statement in java:
if ( condition_to_test ) { then_ clause} else { else_clause} // end if (optional comment)
If the Pseudocode was: -◦ if “the average mark is over 59” ◦ then the degree classification is 2.1◦ else the degree classification is 2.2
What would the code be if average has the value 65?
if...then …else statements in java
int average = 65;String result;if ( average > 59 ) { result = "2:1"; // then clause} else { result = "2:2"; // else clause} // end if
if...then…else
if ( condition_one ) { then_clause} else { if ( condition_two ) { then_clause } else { else_clause } // end inner if} // end outer if Anything not caught by the first two conditions will
be caught by the final else.
Nested if
Try this out: - Assume that you can have three grades for a
MSc module: ◦ distinction, ◦ pass or ◦ fail.
A distinction is a grade over 70, a pass is a grade over 50, and a fail is everything else.
How would you code this? Jot down what you think before we look at
the answer on the next slide.
Using else ifif ( condition_one ) { then_clause} else { if ( condition_two ) { then_clause } else { else_clause } // end if} // end if
if ( grade > 70 ) { Result = "Distinction";} else { if ( grade > 50 ) { Result = "Pass"; } else { Result = "Fail"; } // end if} // end if
Solution
inner
outer
Although this would work, any user error could give a result which would seem correct but could cause administrative problems later on.
What kind of errors could cause problems?
There are a few problems with this...
if (grade > 70 && grade < 101) { Result = "Distinction";} etc. ...
There are many different comparison operators that you can use which cover every combination of events.
Imagine that you wanted to know if a car was either 5 or 6 years old. You would use the OR operator ||.
How would you put this into an if condition?
This is where it may be useful to use different operators.
if (carAge == 4 || carAge == 5) { Result = "Buy it";} // end if
Note the == sign which is a way of testing if something is equal to for numeric variables.
Remember how to test String variables?
Using || which means “or”
String word1 = "Java";
String word2 = "Java";
System.out.println("same = " + word1.equals(word2));
You have various pieces of work to finish for next week.
Ensure that jafa is up to date. Read Currie (ch 5). Do the dry run on paper so that you can
have a go at the “mock” test in the workshop next week.
Work through the workbook, ensuring that you can understand all of it. If you don’t understand anything, ask in the workshop.
Homework