博客园 首页 新随笔 联系 订阅 管理
  3 随笔 :: 0 文章 :: 0 评论 :: 3242 阅读

int
x=10; do { System.out.println("value of x:"+x); x++; } while(x<20); //do while循环
1 int x=10;
2     while(x<20) {
3     System.out.println("value of x:"+x);
4     x++;
5 }                    //while循环

while(布尔表达式) 只要布尔表达式内容为真 循环体就会一直循环下去

do  while 即使不满足条件 也至少执行一次

1            for(int x=10;x<20;x=x+1)
2         {
3             System.out.println("value of x:" +x);
4         }                    //for循环   for语句 和while 语句后面没有;        

循环次数在循环执行前就已经确定了

注意for循环内部用 

 

复制代码
 1         int [] numbers= {10,20,30,40,50,60};
 2         for (int x:numbers) {
 3             System.out.print(x);
 4             System.out.print(',');
 5         }
 6         System.out.print("\n");
 7         String []names = {"Jame","Tom","Jerry"};
 8         for(String name : names) {
 9             System.out.print(name);
10             System.out.print(',');
11         }                                //数组增强for循环
复制代码

Java增强for循环

for(声明语句:表达式)

声明语句:声明新的局部变量,变量类型必须与数组元素匹配 

表达式:要访问的数组名,或者是返回值为数组的方法

1         int [] numbers= {10,20,30,40,50};
2         for(int x:numbers) {
3             if(x==30) {
4                 break;
5             }
6             System.out.println(x);
7         }                                //break关键字

break通常应用于switch语句中,用于跳出循环 

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

1         int [] numbers= {10,20,30,40,50};
2         for(int x:numbers) {
3             if(x==30) {
4                 continue;
5             }
6             System.out.println(x);
7         }                                //continue关键字

continue适用于许多的循环结构 

for循环中,continue语句使程序立即跳转到更新语句

posted on   Slientsake  阅读(114)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示