4.4循环结构
循环语句可以在满足循环条件的情况下,反复执行某一段代码,被重复执行的代码段称为循环体,在反复执行循环体的时候,需要在合适的情况下将条件改为假,否则循环将一直执行下去,形成可怕的 死循环 。
常用的循环结构:while循环,do…while循环,for循环。
4.3.1 while循环语句
while循环的基本格式:
while(判断条件语句) {
循环体语句;
}
当while条件为真(true)时,执行循环体中的代码
举例1:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
int count = 1;
while(count<10){
System.out.println("当前值为:"+count);
count++;
}
}
}
4.3.2 do while 循环语句
do…while循环的基本格式:
do {
循环体语句;
}while(判断条件语句);
do代码段中为循环体,先执行循环体中的代码,然后再去判断条件是否为真(true),如果为真再执行do代码段中的循环体。
举例2:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
int count = 1;
do {
System.out.println("当前的数值为:"+count);
count++;
} while(count<10);
}
}
循环语句的区别:
do…while循环至少执行一次循环体。
而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句
4.3.3 for循环
for循环格式:
for(初始化语句;判断条件语句;控制条件语句) {
循环体语句;
}
执行流程:
A:执行初始化语句
B:执行判断条件语句,看其返回值是true还是false
如果是true,就继续执行
如果是false,就结束循环
C:执行循环体语句;
D:执行控制条件语句
E:回到B继续。
For循环体是更加简洁的循环语句,在大部分情况下for循环可以代替while循环、do while循环。
举例1:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
System.out.println("--------------开始for循环------------");
for(int i=0;i<10;i++){
System.out.println("当前的数值为:"+i);
}
System.out.println("---------------for循环结束---------------");
}
}
注意事项:
A:判断条件语句无论简单还是复杂结果是boolean类型。
B:循环体语句如果是一条语句,大括号可以省略;如果是多条语句,大括号不能省略。建议永远不要省略。
C:一般来说:有左大括号就没有分号,有分号就没有左大括号
4.3.3.1for循环与while循环的区别
while循环与for循环的区别:如果想在循环的过程中使用控制循环的变量,那么用while循环,否则使用for循环,如果不清楚使用不使用,那么也用for循环。因为在for循环中定义的变量出了for循环就在内存中消失,提高了内存的使用效率。
4.3.3.2嵌套循环
for循环的嵌套,比如要想打一个5X5的正方形,要怎么打印?
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
三角形: triangle
*
* *
* * *
* * * *
* * * * *
九九乘法表:
1X1=1
1X2=2 2X2=4
.
.
.
1X9=9…………………………………9X9=81
4.3.4 foreach循环
数组的输出一般都会使用for循环,但在JDK1.5后为了方便数组的输出,提供了一种foreach方法,foreach循环更加简洁,循环的必须是个集合或者数组。For表达式中定义的变量执行了数组或集合中的每一个具体的值。
for(数据类型 变量名称:数组名称){
}
举例:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
System.out.println("--------------开始for循环------------");
int num[] ={11,20,33,40,55};
for(int i : num){
System.out.println("当前数值为:"+i);
}
System.out.println("---------------for循环结束---------------");
}
}
4.5控制循环结构
Java语言提供了continue语句和break语句来控制循环结构,除此之外还可以使用return语句来结束整个方法体。
4.4.1 break结束循环
在代码体中,一旦执行到break语句,那么就结束整个循环,跳出循环体,不再执行,即使还没有执行完成也将不再执行。
举例1:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
for(int i=0;i<10;i++){
System.out.println("当前执行第"+(i+1)+"次");
if(i==5){
break;//当执行第6次的时候跳出循环体
}
}
System.out.println("循环结束");
}
}
4.4.2 continue结束本次循环
与break有点类似,区别是终止后面代码的执行,跳出本次循环,继续执行下一次循环。
举例1:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
for(int i=0;i<5;i++){
if(i==2){
continue;
}
System.out.println("当前执行第"+(i+1)+"次");
}
System.out.println("循环结束");
}
}
4.4.3 return结束方法
return关键字,并不是专门用于结束循环体的,return的功能时结束一个方法。return关键字后面还可以跟变量、常量、表达式用于对方法的返回值。
举例1:
package com.langsin.test;
public class TestCar {
public static void main(String[] args) {
for(int i=0;i<5;i++){
if(i==2){
return;
}
System.out.println("当前执行第"+(i+1)+"次");
}
System.out.println("循环结束")
}
}