undefined

Conditional operators

Java Syntax
Level 4 , Lesson 4
Available

"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
2
Task
Java Syntax, level 4, lesson 4
Locked
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!
undefined
8
Task
Java Syntax, level 4, lesson 4
Locked
Number of days in the year
On Earth, a year lasts 365 or 366 days. The number of days is calculated according to the following formula: A leap year (366 days) is every year evenly divisible by 4, except for years that are multiples of 100 but not multiples of 400. We'll write a program that will determine whether the user has entered a leap-year or ordinary year from the keyboard.
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Rule of the triangle
Can any three line segments be the sides of a triangle? You probably already guessed (or remembered from high-school geometry) that you can only have a triangle if the sum of the lengths of any two sides is greater than the length of the third side. Well, now we're going to write code to check whether 3 line segments are suitable for forming a triangle.
undefined
8
Task
Java Syntax, level 4, lesson 4
Locked
Crossing the road blindly
Let's say that we are certain that at the beginning of every hour our traffic light is green for 3 minutes, yellow for a minute, and then red for another minute. Then the sequence repeats. Our program must determine what light is on now (where "now" is a real number that indicates the number of minutes that have elapsed since the beginning of the hour).
undefined
4
Task
Java Syntax, level 4, lesson 4
Locked
Do we have a pair?
Suppose we have three numbers. Now let's imagine that they are not numbers, but people ... Actually, never mind about that. We don't need to make this weird. Let's just check if there is at least one pair of identical numbers among the three. If there is, we'll display it on the screen. And if the three numbers are the same, we'll display all three.
Comments (118)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Aggrey “Oluande” Ochieng Level 9 Nairobi Kenya
28 March 2021
Is it just me or the "Conditions.tcs" file blanks out for everyone in the intellijidea?
Zach Level 17 Fort Collins United States
31 January 2021
In the USA Sunday is considered the first day of the week. Is this not the case in different parts of the world?
Karol Level 18 Radom Poland
30 January 2021
easy xD lol crossing the road blindy done in 10 minutes
Volodymyr Level 7 L'viv Ukraine
22 January 2021
In my IntelliJ Idea - "RUN" button is not active. So, to check the code i have to run it in a browser. Does it have to be like that or it can be fixed? Thanks.
Lena Level 4 Seattle United States
6 January 2021
"closest to 10" problem made me wanna kms
Dirk DuifCodeGym Level 17 Den Haag Netherlands
23 December 2020
The biggest tip that I can give for "Crossing the road blindly": Use % for your calculations!
Chandan Thapa Level 22 Dubai United Arab Emirates
2 October 2020
longest time I spent out of the above was on "Crossing the road blindly". Great learning! I'm enjoying it so far.
Michael Grundt Level 4 Sankt Augustin Germany
1 October 2020
@Number of days in the year: According to the hints 3300 should be a leap year, because it's evenly divisible by 4 (825 * 4 = 3300). But it's also evenly divisible by 100, so it should be a regular year. This is a contradiction or do i miss something.
mastere Level 9 Rochester United States
1 October 2020
Didn't the tasks having to do with making min methods to get the minimum of 2, 3 and 4 numbers require using if/else statements? Because that was the only way I could think to do them.
19 September 2020
Try using an Excel spreadsheet : Col A is "minutes" 1 through 30, for example. Col B is "MOD(A1, 5). " See if there's a pattern. Color code (red, yelloe, green) the pattern to make it clearer. Sort on the color, and the pattern should become obvious. You can use that in your if statement. In your program you may also need to use MATH.floor() to account for the use of doubles in the input.