当前位置:网站首页>循环结构--do-while循环
循环结构--do-while循环
2022-08-02 10:15:00 【白茶清欢*】
一、do-while循环
1.、循环结构的四个要素
① 初始化条件
② 循环条件 —> 是 boolean 类型
③ 循环体
④ 迭代条件
2.、do-while 循环的结构
①
do{
③ ;
④ ;
}while( ② );
3、执行过程:① - ③ - ④ - ② - ① - ③ - ④ - … - ②
4、说明:do-while 循环至少执行一次循环体。
1)不在循环条件部分限制次数的结构:
while(true) , for(true)
2)结束循环的几种方式:
方式一:循环条件部分返回 false;
方式二:在循环体中,执行 break;
二、do-while循环练习
1.遍历100以内的偶数,并计算所有偶数的和及偶数的个数
class DoWhileTest{
public static void main(String[] args){
int num=1;
int sum=0;//记录总和
int count=0;//记录个数
do{
if(num%2==0){
System.out.println(num);
sum+=num;
count++;
}
num++;
}while(num<=100);
System.out.println("总和为:"+sum);
System.out.println("总和为:"+count);
}
}
边栏推荐
猜你喜欢

MySql tens of millions of paging optimization, fast insertion method of tens of millions of data

MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救

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

享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世

太帅了!我用炫酷大屏展示爬虫数据!

为什么要使用BGP?

链表的实现

matlab-day02

新“内卷”席卷科技圈,Google CEO 要求 174000 员工提高工作效率!

MySql千万级分页优化,快速插入千万数据方法
随机推荐
第十六章 协程
yolov7创新点
DVWA Clearance Log 2 - Command Injection
Turning and anti-climbing attack and defense
currentstyle 织梦_dede currentstyle属性完美解决方案
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
牛客网项目17节生成验证码 刷新验证码一直没反应
LayaBox---TypeScript---声明合并
用正向迭代器封装实现反向迭代器
【面向校招】Golang面试题合集
Alibaba CTO Cheng Li: Alibaba Open Source History, Concept and Practice
小几届的学弟问我,软件测试岗是选11k的华为还是20k的小公司,我直呼受不了,太凡尔赛了~
ConvNeXt论文及实现
从零开始Blazor Server(5)--权限验证
LayaBox---TypeScript---Symbols
LayaBox---TypeScript---迭代器和生成器
LayaBox---TypeScript---Decorator
练习16-两道模拟题
[Science of Terminology] For those difficult words about the integrated workbench, read this article to understand in seconds!
Spearman's correlation coefficient