diff --git a/1st-semester/pg/conditional-statements.md b/1st-semester/pg/conditional-statements.md new file mode 100644 index 0000000..b6664f7 --- /dev/null +++ b/1st-semester/pg/conditional-statements.md @@ -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") +} +``` \ No newline at end of file diff --git a/1st-semester/pg/data-storage.md b/1st-semester/pg/data-storage.md new file mode 100644 index 0000000..ceb2b4f --- /dev/null +++ b/1st-semester/pg/data-storage.md @@ -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 +``` \ No newline at end of file diff --git a/1st-semester/pg/functions.md b/1st-semester/pg/functions.md new file mode 100644 index 0000000..0be73d7 --- /dev/null +++ b/1st-semester/pg/functions.md @@ -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 +} +``` \ No newline at end of file diff --git a/1st-semester/pg/io.md b/1st-semester/pg/io.md new file mode 100644 index 0000000..1b269ba --- /dev/null +++ b/1st-semester/pg/io.md @@ -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") +} +``` \ No newline at end of file diff --git a/1st-semester/pg/loops.md b/1st-semester/pg/loops.md new file mode 100644 index 0000000..388139f --- /dev/null +++ b/1st-semester/pg/loops.md @@ -0,0 +1,135 @@ +# Loops + +Loops can be done in alot of ways in Kotlin, the main ones are: +- while +- do while +- repeat +- for + +## While + +Executes block while the expression is true. +*In case the expression is always false, it does not even enter the loop.* + +```kt +var counter = 0 +while (counter != 5) { + println(counter) + counter++ +} +``` + +Output: +``` +0 +1 +2 +3 +4 +``` + +## do ... while + +Works the same as `while`, but always executes **at least** 1 iteration before checking the expression + +```kt +do { + println("hi") +} while (false) +``` + +Output: +``` +hi +``` + +## repeat + +`repeat` just... repeats the times you tell him to. + +```kt +repeat(5) { + print("hi ") +} +``` +Output: +``` +hi hi hi hi hi +``` + +## for + +To use for loops, you mostly use "ranges". + +```kt +for (i in 0..5) { + println(i) +} +``` + +Output: +``` +0 +1 +2 +3 +4 +5 +``` + +You can use `until` or `..<` to exclude last iteration. + +```kt +println("until") +for (i in 0 until 5) { + println(i) +} + +println("..<") +for (i in 0 ..< 5) { + println(i) +} +``` + +Output: +``` +until +0 +1 +2 +3 +4 +..< +0 +1 +2 +3 +4 +``` + +## Iterating over collections + +In case you have an array/list and want to iterate over it, you can do it with `forEach`: + +```kt +val list = listOf("apple", "banana", "peach") +list.forEach { it -> + println(it) +} +``` + +or even with `for`: + +```kt +val list = listOf("apple", "banana", "peach") +for (fruit in list) + println(fruit) +} +``` + +Output: +``` +apple +banana +peach +``` \ No newline at end of file