Lesson 5
If Statement Body, Increment/Decrement, Compound-Assignment
Author: Brandon Fajardo (9/1/17). Updated (8/1/19)
Introduction
Review: The last lesson covered mathematical equations and accessing the Math class for more complex functions.
Lesson topic: In this lesson, you will learn how to apply what you have learned thus far about conditions and equations to the bodies of if statements.
Topic 1 - Using Mathematical Expressions in Conditions
Subtopic 1 - Restricting the Domain/Range
When working with mathematical equations, we can use the condition of an if statement to restrict domain or range.
EX: Here, we restrict the domain by using the condition to prevent division by 0 where it would be undefined.
int x;
if (x!=0) {
(x+2)/x
}
EX: Here, the print statement only runs if the number inputted is positive and not 0.
int y;
if (y>0) {
System.out.println(“The number is positive and not 0”);
}
EX: Here, we test to see if the modulus (remainder) of 9 is 0. If it is true, z must be evenly divisible by 9.
int z;
if (z%9 == 0) {
System.out.println(z + “is evenly divisible by 9”);
}
Problem #1:
Try creating an if statement condition which tests if a value is even. Write another for when the value is odd.
*Solutions at bottom of this document
Subtopic 2 - Using Mathematical Expressions in the Condition
You can also use mathematical expressions within the condition.
EX:
double d;
if (d*2>=10) {
System.out.println(d/2);
}
*The test value is first multiplied by 2 before checking if it is greater than or equal to 10.
Topic 2 - Redefining Variables
You may also redefine the value of a variable within the body of an if statement.
Subtopic 1 - Declare a New Value
You can declare a new value for a variable.
EX:
boolean b = true;
if(b) {
b = false; *Here, b is redefined to be false
System.out.println(b);
}
Subtopic 2 - Modify a Current Value
You can also add to or subtracting from the value of a variable. This involves equating a variable to itself along with some modification. This is often written as x = x + y, where x is the original variable and y is the modification.
EX:
int i = 4;
if (i >-1) {
i = i + 4; //Here, the variable becomes 4 greater and is now equal to 8
System.out.println(i);
}
EX:
String s = “First”;
if (s == ”First”) {
s = s + “ Last”;
System.out.println(s);
}
*Here, “ Last” is added to the end of the variable s. The variable s now equals “First Last”
Topic 3 - Notations to Modify Variables
In Java, there are built-in notations to more easily add to or subtract from variables. These include: Increment, Decrement, and Compound-Assignment Operators
Subtopic 1 - Increment and Decrement
Increment/Decrement Operations:
Symbol
Name
Notation
Notation Equivalent
Use
++
Increment
x++;
x = x + 1;
Adds 1 to itself
--
Decrement
x--;
x = x - 1;
Subtracts 1 from itself
These operations are shorthand ways to increase or decrease a mathematical variable by 1.
*x++ and x --can not be used within print statements.
EX: If it is your birthday, your age increases by 1
int age = 16;
boolean Birthday;
if (Birthday) {
age++;
}
EX: If you eat a cookie, the number of cookies decreases by 1
int cookie;
boolean eat;
if (eat) {
cookie--;
}
Subtopic 2 - Compound-Assignment Operators
Compound-Assignment Operators are additional shorthand notations used to modify mathematical variables in different ways.
Symbol
Name
Notation
Notation Equivalent
Use
+=
Plus equals
x+=v;
x = x + v
Adds a value v to x
-=
Minus equals
x-=v;
x = x - v
Subtracts a value v from x
*=
Times equals
x*=v;
x = x * v
Multiplies a value v and x
*The product result become x
/=
Divide equals
x/=v;
x = x / v
Divides x by a value v
*The dividend becomes x
%=
Modulus equals
x%=v;
x = x % v
Finds the modulus of x by the value v
*The remainder becomes x
This original variable is represented by x here.
EX: Adds 100 points to your score if you kill an enemy.
double score;
boolean killenemy;
if (killenemy) {
score+=100;
}
Problem #2: Compound-Assignment Operators
Try using each of the compound-assignment operators in its own if statement.
Things to note:
You cannot apply most Compound-Assignment Operators onto Strings or booleans.
The exception is that += will work with Strings.
Conclusion
Recap: In this lesson, you learned how to combine if statement conditions with mathematical equations, two concepts you have previously learned.
Next: The next lesson will cover how to use multiple components in an if statement condition. This includes the use of nested if statements and logical operators (||, &&).
Solutions
Problem #1:
Even: if(z%2==0)
Odd: if(z%2!=0)
Problem #2:
No solutions given
Last updated
Was this helpful?