当前位置:网站首页>循环结构--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);
}
}
边栏推荐
- Smoothing of time series data in R language: smoothing time series data to remove noise using the dpill function and locpoly function of the KernSmooth package
- 斯皮尔曼相关系数
- 3 d laser slam: LeGO - LOAM - ground point extracting method and the analysis of the code
- LayaBox---TypeScript---三斜线指令
- LayaBox---TypeScript---Mixins
- logo 图标(php图片加文字水印)
- Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
- Rust 从入门到精通03-helloworld
- 阿里巴巴 CTO 程立:开源是基础软件的源头!
- LayaBox---TypeScript---Iterator and generator
猜你喜欢
随机推荐
Hongxing, donate another million
ConvNeXt论文及实现
Shell脚本实现多选DNS同时批量解析域名IP地址(新更新)
【云原生】快出数量级的性能是怎样炼成的?就提升了亿点点
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
云原生应用平台的核心模块有哪些
DVWA 通关记录 2 - 命令注入 Command Injection
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
LayaBox---TypeScript---Three slash instructions
Rust 从入门到精通03-helloworld
软件测试H模型
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)
LayaBox---TypeScript---迭代器和生成器
零代码工具推荐---HiFlow
Why use BGP?
R语言时间序列数据的平滑:使用KernSmooth包的dpill函数和locpoly函数对时间序列数据进行平滑以消除噪声
众城优选系统开发功能
迭代器失效问题
如何安装dosbox(pycharm详细安装教程)









