Java 嵌套循环示例
在Java中,循环被称为控制语句,因为它们根据某个条件决定程序的执行流程。Java允许循环的嵌套,当我们将一个循环放在另一个循环内部时,就称为嵌套循环。嵌套循环在我们需要迭代一个矩阵数组或解决基于模式的问题时非常有用。
在本文中,我们将学习Java中的嵌套循环,并提供一些示例。
嵌套循环
我们可以为以下控制语句创建嵌套循环:
- For循环
-
If else语句
-
While循环
-
Do while循环
-
For each循环
让我们通过一些示例讨论这些嵌套循环
嵌套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’。前两个循环将运行4次,在每次迭代中,’mtr1’和’mtr2’的值将存储在第三个矩阵’add’中。后两个循环将在屏幕上打印结果。
嵌套if语句
语法
if(conditional expression){
// code to be executed
if(conditional expression){
// code to be executed
}
}
示例
public class Main {
public static void main(String []args){
String Fname= "Tutorials";
String Lname= "point";
// Nested if block
if(Fname == "Tutorials") {
if(Lname == "point") {
System.out.println(Fname + Lname);
} else {
System.out.println("Last name not matched");
}
} else {
System.out.println("Not able to fetch info!");
}
}
}
输出
Tutorialspoint
在上面的代码中,我们声明并初始化了两个字符串变量。我们使用了嵌套的if条件语句。在第一个if代码块中,我们检查了第一个姓名是否等于“Tutorials”。在第二个代码块中,我们检查了姓氏是否等于“point”。如果其中任何一个条件为false,那么else代码块将被执行。在这种情况下,两个条件都为true,因此我们得到了连接的结果。
嵌套的while循环
语法
while (conditional expression) {
// code will be executed till conditional expression is true
while (conditional expression) {
// code will be executed till 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循环将在屏幕上打印结果。
结论
一个循环内部的循环被称为嵌套循环。我们已经通过示例理解了嵌套的for循环、嵌套的if块和嵌套的while循环。当我们需要对二维数组和矩阵进行操作时,我们使用它们。我们还在基于模式的问题中使用它们。