Boolean type

In addition to arithmetic operations from school, we know operations of comparison. For example, 5 > 4. It sounds like the question: “5 is more than 4?”. In this case, the answer is “yes.” In other cases, the answer may be no (for example, for 3 < 1).

List of comparison operations:

  • < less
  • <= less or equal
  • > more
  • >= greater or equal
  • == equal
  • != not equal

The logical operation of the type 5 > 4 is an expression, and its result is the value of the logical type. The Java boolean type bool is a value that is written as true or false, for example, var isFivePositive = (5 > 0).


Let us try to write a function that takes the child’s age in years (integer is an int type) and determines whether the child is a baby. Infants are considered children up to a year:

static boolean isInfant(int age) {
  return age < 1;
}

We take advantage of the fact that any operation is an expression, therefore, with a single line of function, we write “return the value that will be obtained by comparing age < 1”.

Depending on the argument that came in, the comparison will be either true or false , and return will return this result.

Call the function and display the result.

public static void main(String[] args) {
  System.out.print(isInfant(3));
}

static boolean isInfant(int age) {
  return age < 1;
}
false

And now let's check a child who is half a year old - zero full years:

```java
System.out.print(isInfant(0));
```

true

instructions

Write the function isPensioner, which takes the age as the only argument and checks if this age is retirement. Pension is considered the age of 60 years and more.


Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on Github.