0% found this document useful (0 votes)
33 views

This Is The Program Code in The Editor

This document provides an explanation of a "Hello World" Kotlin program and discusses various Kotlin programming concepts like functions, variables, comments, and using functions to organize code. It includes examples of printing text, declaring variables, inline and block comments, calling functions, and using functions and loops to print a cake with candles for a birthday celebration.

Uploaded by

shubham pandey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

This Is The Program Code in The Editor

This document provides an explanation of a "Hello World" Kotlin program and discusses various Kotlin programming concepts like functions, variables, comments, and using functions to organize code. It includes examples of printing text, declaring variables, inline and block comments, calling functions, and using functions and loops to print a cake with candles for a birthday celebration.

Uploaded by

shubham pandey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

This is the program code in the editor:

fun main() {
    println("Hello, world!")
}
Let's see what this program does!
1. In the editor, in the top-right corner, find the white or green

triangle   and click it to run the program.


2. Look at the pane at the bottom.
Hello, world!
3. Notice Hello, world! printed, like in the image above. So now you
know what this program does: It prints, or outputs, a hello world
message.
--------------------------------------------------------------------------------------------------
Understand the parts of the program
fun main() {}
 fun is a word in the Kotlin programming language. fun stands for
function. A function is a section of a program that performs a specific
task.
 main is the name of this function. Functions have names, so they
can be distinguished from each other. This function is called main,
because it is the first, or main, function that is called when you run
the program. Every Kotlin program needs a function named main.
 Inside the parentheses, you can put information for the function to
use. This input to the function is called "arguments" or args for short.
You will learn more about arguments later.
 Notice the pair of curly braces {} after the parentheses. Inside a
function is code that accomplishes a task. These curly braces
surround those lines of code.
println("Happy Birthday!")
This line of code prints the Happy Birthday! text.
 println tells the system to print a line of text.
 Inside the parentheses you put the text to be printed.
 Notice that the text to be printed is surrounded by quotes. This tells
the system that everything inside the quotation marks should be
printed exactly as given.
To actually print the text, this whole println instruction has to be inside
the main function.
 The print() instruction just prints the text without adding a line break
at the end of each string.
 Use \n inside the text to add a line break. For example, "line \n
break". Adding a line break changes the output as shown below.

Comments
An inline comment starts with // followed by text, as shown below.

val age = 5
This line means:
 val is a special word used by Kotlin, called a keyword, indicating that
what follows is the name of a variable.
 age is the name of the variable.
 = makes the value of age (on its left) be the same as the value on its
right. In math, a single equal sign is used to assert that the values on
each side are the same. In Kotlin, unlike in math, a single equal sign
is used to assign the value on the right to the named variable on the
left.
A developer would say it like this: This line declares a variable
named age whose assigned value is 5.
To use a variable inside a print statement, you need to surround it with
some symbols that tell the system that what comes next is not text, but a
variable. Instead of printing text, the system needs to print the value of
the variable. You do this by putting your variable inside curly braces
preceded by a dollar sign, like in the example below.
${variable}
Example
1. a variable called name for the name of the birthday person and set
its value to "Rover".
val name = "Rover"
2. Replace the name Rover in the birthday message with the variable,
as shown below.
println("Happy Birthday, ${name}!")

Your Kotlin program always has to have a main() function. In addition,


you can create and use functions of your own. Just like variables help you
avoid duplicating work, functions can help you avoid writing the same
code multiple times. In your code, the print statements for the top and
bottom of the banner are exactly the same. Let's create and use a
function for printing those borders.
1. In the editor, below the main() function, insert an empty line, just to
give you some room to work. The system ignores empty lines, and
you can insert them wherever they are helpful for organizing your
code.
2. Create a function. Start with the fun keyword, followed by a
name, printBorder, a pair of parentheses (), and a pair of curly
braces {}, as shown below.
3. Inside the main() function, write a print statement for the border
between the curly braces of the printBorder() function.
fun printBorder() {
    println("=======================")
}
and to use it simply write printborder() wherever it is required.
Example
fun main() {
    printBorder()
    println("Happy Birthday, Jhansi!")
    printBorder()
}

fun printBorder() {
    println("=======================")
}
you can make your code more smart by using repeat()
fun main() {
    val border = "%"
    printBorder(border)
    println("Happy Birthday, Jhansi!")
    printBorder(border)
}
fun printBorder(border: String) {
    repeat(23) {
        print(border)
  }
    println()
}
again modify your code
fun main() {
    val border = "`-._,-'"
    val timesToRepeat = 4
    printBorder(border, timesToRepeat)
    println("  Happy Birthday, Jhansi!")
    printBorder(border, timesToRepeat)
}
fun printBorder(border: String, timesToRepeat: Int) {
    repeat(timesToRepeat) {
        print(border)
  }
    println()
}
observe it
fun main() {
    val age = 24
    val layers = 5
    printCakeCandles(age)
    printCakeTop(age)
    printCakeBottom(age, layers)
}

fun printCakeCandles(age: Int) {


    print (" ")
    repeat(age) {
          print(",")
  }  
    println() // Print an empty line

    print(" ") // Print the inset of the candles on the cake


    repeat(age) {
        print("|")
  }  
    println()
}

fun printCakeTop(age: Int) {


    repeat(age + 2) {
        print("=")
  }
    println()
}

fun printCakeBottom(age: Int, layers: Int) {


    repeat(layers) {
        repeat(age + 2) {
            print("@")
    }
        println()
  }  
}

You might also like