Java流程控制-循环结构
循环结构
-
while循环
-
do...while循环
-
for循环
-
在Java5中引入了一种主要用于数组的增强型for循环
while循环
while是最基本的循环,它的结构为:
while(布尔表达式){
//循环内容
}
- 只要布尔表达式为true,循环就会一直执行下去。
- 我们大多数情况是会让循环停止下来的,我们需要一个表达式失效的方式来结束循环
- 少部分情况需要循环一直执行,比如服务器的请求响应监听等
- 循环条件一直为true就会造成无限循环(死循环),我们正常的业务编程中应该尽量避免死循环。死循环会影响程序性能或者造成程序卡死崩溃
package com.jiemyx.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.jiemyx.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true){
//等待客户端连接
//定时检查
//。。。。。。
}
}
}
package com.jiemyx.struct;
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+3+。。。+100=?
int i = 0;
int sum = 0;
while (i<=100){
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
输出结果:5050
do...while循环
- 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
- do...while循环和while循环相似,不同的是,do...while循环至少会执行一次。
do{
//代码语句(循环体)
}while(布尔表达式);
- while和do...while的区别
- while先判断后执行。do...while先执行后判断。
- do...while总是保证循环体至少会被执行一次!这是它们的主要区别。
package com.jiemyx.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
输出结果:5050
package com.jiemyx.struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int i = 0;
while (i<0){
System.out.println(i);
i++;
}
System.out.println("===========");
do {
System.out.println(i);
i++;
}while (i<0);
}
}
输出结果:
===========
0
for循环
- 循环结构可以用while或者do...while表示,但是Java提供了另一种语句——for循环,使一些循环结构变得更加简单。
- for循环语句支持迭代的一种通用结构,最有效,最灵活的循环结构。
- for循环执行的次数是在执行前就确定的。
语法:
for(初始化; 布尔表达式; 更新){
//代码语句
}
package com.jiemyx.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 0; //初始条件
while (a<=100){ //判断条件
System.out.println(a); //循环体
a+=1; //迭代
}
System.out.println("while循环结束!");
//IDEA的快捷方式:输入100.for 回车
//初始条件//判断条件//迭代
for (int i=0;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束!");
/*
for循环有以下几点说明:
最先执行初始化步骤。可以声明一种类型,但可以初始化一个或多个循环控制变量,也可以空语句。
然后,检测布尔表达式的值。如果为true,循环体被执行。如果false,循环终止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程
*/
//死循环
/*
for ( ; ; ){
}
*/
}
}
package com.jiemyx.struct;
public class ForDemo02 {
public static void main(String[] args) {
//0~100的偶数和、奇数和
int oddSum = 0;
int eveSum = 0;
for (int i = 0; i <= 100; i++){
if (i%2!=0){ //奇数
oddSum+=i; //oddSum = oddSum + i;
}else { //偶数
eveSum+=i;
}
}
System.out.println("奇数的和:"+oddSum);
System.out.println("偶数的和:"+eveSum);
}
}
输出结果:
奇数的和:2500
偶数的和:2550
package com.jiemyx.struct;
public class ForDemo03 {
public static void main(String[] args) {
//while或者for循环输出1~1000之间能被5整除的数,并且每行输出3个数
for (int i = 0; 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 a = 0;
while (a<1000){
if (a%5==0){
System.out.print(a+"\t");
}
if (a%15==0){
System.out.println();
//System.out.print("\n");
}
a++;
}
}
}
输出结果:
0
5 10 15
20 25 30
35 40 45
50 55 60
......
package com.jiemyx.struct;
public class ForDemo04 {
public static void main(String[] args) {
//九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i) + "\t");
}
System.out.println();
}
System.out.println("=========");
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"*"+j+"="+(j*i) + "\t");
}
System.out.println();
}
}
}
增强型for循环
- Java5引入了一种主要用于数组或集合的增强型for循环
语法:
for(声明语句:表达式){
//代码语句
}
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
package com.jiemyx.struct;
public class ForDemo05 {
public static void main(String[] args) {
//定义一个数组
int[] numbers = {10,20,30,40,50};
for (int i=0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("=============");
//遍历数组的元素
for (int x:numbers){
System.out.println(x);
}
}
}
break与continue
- break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
package com.jiemyx.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
System.out.println("123");
}
}
- continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
package com.jiemyx.struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.print(i);
}
}
}
- 关于goto关键字
- goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用:Java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子——带标签的break和continue。
- “标签”是指后面跟一个冒号的标识符,例如:label:
- 对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
package com.jiemyx.struct;
public class LabelDemo {
public static void main(String[] args) {
//打印101~150之间所有的质数
//质数是指在大于1的自然数中,除了1和它本身以外不在有其他因数的自然数
//不建议使用
outer:for (int i=101; i<150; i++){
//一个合数的因素,除了它本身以外,可能有最大因素是它的1/2
//j代表因素
for (int j=2; j<i/2; j++){
if (i % j == 0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}