Nested IFs and Switches

OliverE

Power member.
Reputation
0
Nested IFs and Switches

In java we tend to use IF statements rather alot. Look at the code below to see how ugly and complex our code can turn into if we use too many IF statements.

[java]import java.util.*;

public class ABCs
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char letter;

System.out.println("Type an upper case letter and "+
"I'll print a cattle breed that comes to mind.");
do
{
letter = sc.next().charAt(0);
} while (letter < 'A' || letter > 'Z');

// many choices:
if (letter == 'A')
{
System.out.println("A is for Angus.");
}
else
{
if (letter == 'B')
{
System.out.println("B is for Boran.");
}
else
{
if (letter == 'C')
{
System.out.println("C is for Corriente.");
}
else
{
if (letter == 'D')
{
System.out.println("D is for Droughtmaster.");
}
else
{
if (letter == 'E')
{
System.out.println("E is for Eringer.");
}
else
{
if (letter == 'F')
{
System.out.println("F is for Freiburger.");
}
else
{
if (letter == 'G')
{
System.out.println("G is for Galloway.");
}
else
{
if (letter == 'H')
{
System.out.println("H is for Hereford.");
}
else
{
if (letter == 'I')
{
System.out.println("I is for Illawarra.");
}
else
{
if (letter == 'J')
{
System.out.println("J is for Jersey.");
}
else
{
if (letter == 'K')
{

System.out.println("K is for Khillari.");
}
else
{
if (letter == 'L')
{
System.out.println("L is for Lebedinskaja.");
}
else
{
if (letter == 'M')
{
System.out.println("M is for Mandalong.");
}
else
{
if (letter == 'N')
{
System.out.println("N is for Nguni.");
}
else
{
if (letter == 'O')
{
System.out.println("O is for Oksh.");
}
else
{
if (letter == 'P')
{
System.out.println("P is for Pinzgauer.");
}
else
{
if (letter == 'R')
{
System.out.println("R is for Rodbroget.");
}
else
{
if (letter == 'S')
{
System.out.println("S is for Senepol.");
}
else
{
if (letter == 'T')
{
System.out.println("T is for Tuli.");
}
else
{
if (letter == 'V')
{
System.out.println("V is for Vorderwalder.");
}
else
{
if (letter == 'W')
{
System.out.println("W is for Wagyu.");
}
else
{
if (letter == 'Z')
{
System.out.println("Z is for Zebu.");
// Q, U, X, and Y have no breeds:
}
else
{

System.out.println("I know of no breed that begins with "+letter+".");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}[/java]

Is there away around this issue? Yes!
We can use what we call a Switch or Case, to see if something meets certain criteria. Although there is still alot of lines of code, it is much tidier and has the exact same output as the code above.

[java]
import java.util.*;

public class SwitchABCs
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char letter;

System.out.println("Type an upper case letter and "+
"I'll print a cattle breed that comes to mind.");
do
{
letter = sc.next().charAt(0);
} while (letter < 'A' || letter > 'Z');

// many choices:
switch (letter)
{
case 'A':
System.out.println("A is for Angus.");
break;
case 'B':
System.out.println("B is for Boran.");
break;
case 'C':
System.out.println("C is for Corriente.");
break;
case 'D':
System.out.println("D is for Droughtmaster.");
break;
case 'E':
System.out.println("E is for Eringer.");
break;
case 'F':
System.out.println("F is for Freiburger.");
break;
case 'G':
System.out.println("G is for Galloway.");
break;
case 'H':
System.out.println("H is for Hereford.");
break;
case 'I':
System.out.println("I is for Illawarra.");
break;
case 'J':
System.out.println("J is for Jersey.");
break;
case 'K':
System.out.println("K is for Khillari.");
break;
case 'L':
System.out.println("L is for Lebedinskaja.");
break;
case 'M':
System.out.println("M is for Mandalong.");
break;
case 'N':
System.out.println("N is for Nguni.");
break;
case 'O':
System.out.println("O is for Oksh.");
break;
case 'P':
System.out.println("P is for Pinzgauer.");
break;
case 'R':
System.out.println("R is for Rodbroget.");
break;
case 'S':
System.out.println("S is for Senepol.");
break;
case 'T':
System.out.println("T is for Tuli.");
break;
case 'V':
System.out.println("V is for Vorderwalder.");
break;
case 'W':
System.out.println("W is for Wagyu.");
break;
case 'Z':
System.out.println("Z is for Zebu.");
break;
default:
System.out.println("I know of no breed that begins with "+letter+".");
break;
}
}
}
[/java]

Tasks
Using the example above, print the sentence: "I am amazing" if the letters A,B or C are input
OR
"I suck" if any other letter is input.

500 bytes for the first person to get it fully functional.
An extra 500 bytes for the first person who spots a way of keeping the code to a minimum by not repeating lines of code.

Good Luck
Let me know if you need help
 
Back
Top