Nimrod Programming/Loops

LoopingsEdit

So, next thing we are going to see are loops. What are loops? You might ask. Well, it's simple, it's a repetition of instructions, repeated as long as a certain condition is met or inside a set of values, that sounded way too complicated ... put simply it will start to repeat a series of instructions (e.g:variables creation and assignation, ifs, function, more loops ...) indicated by you (the programmer), when it will stop it's a matter of which type of loop we use.

In Nim there are two types of loops, we call them for and while:

WhileEdit

We'll start with the easier one. As you may guess, while will be repeating until a certain condition is met. Let's see how it will look. var languageName = "Nim" while languageName == "Nim":

   echo "Hey! I'm using Nim!"

Now, you should try to guess what this will do, don't worry take your time, I'll be waiting here.

Done? You might have ended up reading again without really knowing what this does , then, don't worry it might be difficult, however, if you got it CONGRATULATIONS!!!!! Now, what this does is to repeat forever "Hey! I'm using Nim!", why? Because we set language languageName (remember with var we create a variable and with one equal sign '=' we set a value to it ) and then, we check it's equal to "Nim" (as mentioned in the if part, == it's for checking whether both sides are equal ) and execute echo "Hey! I'm using Nim!" forever given that there's no single line that changes it. Now, if for any reason we went and changed var languageName = "Nim" to anything else , like var languageName = "My name's Ralph", the code inside the while wouldn't execute, not even once, because languageName == "Nim" will be false and the CPU will skip that part.

ForEdit

We already know while, now we are going to meet for.In a nutshell,what it means is "for every element in this set, follow these instructions", as easy as that. But what kind of good book, we would be if we didn't show an example? for a in 0..10: for a in 0..10:echo "this is ", a, " step"

We'll play the guess game again, you know the rules right? Now, let's get started.

Done? Nice, what this does is that it will print "Hey!I'm the number 0" and will print the same for every number in the 0 to 10 sequence, how is this possible? Thanks to & what it does is to take two string and join them together, in other words, it concatenates them (note: you may remove the & and it still would work, due to the fact echo will print every argument, and if the & is removed and the space is kept it will detect those as separate arguments).

Well, we are finished with loops, if understood everything, nice if not ,try to read it once again , those are basic concepts and should not be very difficult to understand.