当前位置:网站首页>循环结构--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);
}
}
边栏推荐
猜你喜欢

斯皮尔曼相关系数

链表的实现

Rear tube implements breadcrumb function

字节跳动软件测试岗,收到offer后我却拒绝了~给面试的人一些忠告....

Linux系统卸载,安装,升级,迁移clickHouse数据库

用正向迭代器封装实现反向迭代器

You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)

Linux system uninstall, install, upgrade, migrate clickHouse database

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

如何选择一块真正“好用的、性能高”的远程控制软件
随机推荐
How to choose a truly "easy-to-use, high-performance" remote control software
从零开始Blazor Server(5)--权限验证
LayaBox---TypeScript---装饰器
LayaBox---TypeScript---命名空间和模块
win10打印服务无法启动(运行时错误automation)
太帅了!我用炫酷大屏展示爬虫数据!
利用二维数据学习纹理三维网格生成(CVPR 2020)
MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
[Science of Terminology] For those difficult words about the integrated workbench, read this article to understand in seconds!
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
图形化矩阵,矩阵到底长什么样?
yolov7创新点
全方位剖析Numpy中的np.diag源代码
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
如何安装dosbox(pycharm详细安装教程)
LayaBox---TypeScript---Three slash instructions
众城优选系统开发功能
The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi