From the course: Programming for Non-Programmers: Android & Kotlin

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using loops

Using loops

From the course: Programming for Non-Programmers: Android & Kotlin

Start my 1-month free trial

Using loops

- [Instructor] In our calculator app, we're going to use a for loop to connect all of the numeric buttons to the same method. So to practice doing that, let's use a for loop here. I'm working with an example from a previous movie for those of you who are following along without the exercise files. So you can grab that previous exercise where you worked with arrays and just start from there. So here I have my buttons array and all of the OnClickListeners set for each item in the array. So let's set these with a loop instead of setting them manually. So I'm going to comment out these three lines of code and then I'm going to create a for loop below that. So for, and then I'll just press Return to use the code hinting, which only gives us parentheses, but it can still save you a little bit of time. Inside of the parentheses, type i and then in and then space buttons.indices. Now, if you're wondering about i, i is not something you have to use. You can name this anything you want…

Contents