Java流程控制
循环结构
(1)while循环
- while是最基本的循环,只要布尔表达式为true,循环就会一直执行。
- 除了少部分情况需要循环一直执行(比如服务器的请求响应监听等),大多数情况还是需要让循环停止下来的,这就需要一个让表达式失效的方式来结束循环。
- 循环条件一直为true就会造成无限循环(死循环),正常的业务编程中应该尽量避免死循环,因为会影响程序性能或者造成程序卡死崩溃。
- 语法:
while(布尔表达式){
//循环内容
}
package com.feihong.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//
int i=0;
while(i<100){
i++;
System.out.println(i);//输出到100停止
}
}
}
package com.feihong.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//while一直为true变成死循环
while(true){
//比如等待客户端连接,闹钟的定时检查等情况需要用这种方式
}
}
}
package com.feihong.struct;
public class WhileDemo03 {
//计算1+……+100
public static void main(String[] args) {
int num=0;
int sum=0;
while(num<=100){
sum=sum+num;
num++;
}
System.out.println(sum);//输出为:5050
}
}
(2)do……while循环
- 对于while语句而言,不满足条件则不能进入循环。但有时我们需要即使不满足条件,也至少执行一次。
- do...while循环和while循环主要不同的是,do...while循环会至少执行一次。
do {
//代码语句
}while(布尔表达式);
- While先判断后执行,Do...While是先执行后判断。
package com.feihong.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
//依旧是计算1+……+100
int i=0;
int sum=0;
do {
sum=sum+i;
i++;
}while(i<=100);
System.out.println(sum);//输出为:5050
}
}
package com.feihong.struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a=1;
while (a<1){
System.out.println(a);//没有输出,因为没有进入循环
a++;
}
System.out.println("==========================================");
do {
System.out.println(a);
a++;
}while (a<1);//输出为:1,表明进了一次循环
}
}
(3)For循环
- For循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
- for循环执行的次数是在执行前就确定的。
- 语法:
for (初始化;布尔表达式;更新){
//代码语句
}
- 快捷输入for循环:


输入100.for,确定回车即可形成一个for循环。
package com.feihong.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a=1;//初始化条件
while (a<=100){//条件判断
System.out.println(a);//循环内容即循环体
a+=1;//迭代,刷新条件以达到满足条件判断以停止循环
}
System.out.println("while循环结束");//输出为:1~100
//for(初始条件;条件判断;迭代)
for (int i=1;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");//输出为:1~100
/*
关于for循环有几点说明:
最先执行的初始化步骤,只可以声明一种类型,但可以初始化一个或多个循环控制变量,此句可以为空语句。
即for (int i = 0; i < 100; i++){}其中的int i = 0部分可以为空,for (; i < 100; i++){}
然后,检测布尔表达式的值如果为true,循环体会被执行。如果为false,循环终止,开始执行循环体后面的语句。
for (int i = 0; i < 100; i++){}其中的i < 100部分也可以为空,for (; ; i++){}
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。再次检测布尔表达式,循环执行以上步骤。
for (; ; ){}即无限循环,死循环
*/
}
}
package com.feihong.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1:分别计算出0~100之间的奇数的和与偶数的和
int oddSum =0;
int evenSum=0;
for (int i = 0; i < 100; i++) {
if(i%2!=0){
oddSum+=i;
}else {
evenSum+=i;
}
}
System.out.println(oddSum);
System.out.println(evenSum);
}
}
练习1:分别计算出0~100之间的奇数的和与偶数的和
package com.feihong.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1000以内能被5整除的数,样式为每行3个
for (int i = 0; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.println();//直接就换行了
//或者System.out.println("\n");换行
//System.out.println();输出完会换行。System.out.print();输出完不会换行
}
}
}
}
练习2:用while或for循环输出1000以内能被5整除的数,样式为每行3个
package com.feihong.struct;
public class ForDemo04 {
public static void main(String[] args) {
//练习3:打印九九乘法表
/*
样式为:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/
//1.先把复杂的问题拆成小问题,第一列的表示方式:
/*for (int i = 1; i <= 9; i++) {
System.out.println(i+"*"+1+"="+i);
}
*/
/*
输出为:
1*1=1
2*1=2
3*1=3
4*1=4
5*1=5
6*1=6
7*1=7
8*1=8
9*1=9
*/
//2.打出后面的其他几列,可以用到for循环的嵌套,即将第一列的for循环嵌套到新的for循环中:
/*for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= 9; i++) {
System.out.println(i+"*"+1+"="+i);
}
}
*/
//这时只是第一列循环了九次,要让i随着j的变化而变化:
/*for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {//int i=1 --->int i=j
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
*/
/*
输出为:
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
*/
//System.out.print(j+"*"+i+"="+(i*j)+"\t");的效果与联系要求吻合
/*
输出为:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/
//int i=j;i<=9;时的效果
for (int j = 1; j <= 9; j++) {
for (int i = j; i <= 9; i++) {//int i=1 --->int i=j
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
/*
输出为:
1*1=1 2*1=2 3*1=3 4*1=4 5*1=5 6*1=6 7*1=7 8*1=8 9*1=9
2*2=4 3*2=6 4*2=8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18
3*3=9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27
4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36
5*5=25 6*5=30 7*5=35 8*5=40 9*5=45
6*6=36 7*6=42 8*6=48 9*6=54
7*7=49 8*7=56 9*7=63
8*8=64 9*8=72
9*9=81
*/
}
}
练习3:打印九九乘法表,习题不是目的,重要的是要掌握复杂问题分析拆解的能力,对以后实现项目功能打好基础
(4)增强for循环
- 这里只作了解,之后数组再重点使用
- Java5引入的主要用于数组或集合的增强型for循环
- 语法:
for(声明语句:表达式)
{
//代码句子
}
package com.feihong.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("====================================");
//遍历数组的元素,及每次循环从数组numbers中取出输出一个数
for (int x:numbers){
System.out.println(x);
}
}
}
break & continue
- break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环剩余的语句。(break也在switch语句中使用)
- continue语句在循环语句中,用于终止某次循环过程,即将跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
package com.feihong.struct;
public class BreakDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
System.out.print(i);
if (i==30){
break;//只是终止了程序中的循环,不影响程序接着往下运行
}
}
System.out.println("\n"+"结束了");
}
/*
输出为:
123456789101112131415161718192021222324252627282930
结束了
*/
}
package com.feihong.struct;
public class ContinueDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;//continue之后的语句不执行了,但循环继续
//System.out.print(1);//在continue之后,报错不可达到的语句
}
System.out.print(i+"\t");
}
/*
输出为:
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
*/
}
}
关于goto关键字(了解即可)
- 尽管goto仍是Java的关键字,但语言中无法使用,但有使用类似功能的方法----带标签的break和continue。
- 标签是指后面跟一个冒号的标识符,例如:“ label: ”
- 对Java来说唯一用到标签的地方是在循环语句之前
package com.feihong.struct;
public class GotoDemo {
public static void main(String[] args) {
//101~150之间所有的质数
//指数是大于1的其他所有自然数中,没有除了1和它本身以外因数的自然数
int count =0;
outer: for (int i = 100; i < 150; i++) {//在之前定义一个名为outer的标签
for (int j=2;j<i/2;j++){
if (i%j==0){//需要他成立之后返回外部,就需要打标签
continue outer;//表示停止运行之后的程序,并返回到outer继续循环
}
}
System.out.print(i+" ");
}
//不建议使用!
}
}
浙公网安备 33010602011771号