首页 > Java > java教程 > 正文

循环时:

聖光之護
发布: 2025-02-04 11:14:12
转载
375人浏览过

> 时:

> java时循环是一种控制流语句,用于重复执行语句块,直到给定条件评估为false为止。一旦条件变为false,执行程序中的循环后立即行。 java中的while循环的语法:

while (test_expression) {   

 // statements        

update_expression; 


}


condition is evaluated before each iteration of the loop
if condition is true, the code block inside the while loop will execute
the process repeats until condition is false
登录后复制
java中的while循环执行:>

控件进入ware循环。

测试条件。image description

如果是真的,请执行循环的主体。
  1. 如果false,请退出循环。
  2. 执行身体后,更新循环变量。
  3. >
  4. 从步骤2重复直到条件为错误。
  5. example 1: display numbers from 1 to 5
    
    // program to display numbers from 1 to 5
    
    class main {
      public static void main(string[] args) {
    
        // declare variables
        int i = 1, n = 5;
    
        // while loop from 1 to 5
        while(i <= n) {
          system.out.println(i);
          i++;
        }
      }
    }
    
    output:
    1
    2
    3
    4
    5
    
    登录后复制
  6. 任务1:
program:
public class count {

    public static void main(string[] args) {
    int count=1;
        while (count<=5){
        count=count+1;
        system.out.println("count: "+count);
        }
        }
        }

output:
count: 2
count: 3
count: 4
count: 5
count: 6

登录后复制

>任务2:

image description

program:

public class count1 {

    public static void main(string[] args) {
        int count=5;
        while (count>=1) {
        count=count-1;
        {
        system.out.println("count:" +count);
        }
        }
    }
}

output:
count:4
count:3
count:2
count:1
count:0

登录后复制

任务3:

image description

program:
public class count2 {
    public static void main (string args[])
    {
    int count=0;
    while(count<10) {
    count=count+2;
    {
    system.out.println("count:" +count);
    }
    }
    }
}
output:
count:2
count:4
count:6
count:8
count:10

登录后复制

>任务4:

image description

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

}
output:

count:3
count:5
count:7
count:9
count:11

登录后复制

参考:

image description

以上就是循环时:的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号