~/Home ~/Notes ~/Categories

Kotlin basics

17 October, 2021  ·   Android Kotlin
  https://developer.android.com/
  1. fun is a leyword used to define a function.

    1 fun main() {
    2     println("Happy Birthday!")
    3     println("Jhansi")
    4     println("You are 25!")
    5 }
    
  2. print() vs println()

    • print() just prints the text without using a line break at end of string.
  3. val keyword is used to store the values that can be set only once. same like const in js. For declaring a changeable variable use var

    1    val age = 15
    
  4. To use a variable inside a print statement surround it with {}

    1   println("Age of a Surya is ${age}")
    
  5. Use a repeat() function to repeat an instruction for ‘n’ number of times. This is one of a kind of loops

    1    printSigns(){
    2        //prints + sign for 50 times
    3        repeat(50){
    4            print("+")
    5        }
    6    }
    
    • To create a Nested loop use repeat() inside a repeat()
      1//print the layers of the cake age times
      2fun printCakeBottom(age: Int, layers: Int) {
      3   repeat(layers) {
      4      repeat(age + 2) {
      5       print("@")
      6      }
      7      println()
      8   }
      9}
      
 kotlin
Array Operations↠ ↞Android Basics