当前位置:网站首页>Android 开发用 Kotlin 编程语言三 循环控制
Android 开发用 Kotlin 编程语言三 循环控制
2022-08-05 10:52:00 【AaVictory.】
For 循环
for 循环可以对任何提供迭代器(iterator)的对象进行遍历,语法如下:
从0循环到100
- 1、
until关键字
until关键字在使用时左侧和右侧都需要一个数值,until循环是一个左闭右开区间
for (index in 0 until 100) {
println(index)
}
- Android 测试
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test()
}
fun test() {
for (index in 0 until 100) {
println(index)
}
}
}
- 输出结果
0 1 2 … 99
in 正序遍历
for (index in 1..100){
print(index)
}
- Android 测试
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test()
}
fun test() {
for (index in 1..100){
print(index)
}
}
}
- 输出结果
1 2 3 … 100
downTo()倒序遍历
包含头和尾,并且按照倒序依次输出
for (index in 100 downTo 1){
print(index)
}
- Android 测试
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test()
}
fun test() {
for (index in 100 downTo 1){
print(index)
}
}
}
- 输出结果
100 99 98 … 1
奇数 隔一个数字
for (index in 1..100 step 2){
println(index)
}
- Android 测试
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test()
}
fun test() {
for (index in 1..100 step 2){
println(index)
}
}
}
- 输出结果
1 3 5 … 99
while 与 do…while 循环
while( 布尔表达式 ) {
//循环内容
}
do…while 循环 对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。
do {
//代码语句
}while(布尔表达式);
实例说明
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test()
}
fun test() {
println("----while 使用-----")
var x = 5
while (x > 0) {
println( x--)
}
println("----do...while 使用-----")
var y = 5
do {
println(y--)
} while(y>0)
}
}
- 输出结果
----while 使用-----
5
4
3
2
1
----do…while 使用-----
5
4
3
2
1
返回和跳转
Kotlin 有三种结构化跳转表达式:
- return。默认从最直接包围它的函数或者匿名函数返回。
- break。终止最直接包围它的循环。
- continue。继续下一次最直接包围它的循环。
在循环中 Kotlin 支持传统的 break 和 continue 操作符
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test()
}
fun test() {
for (i in 1..10) {
if (i==3) continue // i 为 3 时跳过当前循环,继续下一次循环
println(i)
if (i>5) break // i 为 6 时 跳出循环
}
}
}
- 输出结果
1 2 4 5 6
边栏推荐
- 一个栈的输入序列为1 2 3 4 5 的出站顺序的理解
- linux下oracle常见操作以及日常积累知识点(函数、定时任务)
- PCB layout must know: teach you to correctly lay out the circuit board of the op amp
- Google启动通用图像嵌入挑战赛
- The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
- 第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]
- poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)
- 什么是 DevOps?看这一篇就够了!
- PPOCR 检测器配置文件参数详解
- 【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
猜你喜欢

In-depth understanding of timeout settings for Istio traffic management

张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来

软件测试之集成测试

深入理解 Istio 流量管理的超时时间设置

Create a Dapp, why choose Polkadot?

电气工程的标准是什么

上位机开发C#语言:模拟STC串口助手接收单片机发送数据

The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
![[Strong Net Cup 2022] WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[Strong Net Cup 2022] WP-UM

abc262-D(dp)
随机推荐
PCB布局必知必会:教你正确地布设运算放大器的电路板
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
Is digital transformation a business buy-in?
华为分析&联运活动,助您提升游戏总体付费
012_SSS_ Improving Diffusion Model Efficiency Through Patching
Score interview (1)----related to business
Latex如何控制表格的宽度和高度
MySQL 中 auto_increment 自动插入主键值
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
反射修改jsessionid实现Session共享
登录功能和退出功能(瑞吉外卖)
The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
nyoj754 黑心医生 结构体优先队列
[Android]如何使用RecycleView in Kotlin project
RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
Where is your most secretive personality?
STM32入门开发:编写XPT2046电阻触摸屏驱动(模拟SPI)
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
导火索:OAuth 2.0四种授权登录方式必读
SQL Outer Join Intersection, Union, Difference Query