当前位置:网站首页>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
边栏推荐
- The query that the user's test score is greater than the average score of a single subject
- Custom filters and interceptors implement ThreadLocal thread closure
- 微服务结合领域驱动设计落地
- flutter 服务器返回数据判断是否为空
- Chapter 4: activiti RuntimeService settings get and get process variables, and the difference from taskService, set process variables when starting and completing tasks [easy to understand]
- 时间格式2020-01-13T16:00:00.000Z中的T和Z分别表示什么,如何处理
- ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
- Detailed explanation of PPOCR detector configuration file parameters
- How to choose coins and determine the corresponding strategy research
- 化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
猜你喜欢

机器学习——集成学习

MySQL 中 auto_increment 自动插入主键值

Google启动通用图像嵌入挑战赛

RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)

Android 开发用 Kotlin 编程语言 二 条件控制

DocuWare平台——文档管理的内容服务和工作流自动化的平台详细介绍(下)

Naive bayes

RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)

ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解

gradle尚硅谷笔记
随机推荐
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
工程设备在线监测管理系统自动预警功能
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
FPGA:开发环境Vivado的使用
The query that the user's test score is greater than the average score of a single subject
微信小程序标题栏封装
Android 开发用 Kotlin 编程语言三 循环控制
Http-Sumggling缓存漏洞分析
反射修改jsessionid实现Session共享
Leetcode刷题——623. 在二叉树中增加一行
nyoj754 黑心医生 结构体优先队列
Chapter 5: Multithreaded Communication—wait and notify
HDD杭州站•ArkUI让开发更灵活
Oracle的自动段空间管理怎么关闭?
In-depth understanding of timeout settings for Istio traffic management
STM32 entry development: write XPT2046 resistive touch screen driver (analog SPI)
trie树模板
60行从零开始自己动手写FutureTask是什么体验?
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
Android 开发用 Kotlin 编程语言 二 条件控制