Kotlin loop
When you work with arrays, you often need to iterate through all the elements.
To iterate through the array elements, use thefor
Loop andin
Operator:
typical example
exportscars
All elements in the array:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
for (x in cars) {
println(x)
}
You can traverse various types of arrays. In the above example, we used an array of strings.
In the following example, we iterate through an array of integers:
typical example
val nums = arrayOf(1, 5, 10, 15, 20)
for (x in nums) {
println(x)
}
traditionalFor
circulate
together withJava
Unlike other programming languages, theKotlin
There is no traditionalfor
Loop.
existKotlin
Middle.for
Loops are used to traverse arrays, ranges, and other things that contain countable values.
Kotlin Scope
utilizationfor
loop, you can also use the..
Creates a range of values:
typical example
Prints the entire alphabet:
for (chars in 'a'..'x') {
println(chars)
}
You can also create numeric ranges:
typical example
for (nums in 5..15) {
println(nums)
}
Note: The first and last values are included in the range.
Check if the value exists
You can also use thein
operator checks if the value exists within the range:
typical example
val nums = arrayOf(2, 4, 6, 8)
if (2 in nums) {
println("Exists!")
} else {
println("Does not exist.")
}
typical example
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
if ("Volvo" in cars) {
println("Exists!")
} else {
println("Does not exist.")
}
Interrupt or continue a range
You can also find out more about the range/for
loop using thebreak
cap (a poem)continue
Keywords:
typical example
(coll.) fail (a student)nums
be tantamount to10
The cycle stops when the
for (nums in 5..15) {
if (nums == 10) {
break
}
println(nums)
}
typical example
Skip the loop in the10
and move on to the next iteration:
for (nums in 5..15) {
if (nums == 10) {
continue
}
println(nums)
}
Kotlin Functions
A function is a piece of code that runs only when called.
You can pass data (called parameters) to a function.
Functions are used to perform certain operations and they are also known as methods.
Predefined Functions
As it turns out, you already know what a function is. You have been using it throughout this tutorial!
For example.println()
is a function. It is used to output/print text to the screen:
typical example
fun main() {
println("Hello World")
}
Create your own function
To create your own function, use thefun
keyword and write the name of the function followed by the parentheses()
:
typical example
Create a file namedmyFunction
function which should output some text:
fun myFunction() {
println("I just got executed!")
}
call function
Now you have created a function that can be executed by calling it.
reliable sourceKotlin
to call a function, write the name of the function, followed by two parentheses()
。
In the following example, themyFunction()
When called, it prints some text (the action):
typical example
fun main() {
myFunction() // call (programming) myFunction
}
// exports "I just got executed!"
You can call a function multiple times if you wish:
typical example
fun main() {
myFunction()
myFunction()
myFunction()
}
// I just got executed!
// I just got executed!
// I just got executed!
function parameter
Information can be passed as an argument to a function.
Arguments are specified after the function name, inside parentheses. You can add as many arguments as you want, just separate them with commas. Note that you must specify the type of each parameter (Int
、String
(etc.).
The following example has a function that takes a function namedfname
The string argument to the function. When calling a function, we pass a name, which is used inside the function to print the full name:
typical example
fun myFunction(fname: String) {
println(fname + " Doe")
}
fun main() {
myFunction("John")
myFunction("Jane")
myFunction("George")
}
// John Doe
// Jane Doe
// George Doe
When an argument is passed to a function, it is called a real parameter. So, from the example above:fname
is a parameter, and theJohn
、Jane
cap (a poem)George
It's the actual senator.
Multiple parameters
You can have as many parameters as you want:
typical example
fun myFunction(fname: String, age: Int) {
println(fname + " is " + age)
}
fun main() {
myFunction("John", 35)
myFunction("Jane", 32)
myFunction("George", 15)
}
// John is 35
// Jane is 32
// George is 15
Note: When using multiple arguments, the function call must have the same number of real parameters as the number of arguments, and the real parameters must be passed in the same order.
return value
In the above example, we used a function to output a value. In the following example, we will use a function to return a value and assign it to a variable.
To return a value, use thereturn
keyword and specify the return type after the function's parentheses (in this case, theInt
):
typical example
have aInt
parameters andInt
Functions of the return type:
fun myFunction(x: Int): Int {
return (x + 5)
}
fun main() {
var result = myFunction(3)
println(result)
}
// 8 (3 + 5)
Use two parameters
typical example
has twoInt
parameters andInt
Functions of the return type:
fun myFunction(x: Int, y: Int): Int {
return (x + y)
}
fun main() {
var result = myFunction(3, 5)
println(result)
// 8 (3 + 5)
Shorthand syntax for return values
There is also a shorter syntax for returning values. You can use the=
operator instead ofreturn
without specifying a return type.Kotlin
Smart enough to figure out the return type automatically:
typical example
fun myFunction(x: Int, y: Int) = x + y
fun main() {
var result = myFunction(3, 5)
println(result)
// 8 (3 + 5)
ultimate
To make it easier for peeps on other devices and platforms to view past articles:
WeChat Search:Let us Coding
Follow to get the latest articles tweeted
If you find it helpful, please like, favorite and follow it!