I am also a beginner in Kotlin, if there is any mistake, please help to point out, continue to update

Android: Kotlin – Package – Control Flow – Return and jump – Basic syntax (3)

Android: Kotlin 内 容 提 要 : Kotlin 内 容 提 要 : Kotlin 内 容 提 要 :

This article focuses on Kotlin’s package – Control Flow – return and jump

package

A source file begins with a package declaration:

package foo.bar 
fun bza(a) {} 
class Goo {}
/ /...
Copy the code

All contents of the source file, such as classes and functions, are included in the package declaration. So in the example above, the full name of bza() would be foo.bar.bza and the full name of Goo would be foo.bar.goo. If no package name is specified, the contents of the file are subordinate to the nameless “default” package.

Imports

In addition to the packages imported by default in a module, each file can have its own import instructions.

import foo.Bar // Bar can now be used without conditions
importFoo.*/ / all in foo can be usedimport bar.Bar as bBar // bBar stands for 'bar.bar
Copy the code

The import keyword is not limited to imported classes; You can also use it to import other declarations:

  • Top-level functions and attributes
  • Functions and properties declared in object declarations
  • — Enumeration constants

Visibility and package nesting

  • If the topmost declaration is marked private, it is private to its corresponding package.
  • If a package has a private property or method, it is visible to all subpackages.
  • Note that members outside the package are not imported by default; for example, we cannot obtain members of foo after importing foo.bar

Process control

If expression

In Kotlin, if is an expression that can return a value. Thus Kotlin has no ternary operator (condition? Then: else), because the if statement already satisfies the effect.

// Traditional usage
var max = a 
if (a < b)
max = b 
/ / take the else
var max: Int 
if (a > b) 
max = a 
else
max = b 
// as an expression
val max = if (a > b) a else b
Copy the code

The if branch can be used as a block, and the last expression is the value of the block:

val max = if (a > b){
	 print("Choose a") 
	 a 
	 }else{
	 print("Choose b") 
	 b 
 }
Copy the code

If you use If as an expression rather than a statement (for example, returning its value or assigning it to a variable), you need an else branch.

When the expression

When replaces switch in the Java style language. The simplest use is something like this

when (x) { 
	1 -> print("x == 1") 
	2 -> print("x == 2") 
	else- > {// Notice this statement block
	print("x is neither 1 nor 2")}}Copy the code

When checks all branches until one condition is met. When can be used as an expression or a sound. If used as an expression, the branch that satisfies the condition is the total expression. If used as a declaration, the value of the branch is ignored. (Like if expressions, each branch is a statement block, and its value is the value of the last expression.) The else branch is matched by default if none of the other branches match. Else branching is mandatory if you use when as an expression, unless the compiler can prove that the branching condition has covered all possibilities. Branching conditions can be joined if there are branches that can be treated in the same way:

when (x) { 
	0.1 -> print("x == 0 or x == 1") 
	else -> print("otherwise")}Copy the code

You can even use in or! In checks whether a value is in a range or a collection

when (x) { 
	in 1.10. -> print("x is in the range") 
	in validNumbers -> print("x is valid")!in 10.20. -> print("x is outside the range") 
	else -> print("none of the above")}Copy the code

The for loop

The for loop iterates through any provided iterators. The syntax looks like this:

for (item in collection) 
	print(item)
Copy the code

For can iterate over any provided iterator, defined as

  • There is an iterator() member function or extension function whose return type is
  • — There is a next() member function or extension function
  • — Has a hasNext() member function or extension function that returns Boolean

A for loop over an array does not create an iterator object, but is compiled into an index-based loop. If you want to iterate through the index of a list or array,

for (i in array.indices) 
	print(array[i])
Copy the code

The while loop

And while the do… While is like Java

while (x > 0) { 
	x-- 
}
do {
val y = retrieveData() 
} while(y ! =null) // y is visible here
Copy the code

Using break and continue in loops Kotlin supports the traditional break and continue operators

Return and jump

Kotlin has three types of structured jump expressions:

  • — return
  • — break Ends the most recent closed loop
  • — continue jumps to the next loop of the nearest closed loop

The break and continue tags can be added to expressions in Kotlin. Tags are denoted by an @ ending, such as: ABC @, fooBar@ are valid insert code slice here. To use tag syntax, just look like this:

loop@ for (i in 1.100.) {// ... 
}
Copy the code

Now we can use the tag to make a quick break or continue jump:

loop@ for (i in 1.100.) { 
	for (j in i.100.) { 
	if(...).break@loop}}Copy the code

Break is the expression after the jump tag, and continue is the next iteration of the jump to the loop.

Back to the tag

In literal functions, local functions, and object expressions, functions can be wrapped in Kotlin. Return allows us to return to the outer function. The most important example is returning from a literal function

fun foo(a) { 
	ints.forEach { 
	if (it == 0) return 
	print(it) 
	} 
}
Copy the code

The return expression returns the nearest closed function, such as foo (note that such nonlocal returns can only be used in inline functions). If we need to return from a literal function we can use the tag to modify the return:

fun foo(a) { 
	ints.forEach lit@ { 
	if (it ==0) return@lit print(it)}}Copy the code

Now it just returns from the literal function. Always use a more convenient, implicit label: for example, a label with the same name as the lambda expression passed in.

fun foo(a) { 
	ints.forEach { 
	if (it == 0) return@forEach 
	print(it) 
	} 
}
Copy the code

Alternatively, we can substitute function expressions for anonymous functions. Using a return statement in a function expression returns from the function expression.

fun foo(a) { 
	ints.forEach(fun(value: Int){ 
	if (value == 0) return 
	print(value) 
	}) 
}
Copy the code

When a value is returned, the parser gives a reference

return@a 1
Copy the code

Instead of returning a tag expression (@a 1), the naming function automatically defines the tag:

foo outer() { 
	foo inner() { 
		return@outer}}Copy the code

This article focuses on Kotlin’s package – Control Flow – return and jump

I am also a beginner in Kotlin, if there is any mistake, please help to point out, continue to update