Kotlin when
As with the preparation of manyif..else
expression, you can usewhen
expression, it is more readable.
It is used to select one of multiple code blocks to be executed:
typical example
Use the number of the day of the week to calculate the name of the day of the week:
val day = 4
val result = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day."
}
println(result)
// output "Thursday" (day is 4)
when
expression is similar to the Javaswitch
Statements.
It works as follows:
- treat (sb a certain way)
when
Variables (day
) Conduct an assessment. - commander-in-chief (military)
day
The value of the variable is compared to the value of each "branch". - Each branch starts with a value followed by an arrow (
->
) and a result. - If it matches, the associated code block is executed.
- utilization
else
Specifies the code to run if there is no match.
In the example above, theday
The value of the4
The printout will therefore be"Thursday"
。
Kotlin While loop
Loops can execute a block of code when a specified condition is reached. Loops are very handy because they save time, reduce errors, and make code more readable.
Kotlin while
circulate
while
The loop pass condition istrue
When you loop through a block of code:
grammatical
while (condition) {
// The block to execute
}
In the following example, as long as the counter variablei
less than5
The code in the loop will run over and over again:
typical example
var i = 0
while (i < 5) {
println(i)
i++
}
Note: Don't forget to add the variables used in the condition or the loop will never end.
Kotlin do..while
circulate
do..while
The loop iswhile
A variant of the loop. This loop will check if the condition istrue
Execute the block once before, and then as long as the condition istrue
and it repeats the execution loop.
grammatical
do {
// The block to execute
} while (condition)
The following example uses thedo/while
Loop. Even if the condition isfalse
, the loop will also be executed at least once, because the code block will be executed before the test condition:
typical example
var i = 0
do {
println(i)
i++
} while (i < 5)
The above is a brief description of thewhen
expressions as well aswhile
cap (a poem)do..while
Loops are described in detail. These features make writing and understanding conditions and loops easier and more intuitive.
Kotlin Break
and Continue
Kotlin Break
break
statement is used to jump out of the loop.
In the following example, when thei
be tantamount to4
when it jumps out of the loop:
var i = 0
while (i < 10) {
println(i)
i++
if (i == 4) {
break
}
}
Kotlin Continue
continue
statement is used to skip the code for a specific condition in one iteration of the loop and continue to the next iteration.
In the following example, the skip values are4
The situation:
var i = 0
while (i < 10) {
if (i == 4) {
i++
continue
}
println(i)
i++
}
Kotlin Arrays
Kotlin arrays
Arrays are used to store multiple values in a single variable rather than creating separate variables for each value.
To create an array, use thearrayOf()
function and place a comma-separated list of values in it:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
Accessing array elements
You can access an array element by referencing the index number (inside square brackets).
In this example, we access thecars
The value of the first element in the array:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
println(cars[0])
// Output Volvo
Note: The array index starts at0
Start:[0]
is the first element.[1]
is the second element, and so on.
Modifying array elements
To change the value of a specific element, quote the index number:
cars[0] = "Opel"
Example:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
cars[0] = "Opel"
println(cars[0])
// Now output Opel instead of Volvo
Array length/size
To find out how many elements are in an array, use thesize
Properties:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
println()
// Output 4
Checking for the existence of an element
You can use thein
operator to check for the presence of elements in the array:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
if ("Volvo" in cars) {
println("Exists!")
} else {
println("Does not exist.")
}
Iterate over the array
Usually when working with arrays, you need to iterate through all the elements.
You can use thefor
Loop over array elements, about which you will learn more in the next chapter.
The following sample outputcars
All elements in the array:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
for (x in cars) {
println(x)
}
The above is a brief description of thebreak
、continue
as well as a detailed description of basic array operations. These features make controlling loop flow and managing multiple data items more flexible and efficient.
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!