0

I will show you my submitted assignment's answer to give the idea of it

void chkbnch()
{
    System.out.println("\n The students under notice period are =>\n\n");
    for(int i=0;i<25;i++)
    **ol:{**
        int cnm=0;
        int cnm2=0;
        for(int j=0;j<7;j++)
        {
            if(mrks[i][j]>=50)
            {
                cnm++;
            }
            if(cnm==3)
            {
                    //i++;
                    **break ol;**
            }
            if(mrks[i][j]<50)
            {
                cnm2++;
            }
        }
        if(cnm2>=3||cnm<3)
        {
            System.out.println("\n Student id =>"+(i+1));
        }
    }
}

Here I am using break when I don't want the loop to increment and just repeat the loop statement. I know this can be done by also decrementing the loop control but that's not what my question is.

All I want to ask that is this behaviour defined in java or is it just a chance that this is its outcome.

2 Answers2

-1

Seems like you want the continue keyword instead of break. continue breaks the current iteration of the loop and execution jumps back to the evaluation stage where the looping continues if the evaluation passes. break breaks the current iteration of the loop and execution jumps to the first line after the end-of-loop closing brace.

EDIT: If you need the loop increment control to "not increment" when you continue looping, so that, for example, the same object is pulled from the list being looped over after continuing, then you need to roll your own solution.

geneSummons
  • 907
  • 5
  • 15
  • I know that my friend and that's not what my question is. I want to know what should happen it I put the break label at the start of the block only and is that defintive ? And continue does not repeat the loop for the same iteration and that's what I wanted there anyway. – Himanshu Panchal Sep 13 '17 at 18:16
  • Ok, now I see you are talking about using "labels" as a form of `break` (and `goto`). See this answer: https://stackoverflow.com/questions/26430630/how-to-use-goto-statement-correctly. You need both the 'break' label and the 'GoTo' label parts. You could just have the label at the top of the loop, but without the 'GoTo' part, your code would never jump to the label. – geneSummons Sep 13 '17 at 18:33
-1

This use of the break statement is perfectly legal, although it is unusual, and it doesn't do what you say you want.

JLS §14.15 says:

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

The "labeled statement" in your example is the {..} block statement, which is the statement executed by the for loop. When you execute the break, that block statement completes, which returns control to the for loop, which continues by executing the increment i++, testing the condition i<25 and then getting on with the loop.

It has the same behavior as labeling the outer loop directly, and then using continue ol;.

The loop counter will still be incremented. If you want to prevent that, either counteract it with a manual i--; or move i++; out of the for loop header and to the end of the loop body.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • true that and thanks. I think the program ran perfectly because of something else and not this. Although in C(Dev C++), something like this is actually causing an infinite loop int i; for(i=1;i<=10;i++) ol:{ printf("%d",i); if(i%4==0) { goto ol; } } @Boann – Himanshu Panchal Sep 13 '17 at 21:11
  • @HimanshuPanchal That would indeed be infinite in C++. `goto` *goes* to the labeled statement directly, so the `for` loop doesn't get a chance to do `i++`. Whereas `break` *completes* the labeled statement, thus completing the current loop iteration, which returns control to the `for` statement to see if it wants to loop again. – Boann Sep 13 '17 at 23:30