Variables¶
Expression¶
Expressions simply describe any computation that produces a value and are the basic building blocks of any program.
Examples of expressions
You've probably seen expressions before. Here are some examples: $$ \large 22 + 4, \frac{22}{4}, \sin \pi, \sum_{i=1}^n{i}, -2204, \log x, 22.04 $$
Call Expressions¶
Let's take a look at the expression add(2, 3)
. This is called a call expression. It is composed of the following parts:
add
: the function name, known as the operator2
and3
: the operands
When we evaluate a call expression, we
- Evaluate the operands from left to right
- Apply the operator (function) to the operands (arguments)
Practice: Evaluate add(2, add(3, 4))
Let's walk through step-by-step how we would evaluate this expression.
stateDiagram
state "add(2, add(3, 4))" as [original]
state "evaluate 2" as [2]
state "evaluate add(3, 4)" as [a34]
state "evaluate 3" as [3]
state "evaluate 4" as [4]
state "apply 3 + 4" as [ap34]
state "7" as [7]
state "apply 2 + 7" as [ap27]
state "9" as [9]
[*] --> [original]
[original] --> [2]
[original] --> [a34]
[a34] --> [3]
[a34] --> [4]
[3] --> [ap34]
[4] --> [ap34]
[ap34] --> [7]
[2] --> [ap27]
[7] --> [ap27]
[ap27] --> [9]
[9] --> [*]
Keep in mind this whole time, we are evaluating each operand from left to right. Once we encounter another call expression, we begin the call expression evaluation for the specific call expression. Finally we apply the operator to the operands.
Assignment Statements¶
All programs need to be able to store data, and variables help you to do just that. In Kotlin, you can declare:
- read-only variables with
val
- mutable variables with
var
When you declare a variable, you assign it an expression with the =
operator. This is called an assignment statement.
Example of val
We can declare a read-only variable x
with the value 22
, and a read-only variable y
with the value 4
.
Example of var
Let's say my bank account has $100
in it. I can declare a mutable variable bankAccount
with the initial value of 100
.
Why not declare everything as a var
?
Is it alright to legal everything as a var
? Yes! No ones stopping you
However, it is considered good practice to declare variables as val
by default, and only declare variables as var
if necessary. Declaring variables properly can not only let other people know how you intend to use the variable, but also help the compiler optimize your code and catch any potential bugs.
String Templates¶
It's useful to know how to print the contents of variables to standard output. You can do this with string templates $
, which gives access data stored in variables and other objects, and convert them into strings.
Info
A string value is a sequence of characters in double quotes ""
. This can include words, sentences, and even whole paragraphs. See more about Strings here
fun main() {
//sampleStart
val customers = 10
println("There are $customers customers")
// There are 10 customers
println("There are ${customers + 1} customers")
// There are 11 customers
//sampleEnd
}
Comments
You may have noticed the //
in the code above. This is a comment. Comments are ignored by the language, and typically provide information about the code. In this case, the comments are used to show the output of the code. Another way to comment is with /*
and */
. This is useful for multi-line comments.
Practice¶
Complete the code to make the program print "Mary is 20 years old" by using the information provided in the variables name
and age
.
Solution
The solution uses string templates to get the values of name
and age
and combine them into the expected format.
Basic Data Types¶
Every variable and data structure in Kotlin has a data type. Data types are important because they tell the compiler what you are allowed to do with that variable or data structure. In other words, what functions and properties it has.
Kotlin was able to tell in the previous example that customers has type: Int
through a method known as type inference. Because customers
was assigned an integer value, Kotlin infers that customers has numerical data type: Int
. As a result, the compiler knows that you can perform arithmetic operations with customers
fun main() {
//sampleStart
var customers = 10
// Some customers leave the queue
customers = 8
customers = customers + 3 // Example of addition: 11
customers += 7 // Example of addition: 18
customers -= 3 // Example of subtraction: 15
customers *= 2 // Example of multiplication: 30
customers /= 3 // Example of division: 10
println(customers) // 10
//sampleEnd
}
public class Main {
public static void main(String[] args) {
int customers = 10;
// Some customers leave the queue
customers = 8;
customers = customers + 3; // Example of addition: 11
customers += 7; // Example of addition: 18
customers -= 3; // Example of subtraction: 15
customers *= 2; // Example of multiplication: 30
customers /= 3; // Example of division: 10
System.out.println(customers); // 10
}
}
customers = 10
# Some customers leave the queue
customers = 8
customers = customers + 3 # Example of addition: 11
customers += 7 # Example of addition: 18
customers -= 3 # Example of subtraction: 15
customers *= 2 # Example of multiplication: 30
customers /= 3 # Example of division: 10
print(customers) # 10
Tip
+=
, -=
, *=
, /=
, and %=
are augmented assignment operators. Essentially, they assign a new value to the variable on the left side of the operator with the result of the operation on the right side of the operator.
For example, customers += 7
is equivalent to customers = customers + 7
, and customers /= 3
is equivalent to customers = customers / 3
.
For more information, see Augmented assignments
Category | Data Type | Description |
---|---|---|
Integers | Byte , Short , Int , Long | Any whole number that can be positive, negative, or zero. |
Unsigned Integers | UByte , UShort , UInt , ULong | Any integer, but cannot be negative |
Floating Point Numbers | Float , Double | Any decimal number |
Boolean | Boolean | Represents a true or false state |
Characters | Char | Any single character Unicode or ASCII character |
String | String | Any combination of characters |
Advanced Assignment Statements¶
With your new-found knowledge of basic types, you unlock the ability to use more advanced assignment statements. While these are not common in FRC, they are still useful to know about.