What are conditionals
Conditionals are used to check if a certain statement is true or false. These statements also help you act on these statements.
How to check values
To check if two values are equal use the “==” operator. To check if one value is greater than or less than use a “>” or “<” operator. To check if two things are greater than or equal to or less than or equal to use the “>=” or “<=” operators respectively. For Example…
(5 == 5)
//this checks if 5 is equal to 5
//it is equal, thus the conditional return true
/*NOTE: this is not standalone code, it needs some sort of action to act upon, such as an if*/
For strings this is how we check values...
(stringName.equals("what we think it should equal"))
//we can also check if a string is equal regardless of capitalization...
(stringName.equalsIgnoreCase("what we think it should equal"))
/*NOTE: this is not standalone code, it needs some sort of action to act upon, such as an if statement*/
If statements
If statements are used to execute a certain set of code if a logic gate is satisfied. These steps are only checked and executed once. For example…..
public static void main(String[] args){
int integerOne = 5;
int integerTwo = 7;
if(integerOne == 5){
//this will return true
//we can put our "if true" code here
}
else{
/*since the last if statement was true,
the conditional for this will return false
The code here will nor execute*/
}
}
We can use our other conditionals here too...
int integerOne = 5;
int integerTwo = 7;
if(integerOne >= integerTwo){
//this will return false
//the code here will not execute
//we can put our "if true" code here
}
else {
/*since the last if statement was false,
the conditional for this will return true
The code here will execute*/
//code goes here
}
We can compare strings...
String check = "Hello";
if(check.equalsIgnoreCase("World")){
//this will return false
//the code here will not execute
//we can put our "if true" code here
}
else{
/*since the last if statement was false,
the conditional for this will return true
The code here will execute*/
//code goes here
}
Else If statements
Else if statements are used to check multiple statements. If the first ‘if’ statement is false it will check the next ‘else if’ statement to see if it is true. For example...
int integerOne = 5;
int integerTwo = 7;
if(integerOne > integerTwo){
//this will return false
//the code here will not execute
//we can put our "if true" code here
}
else if(integerOne < integerTwo){
/*since the last if statement was false,
it will check if this condition is true.
This condition is true.
The code here will execute*/
//code goes here
}
else{
/*if all other conditions were not true,
(if integer one was equal to integerTwo)
it would return true. Since the else if
statement above it is true it does not execute*/
//code goes here
}
Nested If statements
Nested If Statements are If statements that are inside of If statements. On condition that the parent(main) If statement is satisfied to be true. You can also put else/else if statements in the nested loop. Here is an example of a nested if statement…
int integerOne = 5;
int integerTwo = 7;
if(integerTwo > integerOne){
//this conditional returns true
//thus the if/else statement below executes
if(integerTwo == 5){
//this will not return true
//code here will not execute
//code goes here
}
else{
//since the if statement above is false...
//this will return true
//code here will execute
//code goes here
}
}