Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a help to understand this code

Tags:

java

Actually, this is the first time I see a code like this:

class A
{
    public static void main(String args[])
    {
        outer : for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(j > i)
                {
                    System.out.println();
                    continue outer;
                }
                System.out.print("  "  +( i *j ));
            }
        }
        System.out.println();
    }
}

two lines I don't understand:

outer : for(int i=0;i<10;i++) // this seems similar to 'for each'?

continue outer; // I know that 'continue' will break the loop and continue the next turn, but what will do in this situaton?
like image 731
Eng.Fouad Avatar asked Mar 25 '11 17:03

Eng.Fouad


People also ask

Is there a website that explains code?

Denigma - AI that reads and explains code in understandable english.

Why is it so hard to understand code?

“Coding is hard because it's different” Coding is thought to be hard because it's a different type of skill; and “different” in the sense that it's unlike anything most of us have ever experienced before.

How do you Analyse a code written by someone else?

This 4 step process is simple and will save you a lot of time and effort; all you need to do is: Run the code and explore the results. Find the main function or the start point of the code. Run the code under the debugger and fully understand the code's mechanics.


2 Answers

The outer: part is a label. It's basically labelling the loop. The loop itself works just as normal.

The continue outer; means "continue back to the start of the body of the loop labelled outer" (after incrementing and testing i of course). It's sort of like having a break; statement to get out of the inner loop, and then immediately having a normal continue; statement to continue with the next step of the outer loop.

like image 125
Jon Skeet Avatar answered Oct 03 '22 17:10

Jon Skeet


outer : for(int i=0;i<10;i++) 

defines a label for the outer loop, called outer

continue outer; 

means, go to next iteration of the loop labeled outer

like image 37
MByD Avatar answered Oct 03 '22 18:10

MByD