Back to Top
 
 

Arithmetic operators

We already know there is a numeric type of variables. Thanks to it, we can perform various mathematical calculations using the appropriate arithmetic operators:

  • addition (plus character: „+”)
  • subtraction (minus: „-”)
  • multiplication (asterisk: „*”)
  • division (slash: „/”)
  • modulo (percent: „%”)

The modulo operation, which returns the remainder of the division, may be new to you.

It's like performing a division, but we're not interested in its result just whether there's any remainder left.

 

Such as: 7 % 2 (seven modulo divided by two) returns 1.

Let's explain one by one why that happens:

7%2 = 3 * 2 + 1;

There are three twos in a seven: and a one that remains. That remainder is the modulo division result.

 

If we need to perform calculations, we don't need to do it ourselves - Java will do it for us. We only need to learn to give it the data on which we want to perform an operation.

So, if we want to perform an arithmetic operation on two numbers we declare two variables; we assign certain values to them, and assign the result of the operation to a third variable, which we can later display on the screen, in the way we already know.

See examples of arithmetic operations:

Conditional statements

Sometimes we want an operation to execute only if certain conditions are met. For example, access to some Facebook content can only be granted if you are logged in. You can fly overseas only if you have the proper documents.

In Java conditions are set with control instructions. In this context we also speak about "program flow control": we decide when and what actions the program is to perform. Based on the set conditions, the program "picks" the instruction to work with.

It is impossible to omit here logical operators, which are often used in conditional instructions. Here's the list:

==                equal
> greater than
>=  greater or equal
< less than
<= less or equal

A condition can be met or not: we mark that with a boolean type variable set to True or False.

The simplest condition we can come up with is a single 'if' followed by the instructions to be executed:

 

Here's an example:

if (condition == true) {
// instructions
}

See an example of a conditional statement:

That is, if the condition is met the program must perform certain actions. On the other hand, if the condition is not met, the program does nothing. We can make the example a bit more complex and specify what to do when the condition does not occur. It's easy to do with the else clause: the instructions to perform in case the condition is not met. We need to write:

if (condition == true) {
 // instructions
 } else {
 // instructions
 }

See the second example:

We can also check more conditions with the if-else if-else structure.

 

Here's an example:

if(condition == true) {
 // instructions
 } else if (condition == true) {
 // instructions
 } else {
 // instructions
 }

Let's see what's going on here:

  • if (condition == true) {...} – we ask if the condition given in the parentheses is met. If so (the result is 'True'), the instructions given in the curly brackets execute. If it is not: the program omits the instructions and goes further on.

  • else if (condition == true) {...} – if the first condition result was 'False' we move on to the next one. Here we are checking another condition – and as before – if it is 'True' the instructions in the curly brackets execute; if it isn't the program moves on.

  • else {...} – and if none of the earlier conditions were met, the instructions written after 'else' execute.

Let's see this in practice. Let's assume we want to show the user a message with their LOTTO results. To make it easier let's assume that only a 'five' and 'six' win.

See the 'if else' example:

If we have several conditions, but they relate to one issue (for example, declaring a range of values, i.e.: the number is greater than 3 and at the same time less than 25), we do not need to use the if construction twice - we can combine both conditions in one set of parentheses.

There are two operators to combine conditions:

  1. AND, written as: && - the value must meet both conditions: the one on the left and on the right.

  2. OR, written as: || – the value can meet one of the conditions: doesn't matter which one.

If we assume that a course group cannot be smaller than 7 people but at the same time not bigger than 15, we will use AND.

See the examples of combining conditions with AND:

We can write the same in a different way: using the OR operator. Our group cannot be smaller than 7 and bigger than 15 so if only one of these conditions is not met the group will not be created.

See the example of combining conditions with OR:

 

Tasks

 

Task 1

Declare a few variables, do calculations on them, assign their results to variables and display them. Try to divide 15 by 7 and take a close look at the result: how do we get it? Is the result exact?

Go to the first task:

When dividing, only the integer part of the result is obtained, and the fractional part is discarded - this is because we declared the variable type as int, and it only stores integers.

To handle what we commonly call fractions: floating-point numbers there are separate types (e.g., double). See what the result of the earlier division is once you declare the double type variables rather than int.

If you want to change the value of a number by adding another one to it, we can write it as: x = x + y; or shorter: x += y;

This works similarly with subtraction, multiplication, etc. The shorthand is very popular and often used by programmers as a cleaner format.

Task 2

Check if the user logging in is an adult. Write the correct appropriate conditions and display messages accordingly: (e.g., "User is adult", "User is underage").

Go to the second task:

Task 3

You probably had your blood tested at least once. The results we get must meet the norm. E.g., for hemoglobin (HGB) – the level for women is 12 to 16 g/dL; for men: from 14 to 18 g/dL. Write the code that checks if the resulting levels meet the norms, separately for men and women. Write one condition using the && operator and the other one with ||.

Go to the third task:

Task 4

Now for some sport: there's a weigh-in before a boxing competition. Create the code that assigns the boxer to a preset weight class.

Go to fourth task: