Logic & Loops
In this lesson, you will learn about "Control Flow" in Kotlin. You will learn how to use conditional statements and loops to control the flow of your program.
Conditional Statements
Conditional statements allow you to execute different blocks of code based on certain conditions. In Kotlin, you can use the following conditional statements:
if
statementif-else
statementwhen
statement
if
Statement
The if
statement is used to execute a block of code if a condition is true. The syntax for the if
statement is as
follows:
if (condition) {
// Code to be executed if the condition is true
}
If the condition is true, the code block inside the curly braces {}
will be executed. If the condition is false, the
code block will be skipped.
Example
val number = 10
if (number > 0) {
println("Number is positive")
}
In the example above, we declare a variable number
with a value of 10
. We then use an if
statement to check
if number
is greater than 0
. Since number
is 10
, the condition is true, and the message "Number is positive"
will be printed.
if-else
Statement
The if-else
statement is used to execute one block of code if a condition is true and another block of code if the
condition is false. The syntax for the if-else
statement is as follows:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
If the condition is true, the code block inside the first set of curly braces {}
will be executed. If the condition is
false, the code block inside the second set of curly braces {}
will be executed.
Example
val number = -5
if (number > 0) {
println("Number is positive")
} else {
println("Number is negative")
}
In the example above, we declare a variable number
with a value of -5
. We then use an if-else
statement to check
if number
is greater than 0
. Since number
is -5
, the condition is false, and the message "Number is negative"
will be printed.
You can also chain multiple if-else
statements together to check for multiple conditions.
Example
val number = 0
if (number > 0) {
println("Number is positive")
} else if (number < 0) {
println("Number is negative")
} else {
println("Number is zero")
}
In the example above, we declare a variable number
with a value of 0
. We then use multiple if-else
statements to
check if number
is greater than 0
, less than 0
, or equal to 0
. Since number
is 0
, the message "Number is
zero" will be printed.
when
Statement
The when
statement allows you to execute different blocks of code based on a more complex state than true/false.
this is useful when you have multiple conditions to check, so you don't have to chain multiple if-else
statements.
The syntax for the when
statement is as follows:
when (variable) {
value1 -> {
// Code to be executed if variable equals value1
}
value2 -> {
// Code to be executed if variable equals value2
}
else -> {
// Code to be executed if variable does not equal any of the values
}
}
The when
statement checks the value of the variable
and executes the code block corresponding to the value. If
the variable
does not match any of the values, the code block inside the else
block will be executed.
Example
val number = 10
when (number) {
0 -> println("Number is zero")
in 1..9 -> println("Number is between 1 and 9")
else -> println("Number is 10 or greater")
}
In the example above, we declare a variable number
with a value of 10
. We then use a when
statement to check the
value of number
. If number
is 0
, the message "Number is zero" will be printed. If number
is between 1
and 9
,
the message "Number is between 1 and 9" will be printed. If number
is 10
or greater, the message "Number is 10 or
greater" will be printed.
Loops
Loops allow you to execute a block of code multiple times. In Kotlin, you can use the following loop structures:
for
loopwhile
loopdo-while
loop
But first we need to quickly go over ranges and collections.
Ranges
You can create a range of numbers by specifying the start and end values separated by the ..
operator. The range
includes the start value and the end value.
val range = 1..5 // {1, 2, 3, 4, 5}
You can also create a range that counts down by using the downTo
keyword.
val range = 5 downTo 1 // {5, 4, 3, 2, 1}
Collections
Collections are used to store multiple values. In Kotlin, there are many types of collections, but most of them are for you to learn about on your own. For this lesson we will just be talking about lists
A list is an ordered collection of elements. In Kotlin, you can create a list using the listOf
function.
val numbers = listOf(1, 2, 3, 4, 5)
You can access elements in a list using the index of the element.
println(numbers[0]) // Output: 1
List indices start at 0
, so the first element in the list has an index of 0
.
for
Loop
The for
loop is used to iterate over a range, collection, or any other type of iterable. The syntax for the for
loop
is as follows:
for (element in range) {
// Code to be executed for each element
}
The for
loop will iterate over each element in the range and execute the code block for each element.
the element
variable will be assigned to the current element in the range.
Example
val numbers = listOf(1, 2, 3, 4, 5)
for (number in numbers) {
println(number)
}
In the example above, we create a list of numbers 1, 2, 3, 4, 5
and use a for
loop to iterate over each number and
print it to the console.
You can use a shorthand for the for loop by using the .forEach
function. When doing this you can use the it
keyword
to refer to the current element.
Example
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forEach {
println(it)
}
while
Loop
The while
loop is used to execute a block of code as long as a condition is true. The syntax for the while
loop is
as follows:
while (condition) {
// Code to be executed while the condition is true
}
The while
loop will continue to execute the code block as long as the condition is true.
Example
var number = 5
while (number > 0) {
println(number)
number--
}
In the example above, we declare a variable number
with a value of 5
. We then use a while
loop to print the value
of number
and decrement it by 1
until number
is less than or equal to 0
.
You can also use the do-while
loop, which is similar to the while
loop but will always execute the code block at
least once before checking the condition.
Example
var number = 5
do {
println(number)
number--
} while (number > 0)
In the example above, we declare a variable number
with a value of 5
. We then use a do-while
loop to print the
value of number
and decrement it by 1
until number
is less than or equal to 0
.