当前位置:网站首页>循环结构--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);
}
}
边栏推荐
- 享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
- List-based queuing and calling system
- QT专题:自定义部件
- yolov7 innovation point
- 3 d laser slam: LeGO - LOAM - ground point extracting method and the analysis of the code
- This article takes you to understand the commonly used models and frameworks of recommender systems
- Hello, my new name is "Bronze Lock/Tongsuo"
- 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
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- QT专题:组合会话框和文本编辑器
猜你喜欢

How to choose a truly "easy-to-use, high-performance" remote control software

List-based queuing and calling system

软件工程国考总结——选择题

转转反爬攻防战

一款优秀的中文识别库——ocr

第十七章 Excel操作

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

如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?

后管实现面包屑功能

games202:三,实时环境光照IBL + PRT
随机推荐
迭代器失效问题
Rear tube implements breadcrumb function
matlab-day02
小几届的学弟问我,软件测试岗是选11k的华为还是20k的小公司,我直呼受不了,太凡尔赛了~
牛客刷题——剑指offer(第三期)
众城优选系统开发功能
图形化矩阵,矩阵到底长什么样?
LayaBox---TypeScript---JSX
LayaBox---TypeScript---Three slash instructions
logo 图标(php图片加文字水印)
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
QT专题:事件机制event基础篇
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
【OpenCV】-霍夫变换
如何选择一块真正“好用的、性能高”的远程控制软件
LayaBox---TypeScript---高级类型
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化堆叠的柱状图(stacked bar plot)、lab.pos参数指定柱状图的数值标签的位置,lab.col参数指定数值标
从测试入门到测试架构师,这10年,他是这样让自己成才的
鸿星尔克再捐一个亿