接着前一篇学习循环语句
for : 次数循环
for(条件初始化;条件判断;条件变化){
循环体语句;
}
while : 条件循环
条件初始化;
while(条件判断){
循环体语句;
条件变化;
}
执行流程 :
1.条件初始化
2.条件判断,结果为boolean类型,如果为false,结束循环结构,如果为true,继续执行循环体语句
循环结构不停的想要下一次循环体,重复执行,所以需要条件判断来决定循环是否能够下一次执行,是否结束
注意 :
循环之间可以相互转换
for : 次数循环
for(条件初始化;条件判断;条件变化){
循环体语句;
}
while : 条件循环
条件初始化;
while(条件判断){
循环体语句;
条件变化;
}
//for
for(int i=1;i<=10;i++){
System.out.println(i);
}
//while
int i=0;
while(i<=9){
i++;
System.out.println(i);
}
//计算1~100整数和
int j = 1;
int sum = 0;
while(j<=100){
sum+=j;
j++;
}
System.out.println(sum);
do..while :
条件初始化;
do{
循环体语句;
条件变化;
}while(条件判断);
执行流程 :
1.条件初始化;
2.执行do后{}中的循环体语句
3.执行while后的条件判断,boolean结果,如果为false,循环结束,如果为true,满足条件,执行下一次循环体语句
判断的是下一次循环体语句能否执行
while与do..while,for区别 :
do..while无论是否满足条件判断,都至少能够执行一次
for次数循环,能够确定循环次数,能够确定变量条件变化的区间范围选择for循环
while次数循环,如果不确定循环次数,只关注条件判断,选择while
//for
for(int i=1;i<=10;i++){
System.out.println(i);
}
//while
int i = 100;
while(i<=10){
System.out.println(i);
i++;
}
//do..while
int j = 100;
do{
System.out.println(j);
j++;
}while(j<=10);
break与continue区别 :
break :
作用 : 终止 结束
应用场景 : switch,循环
continue :
作用 : 结束本次循环,直接进入到下一次循环
应用场景 : 循环
//输出1~10之间的整数,每逢3或3的倍数跳过
for(int i=1;i<=10;i++){
if(i%3==0){
continue;
}
System.out.println(i);
}
for(int i=1;i<=10;i++){
if(i%2==0){
//continue; //输出5次yjxxt
break; //输出一次 yjxxt
}
System.out.println("yjxxt");
}
死循环 : 循环无法停止
编译器识别死循环 : 一般内部需要配合break关键字一起使用
for(;;){
}
while(true){
}
do{}while(true);
代码结构实现的死循环:
一般逻辑的问题,比如,条件判断逻辑不对,或者忘记定义条件变化..
//编译器识别死循环 :
/*for(;;){
System.out.println();
}*/
/*while(true){
}*/
//do{}while(true);
//代码结构实现的死循环:
for(int i=1;i<=10;){
System.out.println(i);
}
System.out.println("循环之后"); //在死循环的后面的语句为不可达语句
循环嵌套 :
for(条件初始化;条件判断;条件变化){
循环体语句;
for(条件初始化;条件判断;条件变化){
循环体语句;
}
循环体语句;
}
注意 : 外层循环执行一次,内层循环执行一轮
/*
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
*/
int count = 0;
for(int i=1;i<=25;i++){
System.out.print("* ");
count++;
if(count==5){
System.out.println();
count=0;
}
}
System.out.println();
System.out.println("------------------------------");
//外层循环i控制行 内层循环变量j控制列
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
System.out.print("* ");
}
System.out.println();
}
System.out.println();
System.out.println("------------------------------");
/*
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
*/
for(int i=1;i<=6;i++){
for(int j=1;j<=7;j++){
System.out.print("* ");
}
System.out.println();
}
System.out.println();
System.out.println("------------------------------");
/*
* * * * *
* *
* *
* *
* * * * *
*/
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
if(i==1 || i==5 || j==1 || j==5){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
/*
* 1
* * 2
* * * 3
* * * * 4
* * * * * 5
i
*/
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();
}
/*
* 1
* * 2
* * 3
* * 4
* * * * * 5
*/
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
if(i==5||j==1|j==i){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
//九九乘法表
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"x"+i+"="+i*j+"\t");
}
System.out.println();