Java基础系列9-流程语句之循环结构

x33g5p2x  于2021-12-18 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(335)

一.循环结构

1.1 for循环

执行流程:

  1. 执行初始化语句
  2. 执行判断条件语句,看其结果是true还是false
    1)如果是false,循环结束。
    2)如果是true,继续执行。
  3. 执行循环体语句
  4. 执行控制条件语句
  5. 回到2继续

循环结构 for循环语句格式:

for(初始化语句;判断条件语句;控制条件语句) {
         循环体语句;
    }

//实例
for (int x = 1; x <= 5; x++) {
    System.out.println("HelloWorld");
}

for (int i = 0; i < collection.size(); i++) {
        
}

执行流程图:

arraylist可以用for遍历;
collection没有索引,不能用for循环;

代码:

package Java_study;

public class for1 {
	public static void main(String[] args) {
		//for循环1
		for (int x = 1; x <= 5; x++) {
			System.out.println("HelloWorld");
		}
		
		System.out.println("======");
		
		//for循环2  输出1-5
		for (int i = 1; i <= 5 ; i++) {
			System.out.println("i:" + i);
		}
		System.out.println("======");
		
		//for循环3 输出5-1
		for (int i = 5; i > 0; i--) {
			System.out.println("i:" + i) ;
		}
		
		System.out.println("======");
		
		//for循环4 求和
		int sum = 0;
		for (int i = 1; i <= 5; i++) {
			sum += i;
		}
		System.out.println("结果为:" + sum);
		
		
	}

}

测试记录:

HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
======
i:1
i:2
i:3
i:4
i:5
======
i:5
i:4
i:3
i:2
i:1
======
结果为:15

1.2 while循环

执行流程:

  1. 执行初始化语句
  2. 执行判断条件语句,看其结果是true还是false
    1)如果是false,循环结束。
    2)如果是true,继续执行。
  3. 执行循环体语句
  4. 执行控制条件语句
  5. 回到B继续

循环结构 while语句格式:

//基本格式
while(判断条件语句) {
     循环体语句;
}

//扩展格式
初始化语句;
while(判断条件语句) {
     循环体语句;
     控制条件语句;
}

//实例
while(x<=5) {
	System.out.println("HelloWorld");
	x++;
}

执行流程图:

代码:

package Java_study;

public class while1 {
	public static void main(String[] args) {
		
		//while循环1
		int x = 1;
		while (x <= 5) {
			System.out.println("HelloWorld");
			x++;
		}
		
		//while循环2 求和
		int sum = 0;
		
		int i = 1;
		while (i <= 100) {
			//累加即
			sum += i;
			i++;
		}
		
		//输出结果
		System.out.println("sum:" + sum);
	}

}

测试记录:

HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
sum:5050

1.3 do while循环

执行流程:

  1. 执行初始化语句
  2. 执行循环体语句
  3. 执行控制条件语句
  4. 执行判断条件语句,看其结果是true还是false
    1)如果是false,循环结束。
    2)如果是true,继续执行。
  5. 回到2继续

循环结构 do while语句格式:

//基本格式
do {
     循环体语句;
}while(判断条件语句);

//扩展格式
   初始化语句;
do {
     循环体语句;
     控制条件语句;
} while(判断条件语句);

//实例
do {
	System.out.println("HelloWorld");
	x++;
}while(x<=5);

执行流程图:

代码:

package Java_study;

public class dowhile1 {
	public static void main(String[] args) {
		int x = 1;
		do {
			System.out.println("HelloWorld");
			x++;
		} while (x <= 5);
	}

}

测试记录:

HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld

1.4 三种循环的区别

虽然三种循环结构【for、while、do…while】可以完成同样的功能,但是还是有区别:

  1. do…while循环会执行至少一次循环体。
  2. for循环和while循环只有在条件成立时才会去执行循环体

for循环语句和while循环语句的区别:

  1. for循环:控制条件语句变量,在for循环结束后,不可再访问。(for循环结束,该变量就从内存中消失,能够提高内存使用效率)
  2. while循环:控制条件语句变量,在while循环结束后,可以继续访问。

1.5 死循环

两种最简单的死循环

  1. while
int a = 1;
while (true) {
    System.out.println(a++);
}
  1. for
int a = 1;
for (; ; ) {
    System.out.println(a--);
}

循环中需要注意通过控制条件语句来控制循环变量,否则容易陷入死循环

1.6 foreach 循环 / 增强for循环

foreach是一种遍历形式, 可以遍历集合或者数组。
foreach遍历集合实际上是迭代器遍历的简化写法。

增强型for循环定义如下:

	for(ElementType element: arrayName){};
	
上述for循环可被读为:for each element in arrayName do {...}	

相对于for(;;)而言  增强for循环有两个好处:
	1.写起来简单
	2.遍历集合、容器简单

	//实例
        for (int i : arr) {
            System.out.println(i);
        }

二.嵌套循环

在一个循环体语句中又包含另一个循环语句,称为循环嵌套。
实际生产环境会有非常多的这种循环嵌套。

代码:

package Java_study;

public class qiantaoxunhuan1 {
	public static void main(String[] args) {
		
		//嵌套循环1-输出4行5列*
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 5; j++) {
				System.out.print("* ");
			}
			System.out.println();
		}
		
		//嵌套循环2-输出九九乘法表
		for (int i = 0; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(i + "*" + j + "=" + i * j + "\t");
			}
		    System.out.println();
		}
	}

}

测试记录:

* * * * * 
* * * * * 
* * * * * 
* * * * * 

1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81

三. 控制循环语句

3.1 跳转控制语句break

break用于当满足某个条件的时候,跳出循环。
break即可以跳出单层循环,也可以跳出多层循环。

代码:

package Java_study;

public class break1 {
	public static void main(String[] args) {
		//给循环打标签
		wc:
		for (int i = 1; i <= 3; i++) {
			nc:
			for (int j = 1; j <= 3; j++) {
				if (j == 3) {
					// 跳出外层循环wc
					break wc;
				}
				System.out.print("*");
			}
	    System.out.println();
		}
	
	    System.out.println();
	    System.out.println("==========");
	    
	    wc:
		for (int i = 1; i <= 3; i++) {
			nc:
			for (int j = 1; j <= 3; j++) {
				if (j == 3) {
					// 跳出内层循环nc
					break nc;
				}
				System.out.print("*");
			}
	    System.out.println();
		}
	
	
	}

}

测试记录:

**
==========
**
**
**

3.2 跳转控制语句continue

与break是退出当前循环不同,continue 退出本次循环。

代码:

package Java_study;

public class continue1 {
	public static void main(String[] args) {
		for (int i = 1; i <= 5; i++) {
			if (i == 3) {
				continue;
			}
			System.out.println("HelloWorld" + i);
		}
	}

}

测试记录:

HelloWorld1
HelloWorld2
HelloWorld4
HelloWorld5

参考:

  1. https://blog.csdn.net/qq_43529621/article/details/113250176

相关文章