循环结构(do...while语句)

/*
	do...while循环的基本格式:
		扩展格式;
		初始化语句;
		do {
			循环体语句;
			控制条件语句;
		}while(判断条件语句);
		
	
	循环语句的区别:
		do...while循环至少执行一次循环体。
		而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
		
	那么,我们一般使用哪种循环呢?
		优先考虑for,其次考虑while,最后考虑do...while
		
		
	注意死循环:
		A:一定要注意控制条件语句控制的那个变量的问题,不要弄丢了,否则就容易死循环。
		B:两种最简单的死循环格式
			while(true){...}
			for(;;){...}
*/
class DoWhileDemo {
	public static void main(String[] args) {
		//输出10次HelloWorld。
		int x = 0;
		do {
			System.out.println("HelloWorld");
			x++;
		}while(x<10);
		
		System.out.println("--------------");
		
		//求和1-100
		int sum = 0;
		int a = 1;
		do {
			sum += a;
			a++;
		}while(a<=100);
		
		System.out.println(sum);
	}
}


class DoWhileDemo2 {
	public static void main(String[] args) {
		int x = 3;
		while(x < 3) {
			System.out.println("我爱林青霞");
			x++;
		}
		
		System.out.println("--------------");
		
		int y = 3;
		do {
			System.out.println("我爱林青霞");
			y++;
		}while(y < 3);
	}
}


class DoWhileDemo3 {
	public static void main(String[] args) {
		int x = 0;
		while(x < 10) {
			System.out.println(x);
			x++;
		}
		System.out.println("--------------");
		
		/*
		while(true) {
			System.out.println("今天我很高兴,学习了死循环");
		}
		*/
		
		for(;;){
			System.out.println("今天我很高兴,学习了死循环");
		}
		
		//System.out.println("--------------");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值