### arrays

#### Need someone to explain this code [closed]

```I have been trying to understand this:
var arrayOfLabels =
[
"Hello",
"Hey",
"Hi",
"Howdy"
]
#IBOutlet weak var labelHere: UILabel!
var currentElementIndex = 0
#IBAction func clickForNextElement(_ sender: UIButton) {
currentElementIndex += 1
let numberOfElements = arrayOfLabels.count // arrayOfLabels.count = Amount of elements in arrayOfLabels
let nextElement = currentElementIndex % numberOfElements
labelHere.text = arrayOfLabels[nextElement]
}
I don't get what % does, what the constant "numberOfElements" & "nextElement" does.. I do get a little bit, like i would be able to code something similar to this without understanding it. That's why i need some simple, detailed explanation.
Thank you!
```
```numberOfElements is set to the number of items in the arrayOfLabels. You have five items in that array so numberOfElements is set to 5.
The percent sign "%" is called the modulo operator. What it does is tells you the remainder after integer division. So, for example, if you write 5 % 3 what that does is recognizes that 3 will go into 5 only once with a remainder of 2 so 5 % 3 = 2.
In the context of the code currentElementIndex gets increased by one every time you push the button. So it starts at 0, then goes to 1, 2 etc.
When currentElementIndex is less than numberOfElements then currentElementIndex % numberOfElements will be the same as currentElementIndex. You can check that yourself - for example try when currentElementIndex is 2. 2 % 5 says "five goes into two zero times with a remainder of 2" so 2 % 5 == 2 and nextElement will be 2
When currentElementIndex grows large enough to be equal to a multiple of the number of items in the list, you want it to start over. The first time that happens is when currentElementIndex == numberOfElements the values would be 5 and 5. 5 % 5 says "5 goes into 5 one time with a remainder of 0". So 5 % 5 = 0 and your 'nextElement` becomes zero.
The second time it happens would be when currentElementIndex grows to be 10. In that case 10 % 5 say "five goes into 10 twice with a remainder of zero" Again nextElement rolls back to zero.
Try putting print statements into the code and watch how both currentElemetnIndex and nextElement change when you push the buttons/
```
```Here's a commented version of the code:
// create an array of labels
var arrayOfLabels =
[
"Hello",
"Hey",
"Hi",
"Howdy"
]
// a variable for the label
#IBOutlet weak var labelHere: UILabel!
// initialise a count
var currentElementIndex = 0
// create a function
#IBAction func clickForNextElement(_ sender: UIButton) {
// Add one to our count
currentElementIndex += 1
// set numberOfElements to be how many elements are in the array (i.e. 4)
let numberOfElements = arrayOfLabels.count // arrayOfLabels.count = Amount of elements in arrayOfLabels
// use the % to work out remainder when dividing our ongoing count by the number of elements
let nextElement = currentElementIndex % numberOfElements
// set the label text to the value of the remainder-th element
labelHere.text = arrayOfLabels[nextElement]
}```

### Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization