Nested if Statements

An if statement can be placed inside another one, and an if-else can be extended into a chain that picks one outcome from several. Here a video player chooses a streaming quality from the connection speed, in megabits per second.
An if inside an if
Only a premium account can stream in 4K, so the speed check goes inside a check on the account.
boolean premium = true;
int mbps = 18;
if (premium)
{
if (mbps >= 25)
{
System.out.println("4K");
}
else
{
System.out.println("HD");
}
}
else
{
System.out.println("SD");
}
The outer expression is true, so the outer body runs, and that body is an if-else. Since 18 isn't at least 25, it prints HD. With premium set to false, the outer else runs and prints SD. In that case mbps >= 25 is never evaluated, whatever the speed.
Choosing one of several
Every speed gets one of four qualities. An else followed directly by another if starts a chain.
int mbps = 12;
if (mbps >= 25)
{
System.out.println("4K");
}
else if (mbps >= 10)
{
System.out.println("HD");
}
else if (mbps >= 3)
{
System.out.println("SD");
}
else
{
System.out.println("audio only");
}
Java evaluates the expressions from the top. Since 12 isn't at least 25, it continues to the second, and 12 is at least 10, so HD is printed and the statement is finished. The third expression would also have been true, but it's never evaluated.
Order and the trailing else
Since the first true expression wins, the order of the tests is part of the logic. With mbps >= 3 tested first, every speed of 3 or more would print SD, and the 4K and HD segments could never run. A chain of bands is written from one end to the other, here from the fastest speed down.
The trailing else is optional. Without it, a speed of 1 makes every expression false, nothing in the chain runs, and the program continues after the statement with nothing printed.
A chain against separate ifs
Write the first three tests as three separate if statements, and each one is evaluated whatever happened before it. With a speed of 30, all three are true, and the segment prints 4K, HD, and SD on three lines. A chain runs at most one segment; separate if statements can run any number of them.
Nesting and chaining both choose between outcomes, and they suit different questions. The chain fits bands of one value, where exactly one band applies. Nesting fits a question that only matters after another has been answered, as the speed check for 4K matters only for a premium account. In nested code the braces decide which if an else belongs to, so an else placed inside the outer braces pairs with the inner if.