"Hi, Amigo. Today we'll talk about if/else statements."

"Programs would be of little use if they didn't respond to changing external circumstances. A program needs to know how to adapt to circumstances and perform one action in one case and other actions in other cases. In Java, this is achieved using the 'if/else statement' – a special construct that makes it possible to perform different code blocks if a condition is satisfied."

"It consists of three parts: 'condition', 'command 1' and 'command 2'. If the condition is true, then 'command 1' is executed, otherwise 'command 2' is executed. These commands are never both executed. The statement looks more or less like this:"

Code for an if/else statement
if (condition)
    command_1;
else
    command_2;

"How exciting! I think that statement will make programming much more interesting!"

"Yep. Here are a couple of examples for you:"

Code Explanation
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
If a is less than b, the first command will be executed. Otherwise the second command will be executed. The commands are never both executed.
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
You can replace one command with a code block. The rest is the same.
3
if (a < b)
{
    a = 0;
}
else
{
}
You can omit the else block if it's empty.
These three examples are entirely equivalent.
You can omit the curly brackets if you only need to execute one command. If you have more than one command, you need to keep the brackets.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"Diego just asked me to give you a few tasks."

undefined
3
Task
New Java Syntax, level 3, lesson 7
Locked
Good or bad?
Write the compare(int a) method so that it: - displays "The number is less than 5" if the method argument is less than 5, - otherwise, displays "The number is greater than 5".
undefined
3
Task
New Java Syntax, level 3, lesson 7
Locked
Interval
Implement the checkInterval method. The method should check whether an integer is between 50 and 100, not inclusive, and report the result on the screen in the following form: "The number a is not in the interval." or "The number a is in the interval.", where a is the method argument. Example for 11
undefined
3
Task
New Java Syntax, level 3, lesson 7
Locked
Positive and negative numbers
Use the keyboard to enter a number. If the number is positive, then double it. If the number is negative, add one. If the entered number is zero, display zero. Display the result on the screen.
undefined
3
Task
New Java Syntax, level 3, lesson 7
Locked
Day of the week
Use the keyboard to enter a number representing a day of the week. Then, depending on the entered number, display the name of the day of the week: "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", if you enter a number greater than 7 or less than 1, display "No such day o