This week we have dealt with Boolean expressions and if-else statements or selection statements. You will however, need to know some information we learned last week. Here is a quick run down on what you should know this week. You are expected to know everything from the previous labs very well. If you had any difficulties then please consult a tutor, TA, or the professor so that they might go over it with you. Please also feel free to go back to previous lab and redo parts you had difficulties with. Practice makes perfect :).
boolean a = true; boolean b = false;The above are two Boolean values. To make a Boolean expression you are given the following operators (which you must memorize):
| Symbol | Name | Explanation | Examples |
|---|---|---|---|
| && | AND | Both sides of the expression must be true for it to evaluate to true. |
true && true == true; true && false == false; false && true == false; false && false == false; |
| || | OR | One side of the expression must be true for it to evaluate to true. |
true || true == true; true || false == true; false || true == true; false || false == false; |
| ! | NOT | This negates the expression. E.g. it makes false statements true, and true statements false |
!true == false; !false == true; |
| > | Greater Than | This checks to see if the left expression is greater than the right expression |
3 > 4 == false; 4 > 4 == false; 4 > 3 == true; |
| < | Less Than | This checks to see if the left expression is less than the right expression |
3 < 4 == true; 4 < 4 == false; 4 > 3 == false; |
| <= and >= | Less Than or Equals to and Greater Than or Equal To | Similar to above except that equality will also evaluate to true |
3 < 4 == true; 4 <= 4 == true; 4 > 3 == false; 'a' < 'b' == true; 'a' <= 'a' == true; 'a' > 'd' == false; |
| == | Equality | This checks to see if the left expression is equal to the right expression |
3.1337 == 4.1337 == false; 4.1337 == 4.1337 == true; |
| .equals | Equality | This is used with classes and objects such as String to check if the contents are equal |
String aString = "Hello";
aString.equals("Hello") == true;
aString.equals("No") == false;
|
So some example expressions would be:
boolean c = A || B
boolean d = A && B
boolean e = !A
boolean F = C || B && (D || !(B && C))
You can also use Boolean expressions when comparing integers. For example:
boolean g = 4 == 3 || 5 == 5;
Even with variables:
int aNumber1 = 1;
int aNumber2 = 2;
boolean h = aNumber1 == aNumber2;
boolean i = aNumber1 < aNumber2;
boolean j = !(aNumber1 > 2) || (aNumber2 < aNumber1)
If you do not understand one or more of the above statements then please consult the professor, a tutor, or a TA immediately, as this will become a critical part of the course.
if(boolean expr)
{
// code (executes if
// above boolean expr
// is true)
}
|
if(boolean expr)
{
// code (executes if
// above boolean expr
// is true)
} else
{
// code (executes if
// above boolean expr
// is false)
}
|
if(boolean expr)
{
// code (executes if
// above boolean expr
// is true)
} else if (boolean expr)
{
// code (executes if
// above boolean expr
// is true)
} ...
{
// infinitely many
// else if statements
} else
{
// code (executes if
// all above boolean expr
// is false)
}
|
if( 20 < 10)
{
System.out.println("This code will never execute");
} else
{
System.out.println("This code will always execute");
}
if( 10 < 20)
{
System.out.println("This code will always execute");
} else if (1 < 2)
{
System.out.println("This code will never execute");
} else
{
System.out.println("This code will never execute");
}
if (condition_1)
{
if (condition_a)
{
// this code is exectued if both condition_1
// and condition _a are true
} else
{
// this code is executed if condition_1 is true
// and condition_a is false
}
} else
{
if (condition_b)
{
// this code is executed if condition_1 is false
// and condition_b is true
} else if (condition_c)
{
// this code is executed if condition_1 is false
// and condition_b is false and condition_c is true
} else
{
// this code is executed if condition_1 is false
// and condition_b is false and condition_c is false
}
}