<< Back Computer Science Club

October 16 2019 - Java 2 (if else statements 1, relationals)

At this meeting, we went over part of if and else statements along with relational operators.




If and Else Statements

If

An if statement will check whether a comparison is true and in that case run subsequent code. Here's an example:

if (number == 20){

 //runs whatever's in here only if the number variable is 20

}

//runs code over here (outside the if statement) no matter what



Else

Adding an else will let you run code if the if statement isn't true. Here's an example:

if (number == 20){

 //runs whatever's in here only if the number variable is 20

} else {

 //runs whatever's in here only if the number variable isn't 20

}

//runs code over here no matter what




Relational Operators

Relational operators compare two things and return a true or false (used best in if statements). You can put the following operators in between two variables or between a variable and a value to compare them.


==
Checks whether the two are equal to each other.


< or >
Checks whether the left is less/greater than the right.


<= or >=
Checks whether the left is less/greater than or equal to the right.


!=
Checks whether the two are NOT equal to each other.




If you have any questions, ask them at Computer Science Club!