Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Operations on Variables

    var i = 0
    println(i)
    i++
    println(i)
    var j = 10
    i = i + j
    j = i - j
    println("$i $j")

    In this lesson we’ll continue introducing the basic building blocks of our computer programs. We’ll show how you can manipulate the data stored by your variables through simple mathematical operations.

    Reminder: Variables
    Reminder: Variables

    Last time we introduced variables, which allow us to store changing values as our program runs. We looked at how to declare and initialize those variables. The declaration tells Kotlin the name and type of the variable we are going to use, and the initialization sets the variable’s first value:

    // Dear Kotlin: I'm going to use a variable named catsAge to store integer values
    // And I'd like its initial value to be 6
    var catsAge: Int = 6

    And, normally we don’t need to include a type when we initialize the variable, since Kotlin can figure it out on its own:

    // Dear Kotlin: I'm going to use a variable named catsAge
    // I'd like its initial value to be 6
    // Based on that value, you can guess that I'm using it to store integer values!
    var catsAge = 6

    If you’re having any trouble understanding variables, please review the previous lesson.

    Comments
    Comments

    Before we continue, we need to introduce one new piece of Kotlin syntax: comments. Comments allow us to include descriptive text in our programs. Comments are completely ignored by the computer! They are entirely for the humans who write, read, and maintain the code.

    Kotlin supports two types of comment:

    // This is a single line comment. It starts with //
    /*
    This is a multi-line comment. It starts with the two-character symbol on the line above.
    And it ends with the two-character symbol on the line below.
    */

    Single-line comments are good when you have something short to say about something in your code. Multi-line comments that start with /* and end with */ are better when you have more to say and need to write one or more paragraphs.

    Even though comments are ignored by the computer, they are essential to writing good computer code. We’ll use comments frequently in our examples, and show you how you can use them to outline a plan for completing more complex programming tasks.

    Solve: Area Calculation (Practice)

    Created By: Geoffrey Challen
    / Version: 2020.6.0

    Computers can do basic math, quickly and with near-perfect accuracy. This is one of the many things that makes them so useful.

    Your friend wrote a simple function intended to save the area of a room in a variable called area given its width and height stored in int variables width and height. Unfortunately, their code has a small error. Go ahead and fix it for them! (Note that you do not need to declare or modify width and height. But you should declare and set area.)

    var area = width + height

    Modifying Variables
    Modifying Variables

    Now that we can store data in our programs we can harness the next fundamental computer superpower: math. Computers can do math, quickly and with (near) perfect accuracy. Many of the operations that we can perform on variables are mathematical in nature:

    var i = 10 * 20

    Variables can also be set based on the value of other variables:

    var i = 10
    var j = 20
    var k = i + j

    Understanding Variable Assignment
    Understanding Variable Assignment

    Some of you, particularly those with a mathematical bent, may be alarmed by certain lines of Kotlin code. Like this one:

    var i = 10
    i = i + 1
    println(i)

    In math, the statement on the second line is non-sensical. Regardless of the value of i, it can never equal itself plus one—nonsense!

    The source of the confusion here is the = symbol. In Kotlin, a single = does not indicate or test equality. It indicates assignment. We use it when we want to assign a value to a variable.

    To further the confusion, assignments in Kotlin are evaluated from right to left. So they are neither math nor English! That’s why the example above works.

    Let’s examine this assignment carefully together:

    var i = 1
    i = i + 1
    println(i)

    Self-Modification Shortcuts
    Self-Modification Shortcuts

    It’s fairly common in our programs to want to modify the value of a variable but start with its current value. For example:

    var dollars = 0
    // Call parents
    dollars = dollars + 100
    // Phew...
    println(dollars)

    These types of operations are common enough that there are a few shortcuts that you can use:

    var dollars = 0
    // Call parents
    dollars += 100
    // Phew...
    println(dollars)

    Similarly you can use -=, *=, and /=.

    Finally, adding and subtracting by one are so common that they even have an additional set of shortcuts:

    var dollars = 0
    // Call parents
    dollars += 1
    // WTF?
    println(dollars)
    // Call parents again
    dollars++
    println(dollars)
    // ...

    Similarly you can use ++ and --.

    Binary Negation
    Binary Negation

    What we’ve been covering in this lesson are known as operators. (We’ve covered at least a few of the ones listed in the linked reference. We’ll get to more soon!) Many of them are for working with numbers, since, to a computer, everything is a number.

    But there is one operator that we use to work with boolean values:

    var isCS124Awesome = false
    println(isCS124Awesome)
    // Extra credit?
    isCS124Awesome = !isCS124Awesome
    println(isCS124Awesome)

    Solve: Inspiration v. Perspiration

    Created By: Geoffrey Challen
    / Version: 2022.6.0

    Computers can do basic math, quickly and with near-perfect accuracy. This is one of the many things that makes them so useful.

    Learning to program takes time and energy, but as long as you keep trying, you'll get there. As famous inventor Thomas Edison once said, genius is 1% inspiration and 99% perspiration. Using that formula, compute the genius level as a number between 0 and 100 given two double variables: inspiration and perspiration, each between 0 and 1, and save it a double variable named genius. For example, given inspiration 1.0 and perspiration 0.5, you would set genius to 50.5.

    We've provided some starter code, but it's incorrect!

    var genius = 50 * inspiration + 50 * perspiration

    CS People: Alan Turing
    CS People: Alan Turing

    As we teach you the basics of computer science and programming, we’re also going introduce you to computer science people—the humans behind technology’s remarkable achievements.

    The name Turing comes up a lot in computer science. There’s the Turing machine, an abstract model of a machine that, despite its simplicity, can compute any algorithm(1) There’s the Turing test to determine whether a machine can be distinguished from a human. And there’s the Turing Award—computer science’s highest honor, and its equivalent of the Nobel Prize.

    But who was Alan Turing, and why did his life end tragically at the age of only 41? Watch the video below to find out more.

    Note that the British Government formally apologized to Alan Turing—but not until 2009, 55 years after his death.

    More Practice

    Need more practice? Head over to the practice page.