Comparisons: Greater Than and Less Than
Aside from math, we can also compare two values. We can compare values by using the following:
greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
These operations are important because it allows us to make decisions and find answers in our code. They will be used in the next section when we start to program if and while loops
However, they can also be used in code that are not if or while loops. The following is one example:
In this code, it takes has the variable y which is set equal to 4. It also has the variable x which is set equal to 3. In the print statement, it is comparing the y and x variables. If you click the run button, you will see that it prints true. This is because 4 is bigger than 3. You can play around with the numbers and the comparisons operator to get different results.
Conditionals
In everyday life, we made decisions based on different situations. Such as if we are hungry, then we get a snack or if we are tired, we go to bed.
Often in code, we want our program to do something similar and make different decisions based on different situations. We can do this by using Conditional statements.
In Java, we have four different conditional statements:
If statements
Else if statements
Else statements
Switch statements
However, we will be going over the first three.
First lets go over the structure of an conditional. we will use the keywords:
if
else if
else
These are reserved words that are part of the syntax and they are used to dedicated variable or function names.
We will also see parentheses (). These are what hold the conditions in the conditional statements. Lastly, we will see brackets { }. These are used to open and close blocks of code. Below is an example of what the conditionals looks like when we put everything together.
Conditionals: If statement
An if statement works like, If the condition is true then do the following task.
If Condition is True, the code inside the statement will run. If Condition is False, the code inside the statement will not run causing the code to leave the statement.
Lets look at an example! In this example we are looking at what the temperature is to determine if we should bring a jacket when the temperature is 65 F. What do you think is going to happen when we run the code? Think about it first then run the code below
The code will print out "You don't need a jacket". This is because 65 is greater than 60 which makes the condition true. If the number was less than 60, such as 40 for an example, it will not print out anything because 40 is less than 60.
Conditionals: Else If statement
Else if statements work like the following: If the condition is true, run do the following task. Otherwise, if another condition is true, then do a different task.
If Condition_A is True then the code inside the if statement will run. If Condition_A if False, then it will go to the else if statement. If Condition_B is True, the code inside the else if statement will run. If Condition_B if False, then it will exit the statement and none of the code will run.
Lets look at an example! We will use the temperature example again but this time with an else if statement. In this example we are looking at what the temperature is to determine if we should bring a jacket when the temperature is 35 F. What do you think is going to happen when we run the code? Think about it first then run the code below
The code will print out "You might want to bring a jacket". This is because 35 is NOT greater than 60 which makes the first condition false. Because it is false, the code goes to the else if statement. 35 is less than 60 which makes the else if statement true. Because it is true, the code inside the else if statement will run and "You might want to bring a jacket" gets printed.
Conditionals: else statement
Else statements work like the following: If the condition is true then, do the task. Otherwise do the other task.
If Condition is True, then the code inside the if statement will run. Otherwise, the code inside the else statement will run.
This means if Condition is something other than True, so False, the else statement will run.
Lets look at an example! We will use the temperature example again but this time with an else statement. In this example we are looking at what the temperature is to determine if we should bring a jacket when the temperature is 35 F. What do you think is going to happen when we run the code? Think about it first then run the code below
The code will print out "You might want to bring a jacket". This is because 35 is NOT greater than 60 which makes the first condition false. Because it is false, the code goes to the else and "You might want to bring a jacket" gets printed.
In this exercise you will:
Create an int variable called windSpeed and set it equal to 20
create an if statement that compares windSpeed to 15
If windSpeed is less than 15 print out that it's not too windy to go to the beach
You can use an else or else if statement for this next step!
If windSpeed is greater than 15, then print out it's too windy to go to the beach