JAVA · ОСНОВЫ · LESSON 04

If not.

if, else and comparison · about 15 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Check the number.

if stands for «если». Write a check in parentheses. If it is correct, the code runs inside the first brackets.

int points = 8;
if (points >= 5) {
    System.out.println("Ура!");
}

✦ Знак >= значит «больше или равно».

STEP 2

There's another way.

else stands for «иначе». Part of it works when the check is false. So the program chooses one of two paths.

int points = 2;
if (points >= 5) {
    System.out.println("Ура!");
} else {
    System.out.println("Ещё раз");
}
Fork.
Verificationpoints >= 5?Yes.first-lineNo.else

✦ With 2 points, it'll come back again.

IN-CLASS PRACTICE

Type the code yourself

Dial both paths of the program. Find a couple of brackets at each branch.

public class Main {
    public static void main(String[] args) {
        int points = 7;
        if (points >= 5) {
            System.out.println("Супер");
        } else {
            System.out.println("Ещё раз");
        }
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create int score with a value of 8. If score is at least 5, output «Сдано», otherwise «Попробуй ещё». Use if and else.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework