首页  >  文章  >  Java  >  Java 中的嵌套循环

Java 中的嵌套循环

WBOY
WBOY原创
2024-08-30 15:25:34444浏览

内部由另一个循环组成的循环作为嵌套结构构建,外部循环监视内部循环的执行次数,这种结构中的循环称为嵌套循环。它也称为循环中的循环。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

Java 中嵌套循环的语法

以下是不同的语法:

1。嵌套 For 循环

for(initialize;cond1;increment/decrement)
{
for(initialize;cond2;increment/decrement)
{
//body of loop2
}
//body of loop1
}

2。嵌套 While 循环

while(cond1)
{
while(cond2)
{
// body of loop2
}
//body of loop1
}

3。嵌套 Do-while 循环

do{
do{
//body of loop 2
}while(cond2)
//body of loop 1
}while(cond1

4。嵌套异质循环

循环的嵌套没有限制,只能嵌套相似类型的循环。我们可以将任何循环嵌套在任何其他循环中,例如 while 嵌套在 for 循环中或 while 循环嵌套在 do-while 循环中,所有其他可能的组合都适用。

for(initialize;cond1;increment/decrement)
{
while(cond2){
//body of loop2
}
// body of loop1
}

流程图

Java 中的嵌套循环

说明:

在上面的流程图中,首先,当我们进入程序主体时,会执行初始化或打印语句 get 等语句。一旦发现循环,程序就会检查外循环的条件;如果返回true,则进入循环;否则,循环结束,循环后程序的其余语句执行。

一旦进入外循环并遇到内循环,如果有变量则初始化,然后检查内循环条件,如果返回 true 则程序进入内循环;否则,回到Loop1的末尾并执行增量/减量以再次执行Loop1。

如果 cond2 返回 true,则执行 Loop2 语句,并且计数器变量会递增/递减。此过程重复多次,然后程序从 Loop2 退出,然后从 Loop1 退出并移至循环后的语句。

嵌套循环在 Java 中如何工作?

每个循环内部都包含以下 3 个内容:

  1. 初始化:这一步是指初始化计数器的值。
  2. 条件: 此条件是指继续执行循环所需的比较。
  3. 计数器递增/递减: 这是指一个循环结束后对计数器执行的操作。

对于嵌套循环,将针对其中的每个循环检查上述三个步骤。因此,对于外循环的每个流程,内循环都会完整执行,这意味着如果我们有 m 个流程用于外循环,n 个流程用于外循环,那么该循环将一起执行 m*n 次。

注意:可以嵌套的循环数量以及可以嵌套的循环类型没有限制,可以使用任何类型的循环,但必须小心,因为它会影响程序的时间复杂度,从而影响性能。

例如:

for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.println(“LOOP DEMO”);
}
}

在上面给出的嵌套循环的情况下:

外环

Counter variable = i
Condition – i<5
Increment/decrement – i++

内循环

Counter variable = j
Condition – j<5
Increment/decrement – j++

Java 中嵌套循环的示例

下面给出了 Java 中嵌套循环的示例:

示例#1

让我们编写一个程序来打印以下图案:

*

**

***

****

*****

代码:
package Try;
class Demo
{
//static method
public static void main(String[] args)
{
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(i>=j) {
System.<em>out</em>.print('*');}
}
System.<em>out</em>.println("\n");
}
}
}
输出: Java 中的嵌套循环 说明:

在上面的程序中,使用了 2 个内循环,因为需要打印的图案可以类似于以“*”为元素的 5 行 5 列的二维数组。

此外,if(i

I=1 I=2 I=3 I=4 I=5
J=1 *
J=2 * *
J=3 * * *
J=4 * * * *
J=5 * * * * *

We can easily configure that we need to print * only when i

Example #2

Let’s see the example to print a 2D matrix.

Code:

package Try;
class Demo
{
public static void printMatrix(int arr[][][]){
int i=0,j=0;
while(i<arr.length){
while(j<arr[i].length){
for (int k = 0; k <arr[j].length; k++)
System.out.println("arr[" + i + "][" + j + "]["+ k + "] = "+arr[i][j][k] );
j++;
}
i++;
j=0;
}
}
public static void main(String[] args)
{
int arr[][][] ={ { { 10, 2 }, { 30, 4 } }, { { 51, 6 }, { 79, 8 } } };
printMatrix(arr);
}
}

Output:

Java 中的嵌套循环

Explanation:
In the above program, we have used 3 loops to print the elements stored in a 3D Matrix using 3 counter variables. Thus, any number of loops can be tested as well as any type of loop can be used.

Conclusion

Nested loop refers to the placement of loop inside the loop to execute the operations that need multiple loop traversals such as printing star patterns or search operations on any collection data structure array or linked list. Although it helps to make our task easier, it also increases the complexity of the program thus must be used in an efficient manner.

以上是Java 中的嵌套循环的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn