warm tip: this thing that Luke wrote is quite confusing......
Alright, so far we have learned how to make Arrays, Conditionals, and Variables. Now, we need our computer to start making decisions. How do we do that you ask? Through if, else if , and else statements. Note, you guys have hopefully made this in the conditionals lesson. This lesson will explain it better.... probably.
If statements are the most basic control flow statements in programming. What it essentially does is it tells your program to execute a certain function if (see what I did there) a particular test evaluates to true. Let me give you an example.
Lets say we have a Boolean variable called stupid and is set as false.
public class Main {
public static void main(String[] args) {
boolean stupid = false;
}
Now the next thing we need to do is write an if statement. In the parenthesis, that is where your put in your condition where the statement will check if it meets the specific requirements to execute the code it holds. Since we are checking if a variable "has a value of" something, we need to use the ==. Also, for if statements and loops(which you will learn later) we use curly brackets like how we use in our method.
public class Main {
public static void main(String[] args) {
boolean stupid = false;
if(stupid == false){
System.out.println("SMART!@#");
}
}
If you did this right, then your code should spit out SMART!@#. Congratulations, you just made an if statement. However, lets say if stupid was true? Well then the code will obviously not return anything, but there is a way that we can make it return something else even though it doesn't meet the requirements for the if statement.
Start by changing your stupid variable to true and write another if statement except this time, get rid of the parenthesis and if part and replace it with else.
public class Main {
public static void main(String[] args) {
boolean stupid = true;
if(stupid == false){
System.out.println("SMART!@#");
}
else{
System.out.println("DUNCE");
}
}
}
else is the control flow statement that runs if the if statement doesn't run because its requirements were not met to execute its code. Now the code should return DUNCE instead of SMART!@#.
Alright now lets move to else if statements. else if statements is basically an if statement, that's all you need to know. Later I'll explain when to use them and when not to use them. In your code, change else to else if with parenthesis. So it should look like this:
public class Main {
public static void main(String[] args) {
boolean stupid = false;
if(stupid == false){
System.out.println("SMART!@#");
}
else if(stupid == true){
System.out.println("DUNCE");
}
}
The code should work exactly the same as the previous code.
If your confused, let me explain. In programming, an if statement is when you are making a new argument in your code with one option. While an else if statement is essentially an if statement that adds more options to that argument. An else statement is used when all options have been exhausted and that this is the final option of the argument in your code. If you are still confused, check out the video link in the original java tutorial page.
Now you have officially learned how to use if statements in Java! Go away now....
Do this exercise "dateFashion" at codingbat.
http://codingbat.com/prob/p103360
Paste your code after you get a checkmark.
Tip: use "return" to give your answer.
Created: text: Luke Kim, exercise: Dexin, 4/3/18
Updated: Dexin, 12/5/18