目录
一、循环结构
1.While
* while 条件循环
语法 :
for(条件初始化;条件判断;条件变化){
循环体;
}
条件初始化;
while(条件判断){
循环体;
条件变化;
}
public class Class026_while {
public static void main(String[] args) {
//for
for(int i=1;i<=10;i++){
System.out.println(i);
}
//while
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
//1~100之间整数和
i=1;
int sum = 0;
while(i<=100){
sum+=i;
i++;
}
System.out.println(sum);
System.out.println(i);
}
}
注意:
- 循环之间可以相互转化
- 在使用while循环时候,注意条件的声明位置,条件变化的位置
- while循环适合使用在,条件不用声明就已经存在|条件变化不需要手动控制,这种情况适合使用while循环
- for循环适合用在: 确定循环次数,确定条件变化区间,适合使用for循环
2.do...while
/*
while
条件初始化;
while(条件判断){
循环体语句;
条件变化;
}
do..while
条件初始化;
do{
循环体语句;
条件变化;
}while(条件判断);
*/
public class Class027_DoWhile {
public static void main(String[] args) {
/*
while(false){
System.out.println("while...false");
}
*/
do{
System.out.println("do...while...false");
}while(false);
//求100之间整数和
int i=1;
int sum = 0;
//while实现
while(i<=100){
sum+=i;
i++;
}
System.out.println(sum);
System.out.println(i);
//转为do..while实现
int j = 1;
sum = 0; //sum归零
do{
sum+=j;
j++;
}while(j<=100);
System.out.println(sum);
System.out.println(j);
}
}
while与do...while的区别 :
while : 先判断,满足条件要求再执行每一次循环
do..while : 先执行一次,然后判断条件决定下一次知否执行
无论是否满足条件,都至少执行一次
3.死循环
循环无法停止
1.编译器识别的死循环:
while(true){ }
for( ; ; ){ }
do{ }while(true);
死循环后面的代码为不可达语句
2.运行时期的死循环 :
后面出现了语句,编译也不会报错,因为编译器认为这个循环有可能停止
ctrl+c ==> 控制台中强制终止程序的执行
public class Class028_WhileTrue {
public static void main(String[] args) {
//编译器识别的死循环:
/*
do{
}while(true);
*/
//运行时期的死循环 :
/*
boolean flag = true;
while(flag){
System.out.println("while-true");
}
*/
int i=1;
while(i<=10){
System.out.println(i);
}
System.out.println("while..true之后");
}
}
4.break与continue
break :
作用 : 终止,结束
应用场景 : switch,循环
continue:
作用 : 结束本次循环,直接进入到下一次循环
应用场景 : 循环中
public class Class029_BreakContinue {
public static void main(String[] args) {
//输出1~10之间的整数,如果遇到3或者3的倍数跳过
for(int i=1;i<=10;i++){
if(i%3==0){
continue;
}
System.out.println(i);
}
//找到100,300之间前五个能被5整数的数据
int count = 0;
for(int i=100;i<=300;i++){
if(i%5==0){
System.out.println(i);
count++;
//循环结束的条件
if(count==5){
break;
}
}
}
System.out.println(".....");
}
}
5.循环嵌套
for(条件初始化;条件判断;条件变化){
//循环体语句
for(条件初始化;条件判断;条件变化){
循环体语句
}
//循环体语句
}
外层循环执行一次,内存循环执行一轮(从开始到不满足循环条件结束)
public class Class030_NestedLoop {
public static void main(String[] args) {
/*
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
*/
//单层循环
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();
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();
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(); //换行
}
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();
System.out.println();
/*
* * * * * * *
* *
* *
* *
* *
* * * * * * *
*/
for(int i=1;i<=6;i++){
for(int j=1;j<=7;j++){
if(i==1 || i==6 || j==1 || j==7){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println(); //换行
}
System.out.println();
System.out.println();
System.out.println();
/*
* 1
* * 2
* * * 3
* * * * 4
* * * * * 5
*/
for(int i=1;i<=7;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println(); //换行
}
System.out.println();
System.out.println();
System.out.println();
/*
* 1
* * 2
* * 3
* * 4
* * * * * 5
*/
for(int i=1;i<=7;i++){
for(int j=1;j<=i;j++){
if(i==7 || j==1 || j==i){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println(); //换行
}
System.out.println();
System.out.println();
System.out.println();
//九九乘法表
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println(); //换行
}
}
}
6.多重循环嵌套下break与continue的使用
break与continue默认针对最内层循环
如果想要break与continue针对多重循环嵌套下的某一层循环,可以使用带标签的break与continue
步骤:
1.每一层设置标签
2.使用break与continue的时候==> break|continue 标签名;
public class Class031_BreakContinue {
public static void main(String[] args) {
one:
for(int i=1;i<=5;i++){
two:
for(int j=1;j<=5;j++){
if(j==3){
break one;
}
System.out.print("i="+i+" ==> j="+j+"\t");
}
System.out.println();
}
}
}