当前位置:网站首页>循环结构--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);
}
}
边栏推荐
- LayaBox---TypeScript---JSX
- 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
- Hongxing, donate another million
- MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
- 软件测试与质量 之白盒测试
- 阿里CTO程立:阿里巴巴开源的历程、理念和实践
- Linux系统卸载,安装,升级,迁移clickHouse数据库
- The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars
- iNFTnews | 看见元宇宙的两面,何谓全真互联网和价值互联网?
- 斯皮尔曼相关系数
猜你喜欢

阿里巴巴 CTO 程立:开源是基础软件的源头!

云原生应用平台的核心模块有哪些

重磅大咖来袭!阿里云生命科学与智能计算峰会精彩内容剧透

The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit

This article takes you to understand the commonly used models and frameworks of recommender systems

Verilog's random number system task----$random

Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)

yolov7 innovation point

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

php组件漏洞
随机推荐
You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
如何安装dosbox(pycharm详细安装教程)
软件工程国考总结——选择题
阿里CTO程立:阿里巴巴开源的历程、理念和实践
迭代器失效问题
armv7与armv8的区别(v8和w12的区别)
软件测试与质量 之白盒测试
Jay Chou's new song is released, crawl the "Mojito" MV barrage, and see what the fans have to say!
currentstyle 织梦_dede currentstyle属性完美解决方案
第十六章 协程
One Summer of Open Source | How to Quickly Integrate Log Modules in GO Language Framework
MySql千万级分页优化,快速插入千万数据方法
Hongxing, donate another million
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
Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
向量点积(Dot Product),向量叉积(Cross Product)
你好,我的新名字叫“铜锁/Tongsuo”
LayaBox---TypeScript---Symbols
DVWA 通关记录 2 - 命令注入 Command Injection
Why use BGP?