当前位置:网站首页>Kotlin program control
Kotlin program control
2022-07-26 08:26:00 【Peace of mind】

Catalog
Combined with interval operator
Realization switch The function of
Realization if Select branch effect
And in or is Use a combination of
if sentence
Branch judgment
Kotlin Medium if One of the functions is Realization and Java in if Exactly the same Branch selection structure
There are two commonly used forms if (condition) { } else { }
if (condition) { } else if(condition){ } else{ }
var x:Int val num=50 val input=Scanner(System.`in`) x=input.nextInt() if (x>num){ println(" The input number is larger ") }else if (x<num){ println(" The input number is small ") }else{ println(" Input correct ") }
Ternary expression
Java of use ? : To implement ternary expressions , But this grammar has not been JetBrains Accepted
Kotlin It's using if ... else ... To realize the ternary expression
grammar :if (condition) value 1 else value 2
var a:Int var b:Int val input=Scanner(System.`in`) a=input.nextInt() b=input.nextInt() println("$a"+" and "+"$b"+" The smaller one is : "+ if (a<b) a else b)
Block structure
Kotlin in if It can also represent a multi branch block structure and return the value of the last statement in the corresponding block
var a:Int var b:Int val input=Scanner(System.`in`) a=input.nextInt() b=input.nextInt() val x=if (a>b){ println("$a > $b") }else if (a==b){ println("$a = $b") }else{ println("$a < $b") } println(x) // Output Unit, No return value var x1=if (a>b){ println("$a > $b") a-b }else if (a==b){ println("$a = $b") 0 }else{ println("$a < $b") b-a } println(x1) //Int A value of type
for sentence
Kotlin I've given up Java in for Of ( initial value ; Judge ; iteration ) Structure
Incremental loop
Kotlin of use in and until Realized incremental cycle , You can use step Specify step size ( The default step size is 1)
Kotlin The incremental loop implemented is [ a , b )
var sum=0 for (i in 1 until 9) sum+=i println(sum) // Realization 1~8 Sum of 36 sum=0 for (i in 1 until 10 step 2) sum+=i println(sum) // Realization 1~9 Sum of odd numbers 25
Decreasing cycle
Kotlin of use in and downTo The decreasing cycle is realized ,step The function of is consistent with the increasing cycle
Kotlin The decreasing cycle realized is [ a , b ]
var sum=0 for (i in 9 downTo 1) sum+=i println(sum) // Realization 1~9 Sum of 45 sum=0 for (i in 8 downTo 0 step 2) sum+=i println(sum) // Realization 0~8 Even sum of 20
Combined with interval operator
in and Interval operator .. When used together, it can achieve similar until step by 1 The effect of , however The interval is double closed
var sum=0 for (i in 1..8) sum+=i println(sum) // 1~8 Sum up 36 // use until Express yes 1 until 9
Use with serialized objects
in It can be used with serialized objects such as strings 、 Arrays combine , Directly implement the traversal of elements in the string and array
var str="123456aurora" var arr= arrayOf(9,8,7,6,5) for (i in str) print("$i ") //1 2 3 4 5 6 a u r o r a println() for (i in arr) print("$i ")//9 8 7 6 5
when sentence
Realization switch The function of
Kotlin of use when Instead of Java in switch The function of
grammar when (condition) { case1->{ } case2->{ } case3,case4,case5->{ } else->{ }}
The general use is shown in the uplink :, amount to switch No break Of case, But it's a little different , stay Java in , these case Different blocks can be executed , but Kotlin The same block is executed in ;else amount to switch Medium defalut block
var day:String val input=Scanner(System.`in`) day=input.next() when(day){ "SunDay"->{ println(" Today is Sunday ") } "MonDay"->{ println(" Today is Monday ") } "TuesDay","SaturDay"->{ println(" Today is Tuesday or Saturday ") }else->{ println(" Today is Wednesday 、 Friday or Thursday ") } }
Realization if Select branch effect
when It can also be realized if else Branch selection effect , It can also be nested if{ } else if{ } else{ } The selection effect of
grammar :when(condition){ true->{ } else->{ } }
when(condition){ true->{ } else->{ when(condition){true->{} else->{ } } }
var x:Int val num=50 val input= Scanner(System.`in`) x=input.nextInt() when (x>num){ true->{ println("bigger") } else->{ when(x==num){ true->{ println("equal") } else->{ println("smaller") } } } }
And in or is Use a combination of
And in or is Can also be used in combination with if Realization
in It mainly determines whether the element is in the serialized object
is It mainly determines whether the variable is of a certain type
var arr= arrayOf(1,3,5,7,9) when(3){ in arr->{ println("is exit") } else->{ println("no") } } if (3 in arr){ println("is exit") }else{ println("no") } when(arr){ is Array<Int>->{ // Useless inspection println("Yes") }else->{ println("No") } } if (arr is Array<Int>){ // Useless inspection println("Yes") }else{ println("No") }
while sentence
Kotlin in while Use of statements And Java Consistent with the use of
while (condition) { }
do...while sentence
Kotlin in do ... while Use of statements And Java Consistent with the use of
do { }while(condition)
Jump statements
The jump statement has break ,continue ,return Three , Its use is also related to Java In the agreement
break Indicates that subsequent statements will not be executed , Jump straight out of the loop
continue Indicates that subsequent statements will not be executed , Execute next cycle
return It is mainly used for the return value of functions , See blog for specific use Kotlin Basic functions
边栏推荐
猜你喜欢

Software engineering -- dental clinic -- demand analysis
![[GUI] swing package (window, pop-up window, label, panel, button, list, text box)](/img/05/8e7483768a4ad2036497cac136b77d.png)
[GUI] swing package (window, pop-up window, label, panel, button, list, text box)

Beauty naked chat for a while, naked chat over the crematorium!

Burp suite Chapter 3 how to use burp suite agent

R language foundation

Bee guitar score high octave and low octave

Add in the registry right click to open in vscode

Brief introduction to XML

SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据

22-07-12 personal training match 1 competition experience
随机推荐
Dev gridcontrol 捕获按键事件
Awk operation
2022/7/9 exam summary
Vscode utility shortcut
Prefix infix suffix expression (written conversion)
Burp Suite-第八章 如何使用Burp Intruder
Write common API tools swagger and redoc
OSPF总结
第四天作业
Recurrence of strtus2 historical vulnerability
各位老师,请问在flinkcdc中,sqlserver如何获取到ddl?
matplotlib学习笔记
Exam summary on July 13, 2022
基础乐理 节奏联系题,很重要
Apple's tough new rule: third-party payment also requires a percentage, and developers lose a lot!
Use of room database in kotlin
Daily Note (11) -- word formula input arbitrary matrix
Bee guitar score high octave and low octave
Burp Suite-第四章 SSL和Proxy高级选项
Basic music theory rhythm connection problem, very important