Chapter 4.1. More Complex Conditions

In the current chapter, we are going to examine the nested conditional statements in the Java language, by which our program can contain conditions that contain other nested conditional statements. We call them "nested" because we put an if condition into another if condition. We are going to examine the more complex logical conditions through proper examples.

Video

Watch the video lesson about what we will learn in this chapter: https://www.youtube.com/watch?v=L4KtlGD5cbk.

Nested Conditions

Pretty often the program logic requires the use of if or if-else statements, which are contained one inside another. They are called nested if or if-else statements. As implied by the title "nested", these are if or if-else statements that are placed inside other if or else statements.

if (condition1) {
if (condition2) {
// body;
} else {
// body;
}
}


Nesting of*more than three conditional statements inside each other is not considered a good practice and has to be avoided, mostly through optimization of the structure/the algorithm of the code and/or by using another type of conditional statement, which we are going to examine below in this chapter.

Problem: Personal Titles

Depending on age (decimal number and gender (m / f), print a personal title:

• Mr.” – a man (gender “m”) – 16 or more years old.
• Master” – a boy (gender “m”) under 16 years.
• Ms.” – a woman (gender “f”) – 16 or more years old.
• Miss” – a girl (gender “f”) under 16 years.

Sample Input and Output

Input Output Input Output
12
f
Miss 17
m
Mr.
Input Output Input Output
25
f
Ms. 13.5
m
Master

Solution

We should notice that the output of the program depends on a few things. First, we have to check what is the entered gender and then check the age. Respectively, we are going to use a few if-else blocks. These blocks will be nested, meaning from the result of the first, we are going to define which one of the others to execute.

After reading the input data from the console, the following program logic should be executed:

Problem: Small Shop

A Bulgarian entrepreneur opens small shops in a few cities with different prices for the following products:

product / city Sofia Plovdiv Varna
coffee
water
beer
sweets
peanuts
0.50
0.80
1.20
1.45
1.60
0.40
0.70
1.15
1.30
1.50
0.45
0.70
1.10
1.35
1.55

Calculate the price by the given city (string), product (string), and quantity (double).

Sample Input and Output

Input Output Input Output
coffee
Varna
2
0.9 peanuts
Plovidv
1
1.5
Input Output Input Output
beer
Sofia
6
7.2 water
Plovdiv
3
2.1

Solution

We convert all of the letters into lower register using the function .toLowerCase(), to compare products and cities no matter what the letters are – small or capital ones.

More Complex Conditions

Let's take a look at how we can create more complex logical conditions. We can use the logical "AND" (&&), logical "OR" (||), logical negation (!) and brackets (()).

Logical AND

As we saw, in some problems we have to make many checks at once. But what happens when to execute some code more conditions have to be executed and we don't want to make a negation (else) for each one of them? The option with nested if blocks are valid, but the code would look very unordered and for sure – hard to read and maintain.

The logical "AND" (operator &&) means a few conditions have to be fulfilled simultaneously. The following table of truthfulness is applicable:

a b a && b
true
true
false
false
true
false
true
false
true
false
false
false

How Does The && Operator Work?

The && operator accepts a couple of Boolean (conditional) statements, which have a true or false value, and returns one bool statement as a result. Using it instead of a couple of nested if blocks, makes the code more readable, ordered, and easy to maintain. But how does it work, when we put a few conditions one after another? As we saw above, the logical "AND" returns true, only when it accepts as arguments statements with the value true. Respectively, when we have a sequence of arguments, the logical "AND" checks either until one of the arguments is over, or until it meets an argument with value false.

Example:

boolean a = true;
boolean b = true;
boolean c = false;
boolean d = true;
boolean result = a && b && c && d;
// false (as d is not being checked)


The program will run in the following way: It starts the check form a, reads it, and accepts that it has a true value, after which it checks b. After it has accepted that a and b return true, it checks the next argument. It gets to c and sees that the variable has a false value. After the program accepts that argument c has a false value, it calculates the expression before c, independent of what the value of d is. That is why the evaluation of d is being skipped and the whole expression is calculated as false.

Problem: Point in Rectangle

Checks whether point {x, y} is placed inside the rectangle {x1, y1} – {x2, y2}. The input data is read from the console and consists of 6 lines: the decimal numbers x1, y1, x2, y2, x and y (as it is guaranteed that x1 < x2 and y1 < y2).

Sample Input and Output

Input Output Visualization
2
-3
12
3
8
-1
Inside

Solution

A point is internal for a given polygon if the following four conditions are applied at the same time:

• The point is placed to the right from the left side of the rectangle.
• The point is placed to the left from the right side of the rectangle.
• The point is placed downwards from the upper side of the rectangle.
• The point is placed upwards from the down side of the rectangle.

Logical "OR"

The logical "OR" (operator ||) means that at least one among a few conditions is fulfilled. Similar to the operator &&, the logical "OR" accepts a few arguments of bool (conditional) type and returns true or false. We can easily guess that we obtain a value true whenever at least one of the arguments has a true value. This is shown at the truth table below:

In school, the teacher said: "Ivan or Peter should clean the board". For completing this condition (the board to be clean), it's possible only Ivan to clean it, only Peter to clean it, or both of them to do it.

a b a || b
true
true
false
false
true
false
true
false
true
true
true
false

How Does The || Operator Work?

We have already learned what the logical "OR" represents. But how is it being achieved? Just like with the logical "AND", the program checks from left to right the arguments that are given. To obtain true from the expression, just one argument must have a true value. Respectively, the checking continues until an argument with such value is met or until the arguments are over.

Here is one example of the || operator in action:

boolean a = false;
boolean b = true;
boolean c = false;
boolean d = true;
boolean result = a || b || c || d;
// true (as c and d are not being checked)


The program checks a, accepts that it has a value false, and continues. Reaching b, it understands that it has a true value and the whole expression is calculated as true, without having to check c or d, because their values wouldn't change the result of the expression.

Problem: Fruit or Vegetable

Let's check whether a given product is a fruit or a vegetable. The "fruits" are banana, apple, kiwi, cherry, lemon and grapes. The "vegetables" are tomato, cucumber, pepper, and carrot. Everything else is "unknown".

Input Output
banana
tomato
java
fruit
vegetable
unknown

Solution

We have to use a few conditional statements with logical "OR" (||):

Logical Negation

Logical negation (operator !) means that a given condition is not fulfilled.

a !a
true false

The operator ! accepts as an argument a bool variable and returns its value.

Problem: Invalid Number

A given number is valid if it is in the range [100 … 200] or it is 0. Validate an invalid number.

Input Output
75 invalid
150 (no output)
220 invalid

The Parenthesis () Operator

Like the rest of the operators in programming, the operators && and || have a priority, as in the case && is with higher priority than ||. The operator () serves for changing the priority of operators and is being calculated first, just like in mathematics. Using parentheses also gives the code better readability and is considered a good practice.

More Complex Conditions - Problems

Sometimes the conditions may be very complex, so they can require a long bool expression or a sequence of conditions. Let's take a look at a few examples.

Problem: Point on Rectangle Border

Write a program that checks whether a point {x, y} is placed onto any of the sides of a rectangle {x1, y1} – {x2, y2}. The input data is read from the console and consists of 6 lines: the decimal numbers x1, y1, x2, y2, x, and y (as it is guaranteed that x1 < x2 and y1 < y2). Print "Border" (if the point lies on any of the sides) or "Inside / Outside" (in the opposite case).

Sample Input and Output

Input Output Input Output
2
-3
12
3
12
-1
Border 2
-3
12
3
8
-1
Inside / Outside

Solution

The point lies on any of the sides of the rectangle if:

• x coincides with x1 or x2 and at the same time y is between y1 and y2 or
• y coincides with y1 or y2 and at the same time x is between x1 and x2.

Problem: Fruit Shop

A fruit shop during weekdays sells at the following prices:

Fruit Price
banana
apple
orange
grapefruit
kiwi
pineapple
grapes
2.50
1.20
0.85
1.45
2.70
5.50
3.85

During the weekend days the prices are higher:

Fruit Price
banana
apple
orange
grapefruit
kiwi
pineapple
grapes
2.70
1.25
0.90
1.60
3.00
5.60
4.20

Write a program that reads from the console a fruit (banana / apple / …), a day of the week (Monday / Tuesday / …) and a quantity (a decimal number) and calculates the price according to the prices from the tables above. The result has to be printed rounded up to 2 digits after the decimal point. Print “error” if it is an invalid day of the week or an invalid name of a fruit.

Sample Input and Output

Input Output Input Output
orange
Sunday
3
2.70 kiwi
Monday
2.5
6.75
Input Output Input Output
grapes
Saturday
0.5
2.10 tomato
Monday
0.5
error

Testing in The Judge System

A company is giving the following commissions to its traders according to the city, in which they are working and the volume of sales s:

City 0 <= s <= 500 500 < s <= 1000 1000 < s <= 10000 s > 10000
Sofia
Varna
Plovdiv
5%
4.5%
5.5%
7%
7.5%
8%
8%
10%
12%
12%
13%
14.5%

Write a program that reads the name of a city (string) and the volume of sales (double) and calculates the rate of the commission fee. The result has to be shown rounded up to 2 digits after the decimal point. When there is an invalid city or volume of sales (a negative number), print "error".

Sample Input and Output

Input Output Input Output Input Output
Sofia
1500
120.00 Plovidv
499.99
27.50 Kaspichan
-50
error

Solution

When reading the input, we could convert the city into small letters (with the function .toLowerCase()). Initially, we set the commission fee to -1. It will be changed if the city and the price range are found in the table of commissions. To calculate the commission according to the city and volume of sales, we need a few nested if statements, as in the sample code below:

Testing in The Judge System

 It is a good practice to use blocks that are enclosed with curly braces { } after if and else. Also, it is recommended during writing to move aside the code after if and else with a single tabulation inward, to make the code more easily readable.

Switch-Case Conditional Statement

The switch-case condition works as a sequence of if-else blocks. Whenever the work of our program depends on the value of one variable, instead of making consecutive conditions with if-else blocks, we can use the conditional switch statement. It is being used for choosing between a list of possibilities. The statement compares a given value with defined constants and depending on the result, it takes an action.

We put the variable that we want to compare, inside the brackets after the operator switch and it is called a "selector". Here the type must be comparable (numbers, strings). Consecutively, the program starts comparing each value that is found after the case labels. Upon a match, the execution of the code from the respective place begins and continues until it reaches the operator break. In some programming languages (like C#) break is necessary for every case, which contains a program logic. In Java though, the operator break can be skipped, until we get to the next break operator. Even though it is allowed by the language, it's never used and considered a bad practice, because it leads to collapsing of the program code. If there is missing a match, the default construction will run, if there is one.

switch (selector) {

case value1:
construction;
break;
case value2:
construction;
break;
case value3:
construction;
break;
…
default:
construction;
break;
}


Problem: Day of The Week

Let's write a program that prints the day of the week depending on the given number (1 … 7) or "Error!" if invalid input is given.

Input Output
1
7
-1
Monday
Sunday
Error!

Solution

 It is a good practice to put at the first place those case statements that process the most common situations and leave the case constructions processing the more rear situations at the end, before the default construction. Another good practice is to arrange the case labels in ascending order, regardless of whether they are integral or symbolic.

Multiple Labels in Switch-Cases

In Java, we can use multiple case labels in the switch-case construction, when they have to execute the same code. This way, when our program finds a match, it will execute the next code, because after the respective case label there is no code for execution and a break operator.

switch (selector) {

case value1:
case value2:
case value3:
construction;
break;
case value4:
case value5:
construction;
break;
…
default:
construction;
break;
}


Problem: Animal Type

Write a program that prints the type of the animal depending on its name:

• dog -> mammal
• crocodile, tortoise, snake -> reptile
• others -> unknown

Sample Input and Output

Input Output Input Output Input Output
tortoise reptile dog mammal elephant unknown

Solution

We can solve the problem with switch-case conditions with multiple labels in the following way:

What Have We Learned from This Chapter?

In this chapter, we learned how we can use more complex conditions, logical operators inside the condition, and nested if-constructions.

Nested Conditions

if (condition1) {
if (condition2) {
// body;
} else {
// body;
}
}


Complex Conditions with &&, ||, ! and ()

if ((x == left || x == right) && y <= top && y >= bottom)
System.out.println (…);


Switch-Case Conditions

switch (selector) {
case value1:
construction;
break;
case value2:
case value3:
construction;
break;
…
default:
construction;
break;
}


Problems More Complex Conditions

Let's practice using more complex conditions. We will solve a few practical exercises.

Problem: Cinema

In a cinema hall, the chairs are ordered in a rectangle shape in r rows and c columns. There are three types of screenings with tickets of different prices:

• Premiere – a premiere screening, with a price of 12.00 BGN.
• Normal – a standard screening, with a price of 7.50 BGN.
• Discount – a screening for children and students at a reduced price – 5.00 BGN.

Write a program that enters a type of screening (string), a number for rows, and a number for columns in the hall (integer numbers) and calculates the total income from tickets from a full hall. The result has to be printed in the same format as in the examples below – rounded up to 2 digits after the decimal point.

Sample Input and Output

Input Output Input Output
Premiere
10
12
1440.00 leva Normal
21
13
2047.50 leva

Hints and Guidelines

While reading the input, we could convert the screening type into small letters (with the function .toLowerCase()). We create and initialize a variable that will store the calculated income. In another variable, we calculate the full capacity of the hall. We use a switch-case conditional statement to calculate the income according to the type of the projection and print the result on the console in the given format (look for the needed Java functionality on the internet).

Sample code (parts of the code are blurred with the purpose to stimulate your thinking and solving skills):

Problem: Volleyball

Vladimir is a student, lives in Sofia, and goes to his hometown from time to time. He is very keen on volleyball, but is busy during weekdays and plays volleyball only during weekends and on holidays. Vladimir plays in Sofia every Saturday when he is not working, and he is not traveling to his hometown and also during 2/3 of the holidays. He travels to his hometown h times a year, where he plays volleyball with his old friends on Sunday. Vladimir is not working 3/4 of the weekends, during which he is in Sofia. Furthermore, during leap years Vladimir plays 15% more volleyball than usual. We accept that the year has exactly 48 weekends, suitable for volleyball. Write a program that calculates how many times Vladimir has played volleyball throughout the year. Round the result down to the nearest whole number (e.g. 2.15 -> 2; 9.95 -> 9).

The input data is read from the console:

• The first line contains the word “leap” (leap year) or “normal” (a normal year with 365 days).
• The second line contains the integer p – the count of holidays in the year (which are not Saturday or Sunday).
• The third line contains the integer h – the count of weekends, in which Vladimir travels to his hometown.

Sample Input and Output

Input Output Input Output
leap
5
2
45 normal
3
2
38
Input Output Input Output
normal
11
6
44 leap
0
1
41

Hints and Guidelines

As usual, we read the input data from the console and, to avoid making mistakes, we convert the text into small letters with the function .toLowerCase(). Consequently, we calculate the weekends spent in Sofia, the time for playing in Sofia, and the common playtime. At last, we check whether the year is a leap, we make additional calculations when necessary and we print the result on the console rounded down to the nearest integer (look for a Java class with such functionality).

A sample code (parts of the code are blurred on purpose to stimulate independent thinking and solving skills):

Problem: Point in The Figure

The figure consists of 6 blocks with size h * h, placed as in the figure below. The lower left angle of the building is on position {0, 0}. The upper right angle of the figure is on position {2*h, 4*h}. The coordinates given in the figure are for h = 2:

Write a program that enters an integer h and the coordinates of a given point {x, y} (integers) and prints whether the point is inside the figure (inside), outside of the figure (outside), or on any of the borders of the figure (border).

Sample Input and Output

Input Output Input Output
2
3
10
outside 2
3
1
inside
Input Output Input Output
2
2
2
border 2
6
0
border
Input Output Input Output
2
0
6
outside 15
13
55
outside
Input Output Input Output
15
29
37
inside 15
37
18
outside
Input Output Input Output
15
-4
7
outside 15
30
0
border

Hints and Guidelines

A possible logic for solving the problem (not the only correct one):

• We might split the figure into two rectangles with a common side:

• A point is outer (outside) for the figure when it is outside both of the rectangles.
• A point is inner (inside) for the figure if it is inside one of the rectangles (excluding their borders) or lies on their common side.
• In another case, the point lies on the border of the rectangle (border).

Sample code (parts of the code are blurred to stimulate logical thinking and solving skills):