# Rust for the Novice Programmer/Loops and Iteration
## Loops and iteration

Let's say we wanted to go through all the numbers from 1 to 100 and print out all the ones which are divisible by 6. How would we do this? Well, we could write a function checking if a number is divisible by 6 so let's do that first. By separating out the things we need to do we can deal with the problem one step at a time, which makes it easier to reason about. So first let's write the shell of the function:
We use the comment here to indicate we're going to fill out the inside of the function. This won't compile currently since the function must return a boolean value of either true or false and at the moment it returns nothing. How do we check if a number is divisible by six? A quick refresher, being divisible by 6 means if it is divided by 6, the result will be a whole number. However, the input type is i32 so dividing by 6 will always return a whole number since it is doing integer division. So how do we proceed? One way would be to constantly subtract 6 from the number until we get to a number less than 6. If that number is 0, it means the number is divisible by 6, if not then it is not. Then the question is how do we express the 'constantly' 'until' in that logic. The answer is the while loop. A while loop while continually run whatever is inside the braces as long as the boolean value(called the condition) is true.
This is a direct translation of the steps earlier into code. Note that we could write it as 6 < number which is the same but by convention we normally put the variable on the left of conditions since it is more natural to read and understand. What we are doing here is finding the remainder of input. This is quite a common thing, so there is a built-in way to do this, using the percent sign %. So a % b gets the remainder of a divided by b. Therefore, it is much simpler to write the function as this:
We can simplify this further since remainder == 0 is already a boolean value that corresponds to what we want to return, so we can do:
This is much better. The morale of the remainder operator is if you're doing something quite common, there's probably a simple way to do it and the best way to find these ways is generally to search.
Back to the original thing, which was to go through all the numbers from 1 to 100 and print them if they're divisible by 6. Now we can do this using a while loop, we just have to set a number to 1, then keep going through the loop while the number is less than or equal to 100.
This works, but there is a built in easy way to iterate through numbers. These are called for loops, and we would rewrite the above as following:
This does the same result as the while loop above but is a little cleaner. The 1..101 is called a range and it ranges from 1 to 100. The reason it has to be 101 on the right side of the .. is that it is non inclusive, meaning it doesn't include the last number. The reason for this will be seen later. We can use an inclusive range with ..= so 1..=100 would yield the same result. The 'for number in' indicates a variable called number that stores the value as the for loop goes through the values in the range. A for loop can iterate through anything that is an 'iterator' which will be explained soon.
Next: Arrays and Vectors