CONDITIONALS AND CONTROL FLOW
to write Java programs that can follow different sets of instructions depending on the values that we provide to them. This is called control flow.
- There are three Boolean operators that we will explore. Let’s start with the first one: and.
1. The and operator is represented in Java by &&
.2. It returns a boolean value of true
only when the expressions on both sides of &&
are true.Ex: System.out.println(true && true);
- The second Boolean operator that we will explore is called or.
1. The or operator is represented in Java by ||
.2.It returns a Boolean value of true
when at least one expression on either side of ||
is true.Ex: System.out.println(true || false);
- The final Boolean operator we will explore is called not.
1. The not operator is represented in Java by !
.
2. It will return the opposite of the expression immediately after it. It will return false
if the expression is true, and true
if the expression is false.
Ex: System.out.println(!false);
System.out.println( !(5>=1) );
NOTES: The three Boolean operators &&
, ||
, and !
can also be used together and used multiple times to form larger Boolean expressions, just like numerical operators, Boolean operators follow rules that specify the order in which they are evaluated. This order is called Boolean operator precedence.
- The precedence of each Boolean operator is as follows:
1.
!
is evaluated first
2.
&&
is evaluated second
3.
||
is evaluated third
- In Java, the keyword if is the first part of a conditional expression.
1. It is followed by a Boolean expression and then a block of code. If the Boolean expression evaluates to true
, the block of code that follows will be run.
Ex: if (9 > 2) {
System.out.println("Control flow rocks!");
}
In the example above, 9 > 2
is the Boolean expression that gets checked. Since the Boolean expression “9
is greater than 2
“ is true
, Control flow rocks!
will be printed to the console.
We could write a second if
statement with a Boolean expression that is opposite the first, but Java provides a shortcut called the if
/else
conditional.
false
if the expression is true, and true
if the expression is false.1.
!
is evaluated first2.
&&
is evaluated second3.
||
is evaluated third9 > 2
is the Boolean expression that gets checked. Since the Boolean expression “9
is greater than 2
“ is true
, Control flow rocks!
will be printed to the console.if
statement with a Boolean expression that is opposite the first, but Java provides a shortcut called the if
/else
conditional.if
/else
conditional will run the block of code associated with the if
statement if its Boolean expression evaluates to true
.false
, it will run the block of code after the else
keyword.
For that case, we can use the
1. If the Boolean expression after the if
/else if
/else
statement in Java.if
statement evaluates to true
, it will run the code block that directly follows.2. Otherwise, if the Boolean expression after the
else if
statement evaluates to true
, the code block that directly follow will run.3. Finally, if all previous Boolean expressions evaluate to
false
, the code within the else
block will run.
EX: int shoeSize = 10;
if (shoeSize > 12) {
System.out.println("Sorry, your shoe size is currently not in stock.");
} else if (shoeSize >= 6) {
System.out.println("Your shoe size is in stock!");
} else {
System.out.println("Sorry, this store does not carry shoes smaller than a size 6.");
}- The term
ternary
comes from a Latin word that means “composed of three parts”.
These three parts are:
- A Boolean expression
- A single statement that gets executed if the Boolean expression is true
- A single statement that gets executed if the Boolean expression is false
EX: public class Switch {
public static void main(String[] args) {
char penaltyKick = 'R';
switch (penaltyKick) {
case 'L': System.out.println("Messi shoots to the left and scores!");
break;
case 'R': System.out.println("Messi shoots to the right and misses the goal!");
break;
case 'C': System.out.println("Messi shoots down the center, but the keeper blocks it!");
break;
default:
System.out.println("Messi is in position...");
}
}
}
What did I learned?
- Boolean Operators:
&&
,||
, and!
are used to build Boolean expressions and have a defined order of operations - Statements:
if
,if
/else
, andif
/else if
/else
statements are used to conditionally execute blocks of code - Ternary Conditional: a shortened version of an
if
/else
statement that returns a value based on the value of a Boolean expression - Switch: allows us to check equality of a variable or expression with a value that does not need to be a Boolean
No comments:
Post a Comment