Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions 1st-semester/pg/conditional-statements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Conditional statements

You can control your code using conditional statements:

- `if`
- `else`
- `else if`
- `when`

The if statement evaluates the test expression inside the parenthesis ().

- If the test expression is evaluated to true, statements inside the body are executed.
- If the test expression is evaluated to false, statements inside the body are not executed.

You can read it almost as an english phrase:

```kt
if (something_is_true) {
// execute this
} else {
// if something is not true, execute else
}
```

Example:

```kt
if (true) {
println("im inside") // will run
}

if (false) {
println("nope") // wont run
}
```

Example using `if`, `else if`, `else` and `when`.

```kt
val age = 18

if (age > 18) {
println("adult")
} else if (age == 18) {
println("well I guess you're an adult")
} else {
println("not an adult")
}
```

This can be simplified to:
```kt
val age = 18

when {
age > 18 -> println("adult")
age == 18 -> println("well I guess you're an adult")
else -> println("not an adult")
}
```
55 changes: 55 additions & 0 deletions 1st-semester/pg/data-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Data storage

Declaring variables in Kotlin:
```kt
val age = 18
val name = "roby"
val price = 33.5
```

What if you dont want to give it a initial value?
You can do that, but:
- you have to tell the compiler what type the variable will store
- you can not use the variable until you assign a value

```kt
val x: Int // variable declaration that will store an Integer
println("hello")
x = 3 // ok
```

```kt
val x: Int // variable declaration that will store an Integer
println(x) // error, x was not defined yet
x = 3
```

When declaring a variable without writing its type, you need to give it a initial value, so the compiler can infer its type.

```kt
val z // error, the compiler does not know what type z is

val x: Int // variable declaration that will store an Integer
x = 3 // ok

val y: Int // variable declaration that will store an Integer
y = "hi" // error, "hi" is not an integer

val name = "john" // the compiler analyzes "john" and uses its type (String)
val name: String = "john" // same as above

var age: Int = "john" // error, age's type is Integer, can't store a String
```

In Kotlin there is also mutable and immutable variables:

- `var` - variable that you can change its values
- `val` - value that can not be reassigned after it's initializzed.

```kt
val x = 5 // x will store 5 (an Integer)
x = 10 // error, you can't reassign values

var name = "apple" // name will store "apple" (a String)
name = "melon" // ok, name will store "melon" now
```
99 changes: 99 additions & 0 deletions 1st-semester/pg/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Functions

Kotlin has many functions that you can use, one example is `println`, which takes a `String` argument with text that you want to print.

```kt
println("test")
```

This is how you declare your own functions:

```kt
fun main() {
println("hi")
}

fun add(a: Int, b: Int): Int {
return a + b
}
```

The main function does not do much, just prints "hi".
Lets see the `add` function:

- function name: `add`
- parameters: `a` and `b` (both Integers)
- return type: `Int`
- this is the type of the "data" that the function will return, in this case we return `a+b`, when you add 2 `Int`'s, it produces another `Int`
- function body: `return a + b`

How to use the function?

```kt
fun add(a: Int, b: Int): Int {
return a + b
}

fun main() {
val x = 3
val y = 5
val result = add(x, y)
// result will store the return value of add (a+b = x+y = 3+5)
}
```

This calls the function and stores the result (`a+b`) in the value `result`.
With Kotlin you can cool stuff, like this:
```kt
fun add(a: Int, b: Int): Int {
return a + b
}

// is the same as

fun add(a: Int, b: Int): Int = a + b // single return expression

// and the same as

fun add(a: Int, b: Int) = a + b // compiler infers the type automatically
```

Another example:

```kt
// This returns true if the parameter age is >= 18
fun isAdult(age: Int): Boolean {
if (age >= 18) return true
else return false
}

// same as putting return out of if expressions

fun isAdult(age: Int): Boolean {
return if (age >= 18) true
else false
}

// same as

fun isAdult(age: Int): Boolean = if (age >= 18) true else false

// same as

fun isAdult(age: Int) = if (age >= 18) true else false
```

There is also "extension" functions, where you can write new functions for a class or an interface from a third-party library that you can't modify.

To declare an extension function, prefix its name with a receiver type, which refers to the type being extended. The following adds a `isAdult` function to `Int`:

```kt
fun Int.isAdult() = if (this >= 18) true else false

fun main() {
val x = 23.isAdult() // x is "true"

val age = 17
println(age.isAdult()) // will print false
}
```
55 changes: 55 additions & 0 deletions 1st-semester/pg/io.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Input & Output

You can use `println()` and `print()` functions to send output to the standard output (screen):

```kt
fun main() {
println("hello") // prints hello when you run the program
}
```

The difference between `println` and `print` is that `println` will insert a new line after.

```kt
fun main() {
println("hello 1")
println("hello 2")
print("hi")
print("hehe")
}
```

Output:
```
hello 1
hello 2
hihehe
```

You can print variables by using `$`:
```kt
val name = "john"
println("My name is $name") // prints "My name is john"
```

In case you wanna print a result of a function, or access object's methods/members, you need to use braces.

You can print variables by using `$` and `println`:
```kt
fun myName(name: String) = "My name is $name"

println(" ${myName("john")} ") // prints "My name is john"
println(myName("john")) // prints "My name is john"
println(" $myName("john") ") // error
```

To read input, you use the `readln()` function:

```kt
fun main() {
print("write something: ")

val input = readln()
println("text entered: $input")
}
```
Loading