Does anyone knows what the group_skip do?
Maybe it is a basic programming, but I've been programming using Java for some years and just found it today.
group_skip: do {
event = stepToNextEvent(FormController.STEP_OVER_GROUP);
switch (event) {
case FormEntryController.EVENT_QUESTION:
case FormEntryController.EVENT_END_OF_FORM:
break group_skip;
}
} while (event != FormEntryController.EVENT_END_OF_FORM);
Thanks!
This is a labelled loop, when break group_skip; statement is executed, it will jump out of the do while loop which is labelled as group_skip
boolean isTrue = true;
outer: for (int i = 0; i < 5; i++) {
while (isTrue) {
System.out.println("Hello");
break outer;
} // end of inner while
System.out.println("Outer loop"); // does not print
} // end of outer loop
System.out.println("Good Bye");
This outputs
Hello
Good Bye
You can get the concept clear here.
There is a labelled for loop called outer and there is inner while loop
When inner for loop is being executed, it encounters break outer; statement
The outer for loop has a System.out.println"Outer loop" statement but that does not get printed.
This is because break outer causes the control to jump out of labelled for loop directly
Now this example for continue statement
outer: for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.println("Hello");
continue outer;
} // end of inner loop
System.out.println("outer"); // this won't print
} // end of outer loop
System.out.println("Good bye");
This prints
Hello
Hello
Hello
Hello
Hello
Good bye
There is a labelled for loop here and an inner for loop
Inner for loop prints Hello and continues to the outer loop.
Because of this, the statements below inner for loop are skipped and outer loop continues to execute.
At the end of outer for loop, Good Bye is printed
Hope this makes everything clear.
group_skip is a label. Labels allow you to break or continue specific loops when you've got them nested.
Here's what Oracle has to say on the subject.
when ever we use simple break statement then we can only transfer control from inner most loop to the outer most (if we have nesting of loops). for exampel
for(int i=0; i < 10; i++){
if(i==5){
break;
}
}
statement x;
will simply transfer the control to statement x. But if you use it inside nested loops then it will work differently.
for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++)
if(i==5){
break;
}
}
statement y;
}
statement x;
in this case it will send the control to statement y. If you want to send the control from innermost loop to either outermost loop or outside the loop then you need such a break statements with labels. Just do it from your self and you will see interesting output.. :)
group_skip is a label used for things like break. (Also goto and jump in other languages)
In java specifically it would be used to break from a code block identified by the label, behaving just like a break statement in a while loop, except breaking from a labeled code block.
Here is some more discussion on the topic
Related
I don't know interpret this code:
t:
while (true) {
break t;
}
Can you help me?
This construct called a "labeled break" and can be used to simultaneously break out of multiple nested loops.
To quote an example from an Oracle tutorial:
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
Here, an unlabeled break (i.e. simply break) would only terminate the inner loop, whereas break search terminates both loops at once.
See Is using a labeled break a good practice in Java? for a relevant debate.
It's just a label that you place anywhere and then you can "break" or "continue" depending on your conditions.It can be used in nested if-else with for loopings in order to break several loops.
Here break t; simply means break out from the while loop that is labelled as t.
It's useful for breaking out of nested loops
This is called a 'level', the alternative to 'goto' in other languages. Although 'goto' is a reserved word in Java, it is not used in the language; Java has no goto. However, it does have something that looks a bit like a jump tied in with the break and continue keywords: a level.
A label is an identifier followed by a colon, like this: label1:
The only place a label is used in Java is right before an iteration statement. The reason to put a label before an iteration is if you’re going to nest another iteration or a switch inside it. That’s because the break and continue keywords will normally interrupt only the current loop, but when used with a label, they’ll interrupt the loops up to where the label exists:
label1:
outer-iteration {
inner-iteration {
//...
break; // (1)
//...
continue; // (2)
//...
continue label1; // (3)
//...
break label1; // (4)
}
}
In (1), the break breaks out of the inner iteration and you end up in the outer iteration. In (2), the continue moves back to the beginning of the inner iteration. But in (3), the continue label1 breaks out of the inner iteration and the outer iteration, all the way back to label1. Then it does in fact continue the iteration, but starting at the outer iteration. In (4), the break label1 also breaks all the way out to label1, but it does not re-enter the iteration. It actually does break out of both iterations.
Hope this help!!!
This question already has answers here:
Having issue with the concept of extra semi-colon at the end of for loop in Java
(5 answers)
Closed last month.
When I am doing my assignments I done a small mistake by placing ; in the for loop like the following code.
for(i=0;i<n;i++);{
n=n*i;
}
When I compiled the program its compiled with no error but I did not get the output for a while. Then later figured out that I misplaced the semicolon in for loop. What was the bug when I place semicolon after for loop.
class A
{
public static void main(String args[])
{
int i, n=1;
for(i=1; i<=5; i++);
n=n*i;
System.out.println(n);
}
}
I am getting output for the following code as 6 instead of 120.
When you do this: for(i=0;i<n;i++); you are essentially doing this: for(i=0;i<n;i++) {}. This translates to a loop with no body.
This also happens for while loops: while(..);{<Foo>}. The extra ; will make execute only once.
The same goes for if statements. Doing if(a==b);{<Foo>} will still execute <Foo> for the same reason. If a == b, then, the empty statement will be taken into consideration. Afterwards, <Foo> will be executed. This can give the wrong impression that Java will treat a false as a true since if a != b, then, <Foo> would still be executed.
for loop can execute only one code block or one statement, even empty ones. So semicolon here represents empty statement (statement which does nothing).
In other words you can think of for(int i; i<n; i++); as for(int i; i<n; i++){}.
So code like
for(int i; i<n; i++);{
foo();
}
is same as
for(int i; i<n; i++){
//do nothing, except i++
}
//after loop finishes
{
foo();
}
and
for(int i; i<n; i++);
foo();
is simply
for(int i; i<n; i++){
}
foo();
About
int i, n=1;
for(i=1; i<=5; i++);
n=n*i;
System.out.println(n);
I am getting output for the following code as 6 instead of 120.
As explained earlier your code
for(i=1; i<=5; i++);
n=n*i;
is same as
for(i=1; i<=5; i++){}
n=n*i;
which means that it your loop will not execute n=n*i but will try to execute {} which does nothing. Also at end of each iteration i will be incremented because of i++ so when i will become 5 your and i<5 will be false flow of control will leave loop and will execute n=n*5 which means that n will become n=1*5 which is 5.
If you want to get as result 120 by executing n=n*i in each loop simply remove semicolon after loop.
for(i=1; i<=5; i++)//<--removed semicolon
n=n*i;
BTW prefer to place code which should be executed by loop or if statement in code blocks,
for(i=1; i<=5; i++){
n=n*i;
}
This makes your code easier to read and maintain. Also if you will make again your mistake and you will use some auto formatting tool which will indent your code for you you will see that it will be formatted as
for(i=1; i<=5; i++)
;
{
n=n*i;
}
which makes spotting such errors very easy.
If you place semicolon after a for loop then it is technically correct syntax. Because it is considered as an empty statement - that means nothing to execute.
In your case -
for(i=0;i<n;i++);
An empty statement executed up to n times. Or in other words the for-loop simply runs n times.
The curly braces {} after the for loop executed only once. Now the code block inside the curly braces {} considered as a floating block.
You didn't get an output because the semi-colon ended the statement, [ for(I = 0; I < n; I++ ]
The code should be:
for(i=0;i<n;i++) {
n=n*1;
}
The reason why you're getting 6 is simple. At the last iteration the value of i turns from 5 to 6, the loop won't do another iteration because i=6 and it doesn't satisfy the condition for the for loop any more. Since n=1 and i=6 then the output of n*i is 6.
I have two "for" loops.
for(n=0;n<6;n++){
for(w=n;w<6;w++){
// if w==4 then go to first loop an continue from n=4!!
}
}
How can i jump to n=4 when w takes value 4; Like old Basic command "goto"..
Thanks
How can i jump to n=4 when w takes value 4; Like old Basic command
"goto"
You can use break to "jump" out of the current loop back to the outer loop:
for(n=0;n<6;n++){
// other code
for(w=n;w<6;w++){
// if w==4 then go to first loop an continue from n=4!!
if (w == 4) {
n = 4;
break;
}
}
// other code
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to Break from main/outer loop in a double/nested loop?
I have the following situation:
for(int i = 0; i < schiffe.length-1; i++){
if(schiffe[i].schaden){
schlepper.fliege(schiffe[i].x,
schiffe[i].y,
schiffe[i].z);
schlepper.wirdAbgeschleppt = schiffe[i];
for(int k = 0; k < stationen.length-1; k++){
if(stationen[k].reparatur == null){
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
break;
}
}
}
}
I want to
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
be performed once and then break out of the inner loop and continue with the for(int i... loop. So I used a break in my code. But I am not really sure if this is right. Does the break cause a break for all loops or just for the second loop?
It breaks only the inner loop, and therefore does what you want. To break more than one level, in Java, you can use a "labelled break" like so:
schiffe_loop:
for(int i = 0; i < schiffe.length-1; i++){
some_stuff();
for(int k = 0; k < stationen.length-1; k++){
if (something_really_bad_happened()) {
break schiffe_loop;
}
}
}
But usually you will not need this.
You may consider making a separate method for the inner loop anyway; it's something you can easily give a name to ("find_available_station" or something like that), and will probably need somewhere else.
Also, please be aware that your loops right now will miss the last element of each array. Think carefully about why you are using < instead of <=; it's exactly so that you use every value from 0 up to but not including the specified one.
break will break the innermost loop, in your case 2nd loop.
To make it break the outer loop you can use a labeled break as:
OUTER:for(...) {
for(...) {
break OUTER;
}
}
break only breaks out of the innermost loop.
break will cause break for closest loop only (second loop)
Just the second one. You can use "goto" of course.
Break statement terminates the closest enclosing loop or switch statement in which it appears in many languages but you can try it and be sure 100%.
put the inner two loops in a function and use return to break out of both of them.
If you need more control, you can use a labeled break. More info here. As others have said, break will close the closest loop.
What about using a boolean you call 'found' and use it like this:
while(!found) {
for(...) {
//todo
}
}
I Think You Can Do One More Thing..
for(int k = 0; k < stationen.length-1; k++){
if(stationen[k].reparatur == null){
schlepper.fliege(stationen[k].x,
stationen[k].y,
stationen[k].z);
k = stationen.length + 1 ;
}
}
let say my code look like below
for(..)
for(..)
for(..){
break; //this will break out from the most inner loop OR all 3 iterated loops?
}
Your example will break out of the innermost loop only. However, using a labeled break statement, you can do this:
outer:
for(..)
for(..)
for(..){
break outer; //this will break out from all three loops
}
This will only break out from the inner loop. You can also define a scope to break out from. More from the language specs:
A break statement with no label
attempts to transfer control to the
innermost enclosing switch, while, do,
or for statement of the immediately
enclosing method or initializer block;
this statement, which is called the
break target, then immediately
completes normally.
Yes, without labels it will break only the most inner loop.
Instead of using labels you can put your loops in a seperated function and return from the function.
class Loop {
public void loopForXx() {
untilXx();
}
private void untilXx() {
for()
for()
for()
if(xx)
return;
}
}
From the most inner loop :)
int i,j,k;
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
for(k = 0; k < 2; k++)
{
printf("%d %d %d\n", i, j, k);
break;
}
Will produce :
0 0 0
0 1 0
1 0 0
1 1 0
You should take a look here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html
as often mentioned i don't like to break with a label eather. so while in a for loop most of the time i'm adding a boolean varible to simple exit the loop.. (only if i want to break it of cause;))
boolean exit = false;
for (int i = 0; i < 10 && !exit; i++) {
for (int j = 0; j < 10 && !exit; j++) {
exit = true;
}
}
this is in my opinion more elegant than a break..
Many people here don't like labels and breaking. This technique can be compared to using a 'goto' statement, a flow control statement which allows jumping out of a block of code in a non-standard way, obliviating use of pre- and post conditions. Edsger Dijkstra published a famous article in Communications of the ACM, march 1968, 'Goto statement considered harmful' (it's a short read).
Using the same reasoning presented in the article, returning from inside an iteration as suggested by TimW is also bad practice. If one is strict, to create readable code, with predictable entry- and exit points, one should initialize the variable which will hold the return value (if any) at the beginning of the method and return only at the end of a mehod.
This poses a challenge when using an iteration to perform a lookup. To avoid using break or return one inevitably ends up with a while-loop with a regular stop condition and some boolean variable to indicate that the lookup has succeeded:
boolean targetFound = false;
int i = 0;
while (i < values.size() && ! targetFound ) {
if (values.get(i).equals(targetValue)) {
targetFound = true;
}
}
if (!targetFound) {
// handle lookup failure
}
Ok, this works, but it seems a bit clunky to me. First of all I have to introduce a boolean to detect lookup success. Secondly I have to explicitly check targetFound after the loop to handle lookup failure.
I sometimes use this solution, which I think is more concise and readable:
lookup: {
for(Value value : values) {
if (value.equals(targetValue)) {
break lookup;
}
}
// handle lookup failure here
}
I think breaking (no pun intended) the rule here results in better code.
it will breake from most inner loop,
if you want to break from all, you can hold a variable and change its value when you want to break, then control it at the beginning of each for loop