循环结构
-
while循环
- while是最基本的循环,它的结构为:
while(布尔表达式){ //循环内容 }
- 只要布尔表达式为true,循环就会一直执行下去
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环
- 少部分情况需要循环一直执行,比如服务器的请求响应监听等
- 循环条件一直为true就会造成无限循环[死循环],我们正常的业务编程中应该尽量避免死循环,它会影响程序性能或者造成程序卡死崩溃!
- 思考:计算1+2+3+…+100=?
代码实现:
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/20 19:22
*/
public class WhileDemo02 {
public static void main(String[] args) {
//计算1+2+3+...+100=?
int i = 0;
int max =100;
int sum =0;
while (i<max){
i++;
sum = sum +i;
}
System.out.println(sum);
}
}
-
do…while循环
- 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
- do…while循环和while循环相似,不同的是,
do...while循环至少会执行一次。
do...while是先执行后判断,而while是先判断后执行。
- 语法:
do{ //代码语句 }while(表达式);
代码实现:
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/20 19:56
*/
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int max =100;
int sum =0;
do {
i++;
sum = sum +i;
}while (i<max);
//sum同样等于5050
System.out.println(sum);
}
}
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/20 20:03
*/
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a<0){
a++;
//不执行
System.out.println(a);
}
System.out.println("==================================");
do {
a++;
//输出为1
System.out.println(a);
}while (a<0);
}
}
-
for循环
- 虽然所有循环结构都可以用while或者do while表示,但Java提供了另一种语句———for循环,使一些循环结构变得更加简单
- for循环语句是支持迭代的一种通用结构,是最有效的、最灵活的循环结构
- for循环执行的次数是在执行前就确定的。
- IDEA快捷键:100.fori将会生成for (int i = 0; i < 100; i++) {}
- 关于for循环的说明:
最先执行初始化步骤,可以声明一种类型,但初始化一个或多个循环控制变量,也可以是空语句。然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。再次检测布尔表达式,循环执行上面的过程。
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/20 20:27
*/
public class ForDemo01 {
public static void main(String[] args) {
//初始化条件
int a = 1;
//循环体
while (a<100){
//迭代
a+=1;
System.out.println(a);
}
System.out.println("while循环结束!");
for (int i=1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束!");
//死循环的另一种写法
/*
for (; ; ) {
}
*/
}
}
语法:
```
for(初始化;布尔表达式;更新){
//代码语句
}
```
代码实现:
- 计算0~100之间的奇数和偶数的和
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/21 9:18
*/
public class ForDemo02 {
public static void main(String[] args) {
// 计算0~100之间的奇数和与偶数和
int oddSum = 0;
int evenSum = 0;
final int max = 100;
for (int i = 0; i <= max; i++) {
if (i%2==0){
evenSum+=i;
} else{
oddSum+=i;
}
}
System.out.println("0~100之间奇数的和为:"+oddSum);
System.out.println("0~100之间偶数的和为:"+evenSum);
}
}
- 用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/21 10:19
*/
public class ForDemo03 {
public static void main(String[] args) {
//用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
//下面以for循环和while循环为例
for (int i = 1; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%15==0){
System.out.println();
//System.out.print("\n");
}
}
System.out.println("===========================");
int i = 1;
while(i<=1000){
i++;
if (i%5==0){
System.out.print(i+"\t");
}
if (i%15==0){
System.out.println();
}
}
}
}
- 打印九九乘法表
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/21 10:35
*/
public class ForDemo04 {
public static void main(String[] args) {
// 思路:
// 1.先打印出第一列
// 2.我们把固定的1用一个循环包起来
// 3.去掉重复项(i<j)
// 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
*/
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();
}
}
}
-
增强型for循环
- 这里我们先只是见一面,做个了解,之后数组我们重点讲和使用
- Java 5引入了一种主要用于数组或集合的增强型for循环
- Java增强for循环的语法格式如下:
for(声明语句:表达式){ //代码句子 }
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
代码实现:
package com.ruomeng.structure;
/**
* @author 1301450090@qq.com
* @date 2021/3/21 11:23
*/
public class ForDemo05 {
public static void main(String[] args) {
int [] numbers = {10,20,30,40,50,60};
for (int i = 0;i<5;i++){
System.out.println(numbers[i]);
}
//增强for循环简化,提高效率
//遍历数组的元素 IDEA快捷键:number.for
for (int x : numbers) {
System.out.println(x);
}
}
}
如果我的博客对你有一点点帮助,望请大侠可以给文章点个赞再走,你们的鼓励将是我创作优质内容的动力!