I am trying to understand return, break and continue. I know that break will stop the (inner) for loop. For example:
for (int i = 1; i <= 3; i++) {
// inner loop
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
// using break statement inside the inner loop
break;
}
System.out.println(i + " " + j);
}
}
I know the outer loop will go on.
What I don't understand is this here:
for (int i = 0; i < 3; i++) {
String line = null;
switch (a) {
case 0:
line = "Hello";
break;
case 1:
line = "How are you?";
break;
case 2:
line = "What are you doing?";
break;
So it goes to case 0 and then break and the for-loop continues, why? I thought it will break the loop since it's not nested. Or is it because of the switch-statement - it's different than if-statements?
And in this case, it will terminate the whole while-loop... I can't see the difference.
while(winner == false){
input = menuInput.nextInt();
try {
if(input == 0 || input >= 9){
System.out.println("Ungültige Nummer, versuche nochmal!");
break;
}
You can break
a switch
. You can't break
an if
. break
is applied to the closest statement that could be break
ed, so
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
// using break statement inside the inner loop
break;
}
System.out.println(i + " " + j);
}
Here break
refers to the for
.
While here:
for (int i = 0; i < 3; i++) {
String line = null;
switch (a) {
case 0:
line = "Hello";
break;
It refers to the switch
.
Statements that can be break
ed are for
, while
, do...while
, switch
.
For further info, you can see the spec .
The break
applies to the inner-most thing which can be break'd (without a label). In this case, it's the switch
- because the default switch
behavior falls through. First, your code. Change,
for (int i = 0; i < 3; i++) {
String line = null;
switch (a) {
case 0:
line = "Hello";
break;
case 1:
line = "How are you?";
break;
case 2:
line = "What are you doing?";
break;
Could be changed to
loop: for (int i = 0; i < 3; i++) {
String line = null;
switch (a) {
case 0:
line = "Hello";
break loop;
case 1:
line = "How are you?";
break loop;
case 2:
line = "What are you doing?";
break loop;
Now, the second behavior would be fall-through. And that might look something like,
switch (a) {
case 0: case 2: case 4: case 6: case 8:
System.out.println("even < 10");
break;
case 1: case 3: case 5: case 7: case 9:
System.out.println("odd < 10");
break;
}
In switch, statement break means to end switch statement. Take a look at fall trough example .
In the loop, statement break means to end a loop.
Stop looping after using switch statement example:
for (int i = 0; i < 3; i++) {
Boolean shouldBreak = false;
String line = null;
switch (a) {
case 0:
line = "Hello";
shouldBreak = true;
break;
case 1:
line = "How are you?";
shouldBreak = true;
break;
}
if (shouldBreak){
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.