June 2018
Beginner
722 pages
18h 47m
English
You can use this form to avoid creating nested blocks and make the code easier to read and understand. For example, look at the following code snippet:
if(n > 5){ System.out.println("n > 5"); } else { if (n == 5) { System.out.println("n == 5"); } else { if (n == 4) { System.out.println("n == 4"); } else { System.out.println("n < 4"); } } }}
These nested if...else statements can be replaced by if...else...if statements as follows:
if(n > 5){ System.out.println("n > 5");} else if (n == 5) { System.out.println("n == 5");} else if (n == 4) { System.out.println("n == 4");} else { System.out.println("n < 4");}
Such code is easier to read and understand.
If you don't need to do anything when n < 4, you can leave out the last ...
Read now
Unlock full access