1.循环
1. while
语法:
while(逻辑表达式) { //逻辑代码 }
-
首先进行逻辑表达式判断 ,如果成立,就执行 逻辑代码 ,不成立,就不执行
-
如果成立,执行完逻辑代码后,还会继续进行逻辑表达式判断,...
-
为了防止死循环,逻辑表达式中的变量一定要发生变化
package com.qfedu; public class Demo01 { public static void main(String[] args) { /* * 打印10行hello world * * * while(逻辑表达式) { * //逻辑代码 * } * 1. 首先进行逻辑表达式判断 ,如果成立,就执行 逻辑代码 ,不成立,就不执行 * 2. 如果成立,执行完逻辑代码后,还会继续进行逻辑表达式判断,...... * 3. 为了防止死循环,逻辑表达式中的变量一定要发生变化 * */ int i = 1; while(i <= 10) { //1 2 3 4 5 6 7 8 9 10 System.out.println("hello world"); i+=1; } //计算 1+。。。+10的和 int result = 0; // 0+1+2+3+...+10 int j = 1; while(j <= 10) { //1 2 3 4 5 6 7 8 9 10 result += j; j++; } //计算1-100之间的偶数和 2+4+6+...+100 int k = 2; while(k <= 100) { System.out.println(k); k+=2; } System.out.println("代码执行结束----"); } }
2. do-while
语法:
do { //逻辑代码 } while(逻辑表达式);
-
首先执行一次 逻辑代码 然后进行逻辑表达式的判断
-
如果逻辑表达式成立,则继续执行逻辑代码,逻辑代码执行结束,进行进行逻辑表达式判断
-
如果逻辑表达式不成立,则退出本次循环, 执行后续代码
package com.qfedu; public class Demo02 { public static void main(String[] args) { /* * do { * //逻辑代码 * } while(逻辑表达式); * * 1.首先执行一次 逻辑代码 然后进行逻辑表达式的判断 * 2. 如果逻辑表达式成立,则继续执行逻辑代码,逻辑代码执行结束,进行进行逻辑表达式判断 * 3. 如果逻辑表达式不成立,则退出本次循环, 执行后续代码 */ int i = 100; do { System.out.println("hello world"); i+=1; } while(i <= 10); System.out.println("--------"); } }
3. for
语法:
for(变量定义; 循环条件判断; 变量变化操作){ //业务代码 }
-
执行变量定义
-
进行循环条件判断
-
如果成立,则执行业务代码,执行结束后,进行 变量变化 操作, 如果成立继续执行业务代码
-
如果不成立,则跳出本次循环,执行后续代码
package com.qfedu; public class Demo03 { public static void main(String[] args) { /* * 语法: * for(变量定义; 循环条件判断; 变量变化操作){ * //业务代码 * } * * 1. 执行变量定义 * 2. 进行循环条件判断 * 3. 如果成立,则执行业务代码,执行结束后,进行 变量变化 操作, 如果成立继续执行业务代码 * 4. 如果不成立,则跳出本次循环,执行后续代码 * */ for(int i=1;i <= 10;i+=1) { System.out.println("hello world"); } //计算1+2+...+10 int num=0; for(int i=0;i<=10;i++) { num+=i; } System.out.println(num); //计算1-100的偶数和 int num1=0; for(int j=0;j<=100;j++) { if(j%2==0) { num1+=j; } } System.out.println(num1); } }
2. 流程控制
1.1 break
break : 结束循环,执行后续代码
package com.qfedu; public class Demo01 { public static void main(String[] args) { /* * break * 结束当前循环 * continue */ int i=1; while(i<=100) { System.out.println("hello world-"+i); if(i==10) { break; } ++i; } System.out.println("-----"); } }
1.2 continue
continue: 结束本次循环,开始下一次循环
package com.qfedu; public class Demo02 { public static void main(String[] args) { /* * continue 结束本次循环,开始下一次循环 */ for(int i=1; i<=10; ++i) { if(i%2 == 0) { continue; } System.out.println("hello world-"+i); } int sum = 0; for(int i=1; i<=100; i=i+1) { if(i%2 != 0) { continue; } sum+=i; } } }
3.循环的嵌套
1. 打印长方形
***** ***** *****
package com.qfedu; public class Demo03 { public static void main(String[] args) { for(int i=1; i<=3; i++) { //执行3次 for(int j=1; j<=5; j++) { //执行5次 System.out.print("*"); } System.out.println(); } System.out.println("--------------------------------------------"); for(int i=1; i<=2; i++) { //执行2次 for(int j=1; j<=4; j++) { //执行4次 System.out.print("*"); } System.out.println(); } } }
2. 打印直角三角形
* ** *** **** ***** 或者 **** *** ** *
package com.qfedu; public class Demo04 { public static void main(String[] args) { for(int i=1; i<=5; i++) { //i=1,2,3 for(int j=1; j<=i; j++) { //执行4次 System.out.print("*"); } System.out.println(); } System.out.println("--------------------------"); for(int i=1; i<=4; i++) { //i=1,2,3,4 for(int j=1; j<=5-i; j++) { //执行4次 System.out.print("*"); } System.out.println(); } } }
3. 打印正三角形
* * * * * *
package com.qfedu; public class Demo05 { public static void main(String[] args) { for(int i=1; i<=3; i++) { // i = 1,2,3 //打印# for(int j=1; j<=3-i; j++) { System.out.print(" "); } //打印* for(int j=1; j<=i; j++) { System.out.print("* "); } //换行 System.out.println(); } } }
4. 打印九九乘法表
1×1=1 1×2=2 2×2=4 1×3=3 2×3=6 3×3=9 1×4=4 2×4=8 3×4=12 4×4=16 1×5=5 2×5=10 3×5=15 4×5=20 5×5=25 1×6=6 2×6=12 3×6=18 4×6=24 5×6=30 6×6=36 1×7=7 2×7=14 3×7=21 4×7=28 5×7=35 6×7=42 7×7=49 1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64 1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9×9=81
package com.qfedu; public class Demo07 { /* * i=1 j=1 * i=2 j=1 2 * i=3 j=1 2 3 * 4 5 6 7 8 * * 9 1 2 3 4 。。。9 * * 1×1=1 */ public static void main(String[] args) { for(int i=1; i<=9; i++) { for(int j=1; j<=i; j++) { System.out.print(j+"×"+i+"="+(j*i)+"\t"); } System.out.println(); } } }
5. 打印菱形
* * * * * * * * * * * * * * * *
package com.qfedu; //打印菱形 public class Demo01 { public static void main(String[] args) { for(int i=1;i<=4;i++) { for(int j=1;j<=4-i;j++) { System.out.print(" "); } for(int j=1;j<=i;j++) { System.out.print("* "); } System.out.println(); } for(int i=1;i<=3;i++) { for(int j=1;j<=i;j++) { System.out.print(" "); } for(int j=1;j<=4-i;j++) { System.out.print("* "); } System.out.println(); } }
4.关于逻辑运算符的补充
package com.qfedu; public class Demo06 { public static void main(String[] args) { /* * && || * * * &&,|| : 断路 前面如果出现了决定这个表达式的结果的判断,后面就不再执行 * &,| :不会断路 */ int a = 5; boolean r1 = a>10 && a++>3; System.out.println(a); boolean r2 = a>10 & a++>3; System.out.println(a); int b = 6; boolean r3 = b>3 | b++<30; System.out.println("b="+b); } }