当前位置:网站首页>循环结构--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);
}
}
边栏推荐
- 鸿星尔克再捐一个亿
- 软件测试的基本理论知识(软件测试面试基础知识)
- R语言ggpubr包的ggbarplot函数可视化分组柱状图、设置add参数为mean_se可视化不同水平均值的柱状图并为柱状图添加误差线(se标准误差)、position参数自定义分组柱状图分离
- 你好,我的新名字叫“铜锁/Tongsuo”
- 软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当
- 超赞!发现一个APP逆向神器!
- 零代码工具推荐---HiFlow
- The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
- R language time series data arithmetic operation: use the log function to log the time series data, and use the diff function to calculate the successive difference of the logarithmic time series data
猜你喜欢

Long battery life or safer?Seal and dark blue SL03 comparison shopping guide

为什么要使用BGP?

Spearman's correlation coefficient

php组件漏洞

瑞萨RZ/G2L处理器详细测评

DVWA 通关记录 2 - 命令注入 Command Injection

软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当

After 21 years of graduation, I switched to software testing. From 0 income to a monthly salary of over 10,000, I am really lucky...

斯皮尔曼相关系数

牛客网项目17节生成验证码 刷新验证码一直没反应
随机推荐
超赞!发现一个APP逆向神器!
后管实现面包屑功能
DVWA 通关记录 2 - 命令注入 Command Injection
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
Getting Started with SCM from Scratch (1): Summary of Background Knowledge
用正向迭代器封装实现反向迭代器
R language ggplot2 visualization: based on the fill parameter and shape parameter in the aes function, custom draw a grouped line chart and add data points (scatter points), use the legend.position fu
程序员的浪漫七夕
零代码工具推荐---HiFlow
logo 图标(php图片加文字水印)
QT专题:组合会话框和文本编辑器
Verilog的随机数系统任务----$random
云原生应用平台的核心模块有哪些
The 38-year-old daughter is not in love and has no stable job, the old mother is crying
使用较广泛的安全测试工具有哪些?
利用二维数据学习纹理三维网格生成(CVPR 2020)
iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?