Java Extras
More Loops
To iterate through items in an array without an index, you can use the for-each loop:
for(int num : array1){
System.out.println(num);
}
This loop header can be read in English as:
for each num in array1
It should be used whenever you just need the elements in an array without the index, such as to find the sum of a list of numbers.
do-while loop
Use a do-while loop if you want the code in the loop to always run once whether or not the condition is true.
A guessing game could be re-written with a do-while loop:
public static void guessGame()
{
int num;
do {
System.out.println("Guess a Number");
num = getInput();
} while(num != 5);
System.out.println("Correct!");
}
This will print "Guess a number" and run getInput
until the num
is 5.
switch statement
The switch
statement can be used instead of an if-else statement to do something different for different values of a variable. Use break
to prevent the code from "falling through" and executing statements after the correct one.
char selection = 'B';
switch (selection){
case 'A':
System.out.println("Great!");
break;
case 'B':
System.out.println("Good enough!");
break;
case 'C':
System.out.println("You could do better");
break;
default:
System.out.println("Terrible!");
break;
}
This will print out "Good enough!". The switch statement can be used for equality comparison for any primitive variables and (as of Java 7), for Strings too.
break continue return
break
can be used to exit from a loop. This will cause the program to continue executing code from after the loop block.
for(int i=0; i<100; i=i+1) {
if(i == 7){
break;
}
System.out.println(i);
}
System.out.println("finished");
This code will output the following:
0 1 2 3 4 5 6 finished
It usually should be used when there's a special circumstance that cannot be easily covered by the main condition of a loop.
continue
Use continue
to skip the current iteration through a loop without exiting the loop entirely. This code will print the numbers from 0 through 6, except 3:
for(int i=0; i<7; i=i+1) {
if(i == 3){
continue;
}
System.out.println(i);
}
System.out.println("finished");
This will output:
0 1 2 4 5 6 finished
return
When return
is called, the program returns to the code that originally called the method. You can even return nothing to end execution of the method and return immediately to the previous code.
Here's some code for finding a number in an array:
public static void findNum2(int[] array, int num)
{
for (int i = 0; i < array.length; i++ ) {
if (array[i] == num) {
System.out.println(num + " is at index " + i);
return; //this will leave the method when `num` is reached
}
}
System.out.println(num + " not in array");
}
See break continue return for more details.
Java shortcuts
There are many shortcuts in Java that let you save some keystrokes:
// declare variables of the same type:
int a, b, c;
// assign multiple variables to one value:
a = b = c = 5;
//assign variable to new value based on simple math operations
int index = 2;
index += 1; // = 3
index *= 4; // 12
index -= 2; // 10
//increment (postfix)
int count = 0;
count++; // count = 1
count--; // count = 0
// sometimes you want to use a value and increment it at the same time.
// prefix will increment it before that usage, while postfix will increment it after:
int i = 3;
System.out.println(i); //prints 3
System.out.println(++i); //prints 4 (prefix)
System.out.println(i++); //prints 4 (postfix)
System.out.println(i); //prints 5