Java 循环结构

while死循环

基本结构

while( 布尔表达式 ) { 
	//循环内容 
}

注:只要布尔表达式为true,循环将一直执行下去

例:

	public class Test { 
		public static void main(String args[]) { 
			int x = 10; 
			while( x < 20 ) { 
				System.out.print("value of x : " + x ); 
				x++; 
				System.out.print("\n"); 
			} 
		} 
}


在这里插入图片描述

do…while 有条件循环

与while类似,主要区别在于do…while 至少执行一次,后面加条件,如果条件不满足或者布尔值为false,循环将不再执行

基本结构

do {
       //代码语句
}while(布尔表达式);

注:与while的区别在于一个条件在前,一个条件在后

例:

public class Test { 
	public static void main(String args[]){ 
		int x = 10; 
		do{ 
			System.out.print("value of x : " + x ); 
			x++; 
			System.out.print("\n"); 
		}while( x < 20 ); 
	} 
}

在这里插入图片描述

for循环

基本结构

for(声明语句 : 表达式) 
{ //代码句子 
}

注:声明一个局部变量,定义一个或者多个循环控制的变量,当值为false时,循环终止

例:

public static void main(String args[]) {

    for(int x = 10; x < 20; x = x+1) {
        System.out.print("value of x : " + x );
        System.out.print("\n");
    }
}
}

在这里插入图片描述

break

break 跳出最里层的循环,并且继续执行该循环下面的语句

代码示例:

	break

注:基本语法及作用与shell中break的使用一致

continue

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代,与break区别在于,break直接跳出循环,continue会跳过循环,执行下一次循环

代码示例:

continue;

注:基本语法及作用与shell中continue的使用一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值