Download presentation
Presentation is loading. Please wait.
Published byChristopher Richardson Modified over 9 years ago
1
Some revision
2
Today, we will do some revision on: - ◦ booleans, and ◦ if statements
3
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 true
4
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
5
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 8); true TT TF
6
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 F T TF
7
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?
8
int average = 65; String result; if ( average > 59 ) { result = " 2:1 " ; // then clause } else { result = " 2:2 " ; // else clause } // end if
9
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.
10
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. if ( condition_one ) { then_clause } else { if ( condition_two ) { then_clause } else { else_clause } // end if
11
if ( grade > 70 ) { Result = " Distinction " ; } else { if ( grade > 50 ) { Result = " Pass " ; } else { Result = " Fail " ; } // end if inner outer
12
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?
13
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?
14
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? String word1 = "Java"; String word2 = "Java"; System.out.println("same = " + word1.equals(word2));
15
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.