Java: Control Structures
Conditionals
if (condition)
{
}
[else if (condition)
{
}]
[else
{
}]
switch statements
- The selector variable must be one of the following types:
- Primative types:
byte, short, char or int
- Wrapper types:
Character, Byte, Short, Integer
- enumerated types
String type
- The following primative types cannot be used for selector variables:
boolean, long, float, double
String ret;
switch (x)
{
case 0:
rtn = "0";
break;
case 1:
rtn = "1";
break;
case 2:
rtn = "2";
break;
case 3:
rtn = "3";
break;
case 4:
rtn = "4";
break;
case 5:
rtn = "5";
break;
case 6:
rtn = "6";
break;
case 7:
rtn = "7";
break;
case 8:
rtn = "8";
break;
case 9:
rtn = "9";
break;
default:
rtn = "";
};
while Loops
while (condition)
{
}
do while Loops
do
{
}
while (condition);
for Loop
for (initialization; termination; increment)
{
}
for Loop - to iterate thru an array or Collection
int[] intArray = {1, 2, 3, 4, 5};
for (int item : intArray)
{
System.out.println ("item=" + item)
}
break Statement
continue Statement
return Statement