Java process control (3) nesting

We talked about various loop structures before. There are for loops and while loops, which can complete repeated actions, which is quite convenient. So if many cycles are combined again, how to achieve the effect. In this regard, this paper makes some small exploration on nested loops.

Nested loop

Nested loop: it is nothing more than that one loop is nested with another. In fact, as long as the number exceeds two, no matter how many loop structures are completely contained, it is completely nested. Nesting is divided into outer loop and inner loop. The form of nested loops is indefinite, and any type of loop can be combined.

For loop nesting

Let's start with a for loop and a for loop to realize the universal multiplication formula table:

public class Test{	
	public static void main(String[] args){
		//嵌套循环
        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=i;j++)
            {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
	}
}

While loop nesting

Next, let's take another example of nested printing of 99 multiplication formula table by while loop:

public class Test{	
	public static void main(String[] args){
        int i = 1;
        while(i<=9)
        {
        	int j = 1;
        	while(j<=i)
        	{
        		System.out.print(j+"*"+i+"="+j*i+"\t");
        		j++;
        	}
        	System.out.println();
        	i++;
        }
	}
}

Of course, the result is the same as above! Output results:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>