当前位置:网站首页>循环结构--while循环
循环结构--while循环
2022-08-02 10:15:00 【白茶清欢*】
一、while循环
1.while循环结构的四个要素
①初始化部分(init_statement)
② 循环条件部分(test_exp)
③循环体部分(boby_statement)
④迭代部分(alter_statement)
①初始化部分
while( ②循环条件部分 ){
③循环体部分 ;
④迭代部分 ;
}
执行过程:① - ② - ③ - ④ - ② - ③ - ④ - ② - ③ - ④ -…- ②
2.while循环说明:
1)写 while 循环千万要小心不要丢了迭代条件。一旦丢了,就可能导致死循环。
2)写程序时要避免死循环。
3)能用 while 循环的,可以用 for 循环,反之亦然。二者可以相互转换。
区别:for 循环和 while 循环的初始化条件部分的作用范围不同,for循环是出了for循环外就不能调用,而while循环出了循环仍能调用.
二、while循环练习
1.遍历100以内的的所有偶数
class WhileTest{
public static void main(String[] args){
int i=0;
while(i<100){
if(i%2==0){
System.out.println(i);
}
i++;
}
//
System.out.println(i);//此处i的值是101,出了while循环也能调用
//*****do-while至少执行一次循环体
int num1=10;
while(num1>10){
System.out.println("hello:while");
num1--;
}
int number2=10;
do{
System.out.println("hello:dowhile");
number--;
}while(number>2);
}
}
边栏推荐
- You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
- Spearman's correlation coefficient
- 3 d laser slam: LeGO - LOAM - ground point extracting method and the analysis of the code
- 第十五章 多线程
- 开箱即用-使用异步加载布局来优化页面启动速度的几种方案
- 身为程序猿——谷歌浏览器的这些骚操作你真的废吗!【熬夜整理&建议收藏】[通俗易懂]
- DVWA Clearance Log 2 - Command Injection
- 你好,我的新名字叫“铜锁/Tongsuo”
- 向量点积(Dot Product),向量叉积(Cross Product)
- List-based queuing and calling system
猜你喜欢
食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了
从测试入门到测试架构师,这10年,他是这样让自己成才的
软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当
3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
读博一年后对机器学习工程的思考
超赞!发现一个APP逆向神器!
Linux系统卸载,安装,升级,迁移clickHouse数据库
8年软件测试工程师的感悟:与薪资相匹配的永远是实力
MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
随机推荐
众城优选系统开发功能
LayaBox---TypeScript---JSX
List-based queuing and calling system
R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
LayaBox---TypeScript---Module
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
Use compilation to realize special effects of love
开源一夏 | GO语言框架中如何快速集成日志模块
LayaBox---TypeScript---Mixins
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
如何封装微信小程序的 wx.request() 请求
周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
QT专题:组合会话框和文本编辑器
软件测试X模型
全方位剖析Numpy中的np.diag源代码
Long battery life or safer?Seal and dark blue SL03 comparison shopping guide
wireshark的安装教程(暖气片安装方法图解)
DVWA 通关记录 2 - 命令注入 Command Injection
基于列表的排队与叫号系统
第十六章 协程