当前位置:网站首页>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
边栏推荐
- 七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
- Image segmentation model - a combination of segmentation_models_pytorch and albumations to achieve multi-category segmentation
- 第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
- Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
- sqlserver编写通用脚本实现获取一年前日期的方法
- Nature:猪死亡1小时后,器官再次运转
- 产品太多了,如何实现一次登录多产品互通?
- 教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
- 第五章:activiti流程分流判断,判断走不同的任务节点
- 第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」
猜你喜欢

How to choose coins and determine the corresponding strategy research

The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?

SQL外连接之交集、并集、差集查询

【OpenCV】-仿射变换

数据可视化(一)

What are the standards for electrical engineering

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

PostgreSQL 2022 Report: Rising popularity, open source, reliability and scaling key

多线程(进阶) - 2.5w字总结

High-quality DeFi application building guide to help developers enjoy DeFi Summer
随机推荐
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
How OpenHarmony Query Device Type
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
Oracle 19.3 restart 环境
How to choose coins and determine the corresponding strategy research
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
Header file search rules when compiling with GCC
《分布式云最佳实践》分论坛,8 月 11 日深圳见
第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
E-sports, convenience, efficiency, security, key words for OriginOS functions
第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」
导火索:OAuth 2.0四种授权登录方式必读
nyoj754 黑心医生 结构体优先队列
一张图看懂 SQL 的各种 join 用法!
gradle尚硅谷笔记
发现C语言的乐趣
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
结合“xPlus”探讨软件架构的创新与变革
Latex如何控制表格的宽度和高度
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解