1. The flow of control


/** * is used to check and calculate the combination of various conditions and logic to control the running flow of App * loop: for, while and loop control * if: binary judgment * when: meticulous judgment ** /
fun main(args: Array<String>) {
    println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -, in turn, operating a sequence (such as collection type) each item in the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -")
    //for: 1. Operate on each item in a sequence (such as a collection type) with a predictable number of executions
    val numbers = arrayOf(1.2.3.4)
    for (number in numbers) {
        println(number)
    }

    println("---------------- repeat ----------------")
    // 2. Repeat execution: for(a in 1.. 10) {/ /}
    for (n in 1.3.) {
        println(n)
    }

    println("----------------while: loop execution ----------------")
    // While: loop through a series of operations until the condition is not true, suitable for an unknown number of executions
    var number = 1
    while (number < 5) {
        number++
        println("hello")}// Control in loop: continue completes the loop, break completes the loop
    println("---------------- End this loop: continue----------------")
    val numbers2 = arrayOf(1.2.3.4)
    for (number in numbers2) {
        if (number == 2) {
            break
        }
        println(number)
    }

    println("---------------- end the loop: break----------------")
    var number2 = 1
    while (number2 < 5) {
        number2++
        if (number2 == 3) {
            break
        }
        println("hello")}//if: a conditional statement, which is itself an expression
    println(---------------- conditional statement: if----------------)
    val a = 3
    val b = 5
    val result = if (a > b) "A greater than b" else "A less than b"
    println(result)

    //when: can judge the size/range/value expression/type of a variable
    println(---------------- conditional statement: if----------------)
    val c = 11
    when (c) {

        in 0.10. -> {
            println("Between zero and ten.")}11.13.15 -> {
            println("Values of 11,13,15")}is Int -> {
            println("C is an integer.")}else -> {
            println("C is of type String")}}}Copy the code

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -, in turn, operating a sequence (such as collection type) each item in the -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -1
2
3
4---------------- Repeat ----------------1
2
3
----------------while: loop execution -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- hello, hello hello hello -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - end of the cycle:continue----------------
1---------------- to close the loop:break---------------- hello ----------------if---------------- a is less than b ----------------if-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - belongs to11.13.15The values in theCopy the code

2. The function


/** * a function is a named piece of code * containing arguments and (optionally) return values, which can default to */

fun main(args: Array<String>) {
    //F1 function name
    // Can be called with parameter name, better readability (but Java function call invalid)

    println("---------------- is similar to C++ giving arguments a default value ----------------")
    val a = add(3)
    println(a)

    println("---------------- normal call ----------------")
    val b = add(3.5)
    println(b)

    println("---------------- variable pass element ----------------")
    println(sum(1.2.3.4.20))

    println("---------------- variable parameter transfer Array *Array----------------")
    val c = intArrayOf(1.2.3.4.20)
    println(sum(*c))
}



/** * Any Any type * Int Int * : Int means return integer */
fun add(x: Int, y: Int = 1): Int {
    return x + y
}

// The vararg modifier
fun sum(vararg x: Int): Int {
    var total = 0

    for (i in x) {
        total += i
    }
    return total
}
Copy the code

---------------- is similar to C++ giving the parameter a default value ----------------4---------------- The normal call is ----------------8---------------- variable argument pass element ----------------30---------------- variable parameter pass Array *Array----------------30
Copy the code

3. First understanding of lambda and higher-order functions


/** * Higher-order functions: 1 Arguments or return values are of type function * Function: (argument) -> return value * lambda: short for an unnamed function {(argument) -> function execution statement} * Other languages call closures, that is, the ability to access variables outside of their scope */

fun main(args: Array<String>) {
    // Higher-order functions: describe the results of a task, rather than using loop detailed calculations

    println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the map: common and element type integral transformation of collection types -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -")
    //map: Commonly used for integral conversions to element types of collection types
    // The convention for arguments in lambda is called it
    val a = arrayOf(1.2.3.4.5.6.7.8)

    val b = a.map { "To 1$it" }

    for (s in b){
        println(s)
    }
    println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the filter: the filtered collection types -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -")
    //filter: Filter the collection type

    val c = a.filter { it %2= =0 }
    for (i in c) {
        println(i)
    }
    println("----------------filter uses: sum over filter numbers ----------------")
    var sum = 0
    a.filter { it %2= =0 }.forEach{
        sum+=it
        println(it)
    }
    println("Sum:$sum")}Copy the code

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the map: common and element type integral transformation of collection types -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - a11into12into13into14into15into16into17into18----------------filter: filters the collection type ----------------2
4
6
8----------------filter Use: sum the filter numbers ----------------2
4
6
8Sum:20
Copy the code