当前位置:网站首页>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
边栏推荐
- FPGA: Use of the development environment Vivado
- 第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]
- FPGA:基础入门LED灯闪烁
- Guys, I am a novice. I use flinksql to write a simple count of user visits according to the document, but it ends after executing it once.
- 一个栈的输入序列为1 2 3 4 5 的出站顺序的理解
- 解决【命令行/终端】颜色输出问题
- Latex如何控制表格的宽度和高度
- 苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
- Go compilation principle series 6 (type checking)
- linux下oracle常见操作以及日常积累知识点(函数、定时任务)
猜你喜欢

【综合类型第 35 篇】程序员的七夕浪漫时刻

Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database

这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口

双因子与多因子身份验证有什么区别?

【 temperature warning program DE development 】 event driven model instance

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

E-sports, convenience, efficiency, security, key words for OriginOS functions

如何选币与确定对应策略研究

脱光衣服待着就能减肥,当真有这好事?

DocuWare平台——文档管理的内容服务和工作流自动化的平台详细介绍(下)
随机推荐
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
用户考试分数大于单科科目平均分的查询
第五章:多线程通信—wait和notify
Login function and logout function (St. Regis Takeaway)
FPGA: Basic Getting Started LED Lights Blinking
RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
大佬们 我是新手,我根据文档用flinksql 写个简单的用户访问量的count 但是执行一次就结束
[Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
R语言使用yardstick包的pr_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的ROC曲线(precision(精准率),R代表的是recall(召回率)
Getting started with Polkadot parachain development, this article is enough
Voice-based social software development - making the most of its value
Guys, I am a novice. I use flinksql to write a simple count of user visits according to the document, but it ends after executing it once.
STM32 entry development: write XPT2046 resistive touch screen driver (analog SPI)
PPOCR 检测器配置文件参数详解
sqlserver编写通用脚本实现获取一年前日期的方法
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
第四章:redis 数组结构的set和一些通用命令「建议收藏」
uniapp中的view高度设置100%
Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
软件测试之集成测试