Java 嵌套循环及示例
Java 中的循环被称为控制语句,因为它们根据某些条件决定程序的执行流程。 Java 允许嵌套循环,当我们将一个循环放在另一个循环内时,我们称之为嵌套循环。嵌套循环在我们需要遍历矩阵数组以及我们需要进行任何基于模式的问题时非常有用。
在本文中,我们将学习 Java 嵌套循环及示例。
我们可以为以下控制语句创建嵌套循环:
让我们通过一些示例来讨论这些嵌套循环。
嵌套 for 循环
在任何编程语言中,for 循环是最常用的控制语句,因为它易于理解和实现。循环迭代直到给定条件为真。嵌套 for 循环是指另一个 for 循环中的 for 循环。
语法
for (initial expression; conditional expression; increment/decrement expression){
// code to be executed
for (initial expression; conditional expression; increment/decrement expression) {
// code to be executed
}
}
初始表达式 - 循环开始时执行一次。
条件表达式 - 代码将执行直到条件表达式为真。
增量/减量表达式 - 用于增量/减量循环变量。
示例
以下程序将使用嵌套 for 循环执行两个矩阵的加法。
public class Main{
public static void main(String args[]){
int mtr1[][] = {{2,7},
{9,2}};
int mtr2[][] = {{6,3},
{5,1}};
int add[][] = new int[2][2];
// Nested for loop
for (int i= 0 ; i < 2 ; i++ ){
for (int j= 0 ; j < 2 ;j++ ){
add[i][j] = mtr1[i][j] + mtr2[i][j];
// Performing addition
}
}
System.out.println("Sum of given two matrices =");
// Nested for loop to print the resulted matrix
for (int i= 0 ; i < 2 ; i++ ){
for (int j= 0 ; j < 2 ;j++ ){
System.out.print(add[i][j]+"\t");
}
System.out.println();
}
}
}
输出
Sum of given two matrices = 8 10 14 3
在上面的代码中,我们取了两个大小为 2 行 2 列的矩阵 mtr1 和 mtr2。前两个 for 循环将运行 4 次,并且在每次迭代期间,mtr1 和 mtr2 的值将存储在第三个矩阵 add 中。最后两个 for 循环将在屏幕上打印结果。
嵌套 while 循环
在while 循环中,条件在进入循环体之前进行检查,因此它是一个入口控制循环。嵌套 while 循环是指另一个 while 循环内的 while 循环。
语法
while (conditional expression) {
// code will be executed till the conditional expression is true
while (conditional expression) {
// code will be executed till the conditional expression is true
increment/decrement expression;
// to increment/decrement loop variable
}
increment/decrement expression;
// to increment/decrement loop variable
}
示例
以下程序将使用 while 循环执行两个矩阵的加法。
public class Main{
public static void main(String args[]){
int mtr1[][] = {{2,7},{9,2}};
int mtr2[][] = {{6,3},{5,1}};
int add[][] = new int[2][2];
int i=0;
// Nested while loop to perform addition
while(i<2){
int j=0;
while(j<2){
add[i][j] = mtr1[i][j] + mtr2[i][j];
j++;
}
i++;
}
System.out.println("Sum of given two matrices =");
i=0;
// Nested while loop to print result
while(i<2){
int j=0;
while(j<2){
System.out.print(add[i][j]+"\t");
j++;
}
System.out.println();
i++;
}
}
}
输出
Sum of given two matrices = 8 10 14 3
在上面的代码中,我们遵循了与示例 1 程序中相同的逻辑,但这里我们使用了嵌套 while 循环而不是 for 循环。同样,我们取了两个大小为 2 行 2 列的矩阵 mtr1 和 mtr2。前两个 while 循环将运行 4 次,并且在每次迭代期间,mtr1 和 mtr2 的值将存储在第三个矩阵 add 中。最后两个 while 循环将在屏幕上打印结果。
嵌套 do while 循环
在do while 循环中,在检查给定的测试条件之前,循环体先执行,因此它是一个出口控制循环。嵌套 do while 循环也可以通过在一个do-while 循环中使用另一个do-while 循环来创建。
语法
do{
//statement for the outer loop
do{
//statement for the inner loop inner
}while(condition B);
}while(condition A)
示例
此 Java 程序使用嵌套的do-while 循环来打印星号(*)网格。
public class Main {
public static void main(String[] args) {
int rows = 3;
int cols = 4;
int i = 1;
do {
int j = 1;
do {
System.out.print("* ");
j++;
}
while (j <= cols);
System.out.println();
i++;
}
while (i <= rows);
}
}
输出
* * * * * * * * * * * *
在上面的代码中,外循环运行三次,每次运行一行,并在每次行迭代时初始化一个内循环。内循环运行四次,每次运行一列,并打印一个星号后跟一个空格。在打印完一行所需的星号后,内循环结束,程序移到下一行。此过程重复,直到打印完所有行。外循环确保内循环为指定行数运行,从而生成一个3x4 星号网格。
嵌套 for each 循环
一个for each 循环是一种特殊的重复控制结构,它允许你有效地编写一个需要执行特定次数的循环。嵌套 for each 循环是指在一个 for each 循环中使用另一个 for each 循环。
语法
for (Type[] arrayElement : outerArray) {
for (Type element : arrayElement) {
// Inner loop statements
}
}
示例
此 Java 程序演示了如何使用嵌套 for-each 循环遍历字符串的二维数组并打印其元素。
public class NestedForEachExample3 {
public static void main(String[] args) {
String[][] array = {
{"apple", "banana", "cherry"},
{"dog", "elephant", "frog"},
{"grape", "honey", "ice"}
};
for (String[] row : array) {
for (String element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
数组变量是一个二维数组,包含三行,每行包含三个字符串。外层 for-each 循环遍历数组的每一行。对于每一行,内层 for-each 循环遍历该行的元素。每个字符串元素后跟一个空格打印。处理完一行中的所有元素后,二维数组的每一行结果都打印在新的一行上。
结论
一个循环在另一个循环内被称为嵌套循环。我们已经理解了嵌套 for 循环和嵌套 while 循环以及示例。当我们需要对二维数组和矩阵执行操作时,我们会使用这些循环。我们还在基于模式的问题中使用它们。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP