Lesson 3

If Statement, Conditions, Comparisons

Author: Brandon Fajardo (8/30/17). Updated (9/22/18)

Introduction

Review: In the last lesson, you first learned about print statements. Then, you were introduced to the basic mathematical operations.

Lesson topic: This lesson will introduce if statements, one of the most useful statements in Java. Specifically, it will cover if statement conditions.

Topic 1 - If statements

In programming, it is often necessary to control the flow of our code. This allows us to be more specific in exactly what we want to happen. One useful way to do this is to use a conditional statement. That way, certain code only runs when a given condition is met.

If statement: a statement that runs a block of code if a given condition is met.

Notation:

if (condition) {
 action(s)
}

If statements are made of two parts. The first part is the condition. This is written inside a set of parentheses after the word if. The second part is the body. This is written inside a set of braces after the condition. If the condition is met (true), the body of the if statement and its action(s) will run. If the condition is not met (false), the body of the if statements and its action(s) will not run.

Topic 2 - Conditions

Subtopic 1 - Comparison Operators

There are many ways to write conditions. One way to specify conditions is to use certain symbols to compare values.

Comparison Operators

Operator

Name

Function

>

Greater than

Returns true if the first argument is greater than the second

If not, returns false

<

Less than

Returns true if the first argument is less than the second

If not, returns false

>=

Greater than or equal to

Returns true if the first argument is greater than or equal to the second

If not, returns false

<=

Less than or equal to

Returns true if the first argument is less than or equal to the second

If not, returns false

==

Equal to

Returns true if the two arguments are equal

If not (if they are unequal), returns false

!=

Not equal to

Returns true if the two arguments are unequal

If not (if they are equal), returns false

These symbols, many of which are just inequality symbols, are used in if statement conditions.

Things to note:

  • The statements you will learn in the future will also use these comparison operators for their conditions.

  • It is important to distinguish a regular equals sign and a double equals. A double equals sign is used to test equality within a condition while a regular equals sign is used to equate two things in code.

Subtopic 2 - Comparison Operator Examples

To practice what you have learned so far, create a new variable using every data type. Then, use each variable in an if statement condition utilizing all the comparison operators.

*For now, use a commented (//) phrase as the action in the body of the if statement

EX:

int age = 16;
if (age >= 15.5) {
 //Can work
}

double value = 1.38294;
if (value != 3.14159) {
 //Not pi
}

char grade = ‘F’;
if (grade == ‘F’) {
 //Fail class
}

Subtopic 3 - Quirks of Boolean

It is unnecessary to use double equals (==) and not equal to (!=) when using a boolean in an if statement condition. When the boolean is true, just write the variable name. This represents “true.” When the boolean is false, put ! an exclamation point before its name. This represents “not true.”

EX:

boolean lazy = true;
if (lazy) { //Instead of: (lazy == true)
 //Skip homework
}

boolean artistic = false;
if(!artistic) { //Instead of: (artistic != true)
 //Draw stick figures
}

This is because the if statement condition is only looking to see if the condition is true or false, and the variables of a boolean themselves already represent either true or false.

Subtopic 4 - Multivariable Conditions

Expanding upon this idea of comparisons, there can be multiple variables in an if statement condition.

EX:

int highscore = 1;
int myscore = 10;
if (myscore > highscore) {
 //Make myscore the highscore
}

Conclusion

Recap: You have gone through many of the functions of if statement conditions. In the future, these same functions can be used in future conditions of different statements.

Next: You will learn how to create mathematical equations.

Last updated