From the course: Kotlin Essential Training: Functions, Collections, and I/O
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
while loops - Kotlin Tutorial
From the course: Kotlin Essential Training: Functions, Collections, and I/O
while loops
- [Instructor] In nearly any complex application the need will arise to perform some repeated operation. This could be repeating a task a certain number of times or repeating for every item in some range or a collection. Let's explore one mechanism for this type of repeated operation, the while loop. While loops in kotlin behave very similarly to other programming languages. First, we need to define some counter value. So in this case, we'll create a variable called counter and assign it the value of zero to start. Next we set up the looping condition. In this example, we'll set up our loop to run five times by checking whether our counter is less than five or not. So we can do that by using the while keyword and then our condition in this case is going to be, counter less than five, and then within open and close curly braces here we can define what we want to do on each iteration of the loop. If we were to run this code…
Contents
-
-
-
-
-
-
(Locked)
if and when statements12m 55s
-
(Locked)
Statements vs. expressions7m 19s
-
(Locked)
try/catch as control flow6m 25s
-
(Locked)
Smart casting11m 44s
-
(Locked)
while loops5m 17s
-
(Locked)
for loops3m 49s
-
(Locked)
Challenge: Replace an if/else expression with a when expression1m 12s
-
(Locked)
Solution: Replace an if/else expression with a when expression5m 15s
-
(Locked)
-
-
-
-
-