03-循环结构
-
while循环
-
while是最基本的循环,它的结构为:
-
1 while(布尔表达式) { 2 //循环内容 3 }
-
只要布尔表达式为 true,循环就会一直执行下去。
-
我们大多数情况下是会让循环停止下来的,我们1需要一个让表达式失效的方式来结束循环。
1 package xiaoka.struct; 2 3 public class WhileDemo01 { 4 public static void main(String[] args) { 5 6 // 输出1--100 7 8 int i=0; 9 10 while (i<100){ 11 i++; 12 System.out.println(i); 13 } 14 } 15 }
-
少部分情况需要循环一直执行,比如服务器的请求响应监听等
1 package xiaoka.struct; 2 3 public class WhileDemo02 { 4 public static void main(String[] args) { 5 //死循环 6 while (true){ 7 //等待程序响应 8 //定时检查 9 //..... 10 } 11 } 12 }
-
循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死崩溃
-
思考:计算1+2+3+...+100=?
1 package xiaoka.struct; 2 3 public class WhileDemo03 { 4 public static void main(String[] args) { 5 //计算1+2+3+...+100=? 6 7 int i = 0; 8 int sum = 0; 9 10 while (i<=100){ 11 sum = sum+i; 12 i++; 13 } 14 15 System.out.println(sum); 16 } 17 }
-
-
do...while循环
-
对于while 循环语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次
-
do...while 循环和while 循环相似,不同的是,do...while 循环至少会执行一次。
1 do { 2 //代码语句 3 }while(布尔表达式);
1 package xiaoka.struct; 2 3 public class DoWhileDemo01 { 4 public static void main(String[] args) { 5 int i = 0; 6 int sum = 0; 7 8 do { 9 sum = sum+i; 10 i++; 11 }while (i<=100); 12 13 System.out.println(sum); 14 } 15 }
-
while 和do while的区别
-
while 先判断后执行,do while 是先执行后判断
1 package xiaoka.struct; 2 3 public class DoWhileDemo02 { 4 public static void main(String[] args) { 5 int a = 0; 6 while (a<0){ 7 System.out.println(a); 8 a++; 9 } 10 System.out.println("=========================="); 11 12 do { 13 System.out.println(a); 14 a++; 15 }while (a<0); 16 } 17 }
-
do while总是保证循环体会被至少执行一次,这是他们的主要差别
-
-
-
for 循环
-
虽然所有循环结构都可以用 while 循环或者 do...while 表示,但Java 提供了另一种语句———for 循环,使一些循环结构变得简单
-
for 循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。
-
for 循环执行的次数是在执行前就确定的,语法格式如下:
for (初始化;布尔表达式;更新) { //代码语句 }
1 package xiaoka.struct; 2 3 public class ForDemo01 { 4 public static void main(String[] args) { 5 int a = 1;//初始化条件 6 7 while (a<=100){//条件判断 8 System.out.println(a);//循环体 9 a+=2;//迭代 10 } 11 12 System.out.println("while循环结束"); 13 14 //初始化 //条件判断 //迭代 15 for (int i=1;i<=100;i++){ 16 System.out.println(i); 17 } 18 19 System.out.println("for循环结束"); 20 21 /* 22 关于for 循环有以下几点说明 23 24 最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句. 25 其次,检测布尔表达式的值,如果为true,循环体被执行;如果为false,循环终止.开始执行循环体后面的语句 26 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减) 27 再次检测布尔表达式,循环执行上面的过程 28 */ 29 30 //死循环 31 for (; ; ) { 32 33 } 34 } 35 }
-
练习1:计算0到100之间的奇数和偶数的和
1 package xiaoka.struct; 2 3 public class ForDemo02 { 4 public static void main(String[] args) { 5 //计算0到100之间的奇数和偶数的和 6 7 int oddSum = 0; 8 int evenSum = 0; 9 10 for (int i = 0; i < 100; i++) { 11 if (i%2!=0){// 奇数 12 oddSum+=i; 13 }else {// 偶数 14 evenSum+=i; 15 } 16 } 17 18 System.out.println("奇数的和为:"+oddSum); 19 System.out.println("偶数的和为:"+evenSum); 20 } 21 }
-
练习2:用while 或for 循环输出1-1000 之间能被5整除的数,并且每行输出3个
1 package xiaoka.struct; 2 3 public class ForDemo03 { 4 public static void main(String[] args) { 5 //练习2:用while 或for 循环输出1-1000 之间能被5整除的数,并且每行输出3个 6 7 for (int i = 0; i <= 1000; i++) { 8 if (i%5==0){ 9 System.out.print(i+"\t"); 10 } 11 if (i%(5*3)==0){ 12 System.out.println(); 13 //System.out.println("\n"); 14 } 15 } 16 17 //println 输出完会换行 18 //print 输出完不会换行 19 } 20 }
-
练习3:打印99乘法表
1 package xiaoka.struct; 2 //练习3:打印99乘法表 3 public class ForDemo04 { 4 public static void main(String[] args) { 5 //编程使用双重for 循环实现九九乘法表的打印 6 7 //1.使用外层for 循环用于控制打印的行数 8 for (int i = 1; i <= 9; i++) { 9 //2.使用内层for 循环用于控制打印的列数 10 for (int j = 1;j <= i; j++) { 11 System.out.print(j + "*" + i + "=" + i*j + " "); 12 } 13 System.out.println(); 14 } 15 } 16 }
-
-
在java5中引入了一种主要用于数组的增强型for 循环
-
我们这里先只是见一面,做个了解,之后数组重点使用
-
-
Java 增强for 循环语法格式如下
for(声明语句:表达式){
//代码语句
} -
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
-
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
1 package xiaoka.struct; 2 3 public class ForDemo05 { 4 public static void main(String[] args) { 5 int[] numbers = {10,20,30,40,50};//定义了一组数组 6 7 for (int i = 0; i < 5; i++) { 8 System.out.println(numbers[i]); 9 } 10 System.out.println("========================="); 11 12 //遍历数组的元素 13 for (int x:numbers){ 14 System.out.println(x); 15 } 16 } 17 } 18
-