3. Boolean Expressions & If Statements
/*
Writer: SP
Date: March 23rd
References: knowt & Fiveable
Unit 3: Boolean Expressions & If Statements */If Statements
Itâs a conditional statement that is used in Java to help control the flow of the program.
For example, your parents tell you, you can only watch a movie if you finish cleaning your room.
Cleaning your room is the condition for you to be able to watch the movie.
You won't get to go if you donât clean your room.
The else statement in the example below is used for what results to produce when the if condition isnât being satisfied.
Ex:
int num1 = 4, num2 = 5;
if( num1 == num2)
System.out.print(âThe numbers are the same.â);
else
System.out.print(âThe numbers arenât the sameâ);If you want to add multiple control structures that only works for a certain statement, you use âelse ifâ
String teacher;
// User Input to get a teacher's value
if (teacher == "Mr. De Wet") {
System.out.println("He is a computer teacher");
}
else if (teacher == "Mr. Lee") {
System.out.println("He is a science teacher");
}
else if (teacher == "Ms. Connolly") {
System.out.println("She is english teacher");
}
else {
System.out.println("I don't know");
}If use two âifâ statement it might not provide an intended work.
int age = 40;
if (age >= 50) {
System.out.println("He is 50s");
}
if (age >= 40) {
System.out.println("He is 40s");
}
if (age >= 30) {
System.out.println("He is 30s");
}
// Output will be He is 30s. Above code doesnât work properly, because if age is greater than or equal to 40, but also it is greater than or equal to 30, thus, it will make an output âHe is 30sâ. Using âelse ifâ statement can correct this issue.
Nested Conditionals
if (condition1 == true) {
if (condition2 == true) {
// Do something)
}
// Do something
}To deal with nested if statement, we need to check outer if statement first, and check the inner side.
A boolean operator asks a question, while an assignment operator executes a command. The boolean operator in this example determines if num1 is equal to num2.
The assignment statement doesnât produce other results.
Boolean Expressions
Expressions | Definition |
|---|---|
== | Equal to (With two primitive types) |
!= | Not equal to |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
The boolean statement produces a truth value based on the comparison that it conducts.
Sometimes, however, we might want to create a more complicated condition. This is referred to as a compound condition. They include at least one boolean operator.
Common Compound Conditions:
&&- and||- or!- not
Ex: Consider the following boolean expression, (X && !Y) || (!X && Y). Which of the following conditions will always cause the expression to evaluate to true.
Answer: || indicate or for both of the expressions. Note that for or, only 1 of the expressions has to be true. Both X and Y have to be true in their conditions for the expression within the brackets to be true. This rules out just the value of just one variable changing because both are impacted. The values of X and Y canât match other. This means that X and Y have to have the opposite truth values.
Ex: What will be the truth value of (! p && ! q) || (p || q)?
Answer: Letâs start off with an expression on the right It will simplify to !p && !q. It doesnât always evaluate to true or false, because we donât know the values of p and q, and itâs contingent on this. The correct answer is that the expression evaluates to false whenever p and q have opposite values.
The way that if statements work and function applies to all control statements! Make sure you really understand how it works!
DeMorganâs Laws are used to help simplify Boolean expressions.
Truth Table for combinations of Boolean Conditions:
A | B | A&&B | A||B | !A | !B | !(A&&B) | !A||!B |
|---|---|---|---|---|---|---|---|
T | T | T | T | F | F | F | F |
T | F | F | T | F | T | T | T |
F | T | F | T | T | F | T | T |
F | F | F | F | T | T | T | T |