当前位置:网站首页>Android development with Kotlin programming language three loop control
Android development with Kotlin programming language three loop control
2022-08-05 10:58:00 【AaVictory.】
For 循环
for 循环可以对任何提供迭代器(iterator)的对象进行遍历,语法如下:
从0循环到100
- 1、
until关键字
until关键字在使用时左侧和右侧都需要一个数值,untilA cycle is a left-closed, right-open interval
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()倒序遍历
包含头和尾,and output in reverse order
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
奇数 every other number
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
边栏推荐
- 学生信息管理系统(第一次.....)
- 支持向量机SVM
- Android 开发用 Kotlin 编程语言三 循环控制
- R语言使用yardstick包的pr_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的ROC曲线(precision(精准率),R代表的是recall(召回率)
- MySQL 中 auto_increment 自动插入主键值
- In-depth understanding of timeout settings for Istio traffic management
- Scaling-law和模型结构的关系:不是所有的结构放大后都能保持最好性能
- 提问题进不去。想问大家一个关于返回值的问题(图的遍历),求给小白解答啊
- 【C语言指针】用指针提升数组的运算效率
- FPGA:基础入门按键控制LED灯
猜你喜欢
abc262-D(dp)
Create a Dapp, why choose Polkadot?
数据可视化(一)
【深度学习】mmclassification mmcls 实战多标签分类任务教程,分类任务
使用Windbg过程中两个使用细节分享
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
TiDB 6.0 Placement Rules In SQL 使用实践
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
sqlserver编写通用脚本实现获取一年前日期的方法
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
随机推荐
登录功能和退出功能(瑞吉外卖)
trie树模板
Oracle的自动段空间管理怎么关闭?
What do T and Z in the time format 2020-01-13T16:00:00.000Z represent and how to deal with them
提问题进不去。想问大家一个关于返回值的问题(图的遍历),求给小白解答啊
PG优化篇--执行计划相关项
Microcontroller: temperature control DS18B20
脱光衣服待着就能减肥,当真有这好事?
数据可视化(一)
智能算力的枢纽如何构建?中国云都的淮海智算中心打了个样
图像分割模型——segmentation_models_pytorch和albumentations 组合实现多类别分割
【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
例题 可达性统计+bitset的使用
L2-042 老板的作息表
The fuse: OAuth 2.0 four authorized login methods must read
Linux:记一次CentOS7安装MySQL8(博客合集)
Android development with Kotlin programming language - basic data types
智源社区AI周刊No.92:“计算复杂度”理论奠基人Juris Hartmanis逝世;美国AI学生九年涨2倍,大学教师短缺;2022智源大会观点报告发布[附下载]
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
什么是 DevOps?看这一篇就够了!