Java - 循环控制语句

循环控制(for、while、dowhile、多重循环)

基本语法

for (循环变量初始化; 循环条件; 循环变量迭代){
    循环体(可以多条语句);
}

在这里插入图片描述

注意事项

1、循环条件返回的是一个布尔值的表达式

2、for(;循环判断条件;)中的初始化和变量迭代可以写到其他地方,但是两边的分号不能省略

public class for01{
	public static void main(String[] args) {
		int i = 1;
	    for (; i <=10;) {
	    	System.out.println("您好" + i);
	    	i++;
	    }
        System.out.println(i);
	}
}		

3、循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开。循环变量迭代也可以有多条变量迭代语句,中间用逗号隔开。

public class for01{
	public static void main(String[] args) {
	    for (int i=0, j = 0; i <=10; i++, j+=2) {
	    	System.out.println("i=" + i+ "; j=" + j);
	    }
	}
}		

编程思想:1、化繁为简2、先死后活

While循环

基本结构

while (循环条件){
    循环体(语句);
    循环变量迭代;
}
// 循环结构四要素:循环变量初始化、循环条件、循环体、循环变量迭代

public class while01{
	public static void main(String[] args) {
	    int count = 1;
	    while (count <= 10){
	    	System.out.println("niaho" + count);
	    	count++;
	    }
	}
}		

在这里插入图片描述

注意事项和细节说明

1、循环条件是返回一个布尔值的表达式

2、while循环同for循环,是先判断再执行语句。

do while循环控制

基本语法

循环变量初始化;
do{
    循环体(语句);
    循环变量迭代;
}while(循环条件);

说明

1、循环四要素,只是位置不同

2、先执行,再判断,也就是说,最少执行一次

3、最后的while有个分号 ;

4、while 和do while 的区别:要账
在这里插入图片描述

注意事项和细节说明

1、循环条件是返回一个布尔值的表达式

2、do while循环是先执行,再判断,因此至少执行一次。

public class dowhile01{
	public static void main(String[] args){
		int i =1;
		int count = 0;
		do{
			if ((i % 5 == 0) && (i %3!=0)){
				count++;
			}
			i++;
		}while(i <=200);
		System.out.println("个数为:" + count);
	}
}

多重循环

1、循环最多不要超过三层,会造成可读性比较差。

测试题

for (int i=1;i<=9;i++){
  for (int z = 1; z<=i; z++){
    System.out.print(i + "*" + z + "=" + i*z + "\t");
  };
  System.out.println();

}
public class stars{
	public static void main(String[] args){
		int totalLevel = 100;

		for (int level=1; level <= totalLevel; level++){
			// 打印每行的空格
			for (int space=1; space <= totalLevel-level; space++){
				System.out.print(" ");
			}

			// 打印每行的 * 
			for (int star=1; star <= 2 * level -1; star++){
				if (star==1 || star==2 * level -1 || level==totalLevel){
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}

break

基本介绍

break语句用于终止某个语句块的执行,一般使用在switch或者循环中。

注意事项和细节说明

1、break语句出现在多层嵌套的语句块中时,可以通过标签指明要终止的是哪一层语句块。

2、标签的基本使用

public class break01{
	public static void main(String[] args){
		label01:
		for (int j =0; j < 4; j++){
		label02:
			for (int i=0; i<10; i++) {
				if (i==2){
					break label01;
				}
				System.out.println("i=" + i);
			}
		}
	}
}

continue

基本介绍

1、continue语句用于结束本次循环,继续执行下一次循环

2、continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环,这个和break中的标签使用规则一样。

public class Continue01{
	public static void main(String[] args){
		label01:
		for (int j =0; j < 4; j++){
		label02:
			for (int i=0; i<10; i++) {
				if (i==2){
					continue label01;
				}
				System.out.println("i=" + i);
			}
		}
	}
}

return

简介

return使用在方法中,表示跳出所在的方法。

注意:如果在main方法中使用return。 则会退出程序,这是和break的区别。

public class Return01{
	public static void main(String[] args){
		getMethod();
		System.out.println("sss");
	}

	public static void getMethod(){
        for (int i =1; i<3;i++){
			if (i==2){
				return;
			}
			System.out.println("i="+i);
		}
    }
}

测试题

1、输出1-100之间不能被5整除的数,每5个一行。

public class Work{
	public static void main(String[] args){
		int count =0;
		for(int num=1; num<=100; num++){
			if (num % 5 != 0){
				System.out.print(num + "\t");
				count++;

				if (count % 5 == 0){
					System.out.println();
				}
			}
		}
	}
}

2、输出小写字母a-z

for (char a = 'a'; a<='z'; a++){ //关键对char的使用
    System.out.print(a+"\t");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值