This is how I usually code:
public void foo (int x) {
if (x == 1) {
System.out.println("You entered one!");
} else if (x == 2) {
System.out.println("You entered two!");
} else if (x == 3){
................. and so on.
I realized that the following also produces the same results:
public void foo (int x) {
if (x == 1) {
System.out.println("You entered one!");
return;
}
if (x == 2) {
System.out.println("You entered two!");
return;
}
if (x == 3) {
...... and so on.
Which one would be better to use, even though they produce the same results and why? I realize that there is a switch statement.