Chapter: 9
If statement
Conditionals
The if statement allows code to run if a condition is satisfied.
It has the following syntax:
if (condition) {
// some code to run
}
// some code to run
}
KT
For example:
var age = 24
if (age >= 18) {
println("Welcome")
}
if (age >= 18) {
println("Welcome")
}
KT
The code of the if statement will not execute if the condition is false.
else
The if statement can be followed by an else statement, which will run if the condition is false.
For example:
var age = 12
if (age >= 18) {
println("Welcome")
}
else {
println("Not allowed")
}
if (age >= 18) {
println("Welcome")
}
else {
println("Not allowed")
}
KT
The code above will output "Not allowed" as the condition of the if statement evaluates to false.
else
What is the output of this code? var x = 8 if (x > 3) { x += 1 } else { x -= 2 } println(x)
else if
You can check for multiple conditions using the else if statement.
For example, the following code checks whether the given number is positive, negative, or zero:
val num = -7
if (num > 0) {
println("Positive")
}
else if (num < 0) {
println("Negative")
}
else {
println("Zero")
}
if (num > 0) {
println("Positive")
}
else if (num < 0) {
println("Negative")
}
else {
println("Zero")
}
KT
You can have multiple else if statements, but only one else statement.
else if
How many "else if" statements can an if statement contain?
Chapter: 10
Conditional Expressions
Conditional Expressions
A useful feature of Kotlin is the use of if statements to assign a value to a variable. This is called a conditional expression.
For example:
val num = -7
val result = if (num > 0) "Positive" else "Negative"
println(result)
val result = if (num > 0) "Positive" else "Negative"
println(result)
KT
Note, that if we have only one line of code, we can omit the curly braces for if/else.
Conditional Expressions
What is the output of this code? val x = 4 val y = if (x > 6) x-2 else x+2 println(x+y)
when
As the complexity of your conditional statement grows, you can replace the if/else expressions with a when expression, as shown in the following example:
val num = -7
val result = when {
num > 0 -> "Positive"
num < 0 -> "Negative"
else -> "Zero"
}
println(result)
val result = when {
num > 0 -> "Positive"
num < 0 -> "Negative"
else -> "Zero"
}
println(result)
KT
The when expression is equivalent to an if/else if/else expression, but is shorter and easier to read.
Combining Conditions
You can combine multiple conditions in if/when statements using the logical and && and or || operators.
For example, let's check if the value of num is in a certain range:
var num = 42
if(num >= 18 && num <= 60) {
println("Yes")
}
if(num >= 18 && num <= 60) {
println("Yes")
}
KT
Similarly, the or || operator can be used to check if any one of the conditions is true
var name = "John"
if(name == "Jon" || name == "John") {
println("Hi there")
}
if(name == "Jon" || name == "John") {
println("Hi there")
}
KT
You can combine as many conditions as you want.
Combining Conditions
Fill in the blanks to output "Teenager" if the age value is between 13 and 18.
Chapter: 11
While Loop
while Loops
The while loop is used to repeat a block of code as long as a given condition is true.
For example, let's print "Hello" 5 times:
var i = 1
while (i <= 5) {
println("Hello")
i++
}
while (i <= 5) {
println("Hello")
i++
}
KT
Every cycle of a loop is called an iteration.
while Loops
We can use a while loop to calculate the sum of numbers from 1 to 100:
var sum = 0
var i = 1
while (i<=100) {
sum += i
i++
}
println(sum)
var i = 1
while (i<=100) {
sum += i
i++
}
println(sum)
KT
During each iteration, the sum variable adds the value of i, which is then incremented by one.
Chapter: 12
Break and Continue
break
The break statement can be used to stop a loop prematurely.
For example, let's stop the while loop that calculates the sum of the numbers as soon as the sum value becomes greater than 1000:
var sum = 0
var i = 1
while (i<=100) {
sum += i
i++
if(sum > 1000) {
break
}
}
println(sum)
var i = 1
while (i<=100) {
sum += i
i++
if(sum > 1000) {
break
}
}
println(sum)
KT
A break statement can also be used to stop an infinite loop when a certain condition is met. An example infinite loop is while(true)
continue
Similar to break, the continue statement skips an iteration of the loop.
Let's use the continue statement to calculate the sum of only the even numbers:
var sum = 0
var i = 1
while (i <= 100) {
i++
if(i%2 != 0) {
continue
}
sum += i
}
println(sum)
var i = 1
while (i <= 100) {
i++
if(i%2 != 0) {
continue
}
sum += i
}
println(sum)
KT
It is important to increment the loop variable i before the continue statement. If we increment it after the continue statement, the loop will become infinite, as it will not change the value of i before skipping the iteration.
Chapter: 13
Arrays
Arrays
So far we have seen how to store values using variables.
Imagine a program where you need to store the names of contacts - you would have to define a separate variable for each name.
Arrays allow you to store multiple values in a single variable!
For example:
var contacts = arrayOf("John", "James", "Amy")
KT
The contacts array now stores 3 string values.
Arrays
Fill in the blanks to declare a valid array of numbers:
Arrays
Each element of an array has an index, which is used to access it.
The index starts from 0, meaning the first element of an array has the index 0, the 2nd element has the index 1, and so on.
Let's output the 3rd element of our array:
var contacts = arrayOf("John", "James", "Amy")
println(contacts[2])
println(contacts[2])
KT
We can also change the value of an element using its index.
For example: contacts[1] = "Dave"
For example: contacts[1] = "Dave"
Arrays
Fill in the blanks to output the first element of the given array.
Chapter: 14
For Loop
for Loops
Oftentimes when working with arrays, there is a need to iterate through all of its elements.
This is easily done using a for loop.
For example:
var nums = arrayOf(2, 4, 6)
for (x in nums) {
println(x)
}
for (x in nums) {
println(x)
}
KT
So, during the first iteration x will be 2, during the 2nd iteration - 4, and during the last one - 6.
You can call the variable of the for loop anything you want.
for Loops
A for loop can also be used to iterate over the characters of a string.
val name = "James"
for (x in name) {
println(x)
}
for (x in name) {
println(x)
}
KT
We can also access certain characters of a string using square brackets:
val name = "James"
println(name[1])
println(name[1])
KT
The code above will output the second character of the string.
for Loops
What is the output of this code? val s = "testing" println(s[6])
Practice:
Vertical Text
You need to make a string as input and output it vertically, each letter on a new line.
Sample Input:
hello
Sample Output:
h
e
l
l
o
Use a for loop to iterate over the letters of the string.
Chapter: 15
Ranges
Kotlin
Kotlin allows you to easily create ranges of values using the following simple syntax:
for (i in 2..5) {
println(i)
}
println(i)
}
KT
You can also create ranges of characters:
for (x in 'a'..'e') {
println(x)
}
println(x)
}
KT
Note that both the starting and ending values are included in the range.
Kotlin
How many numbers does the range 3..6 contain?
Ranges
You can check if a value is present in a range using the in operator.
For example:
val x = 42
if(x in 15..100) {
println("Yes")
}
if(x in 15..100) {
println("Yes")
}
KT
val x = arrayOf(8, 9, 42, 111)
if(42 in x) {
println("Yes")
}
if(42 in x) {
println("Yes")
}
KT
To iterate a number range which does not include its end element, use the until function: for (i in 1 until 5). In this case 5 is excluded.
Chapter: 16
Module 2 Quiz
Fill in the blanks to output "Pass" if the grade variable is above 75, and "Fail", if not.
What is the output of this code? var x = 8 val y = when { x > 10 -> x-1 x > 5 -> x+1 else -> x } print(y+x)
Code Project
Packing Fee
Parking Fee
You are making a car parking software that needs to calculate and output the amount due based on the number of hours the car was parked.
The fee is calculated based on the following price structure:
- the first 5 hours are billed at $1 per hour.
- after that, each hour is billed at $0.5 per hour.
- for each 24 hours, there is a flat fee of $15.
This means, that, for example, if a car parked for 26 hours, the bill should be 15+(2*0.5) = 16.0, because it was parked for 24 hours plus 2 additional hours.
Sample Input:
8
Sample Output:
6.5
Explanation: The first 5 hours are billed at $1/hour, which results in $5. After that, the next 3 hours are billed at $0.5/hour = $1.5.
So, the total would be $5+$1.5 = $6.5
Note: Comment down your answer.
The output should be a Double, even if the amount is a round number.












0 Comments