VB.NET for Loop: Four-loop in Microsoft Visual Studios

vb.net for loopToday, we are going to discuss the concept of a four-loop.  Four-loop is basically another way of looping through code or blocks of code.

Master the powerful combination of VB.NET and Visual Studios with this course today!

Starting a New Project

1.   First, click on ‘Visual Studios’

2.   Create a new project here using the ‘Windows Console Applications’, and click ‘okay’.  Let’s take a look at this construct here.  consol3.jpg

Make a code here, and if you already have one made, paste it down below.code1.jpg

3.   Here is the syntax that you will follow for X as an integer.  This value is equal to 1 and will go up to, say, 100, and step by 1’s.  This means you will begin at 1, and then you will go to 2, to 3, to 4, and so on.  So the starting value is 1, the stopping value is 100, and you are stepping by 1’s.  This is basically 1 to 2, 2 to 3, 3 to 4, and so on.

Example

Let’s move on to the next one.  Here, we are going to do something similar.

1.   Let’s say that that number divided by 2, for instance, is 0 – then hit enter – and we are going to check if a number is divisible by 2, whether it is even.

div2.png

2.   Now, type [console.rightline], and write the number, and then put a place holder – that means the 0 between the braces – is divisible by 2.  Then close with the– put the X in.

3.   So this will be the X into the place holder – run time.  Okay, so here outside of the next, type [console.reed]. See that? There you go!

You now have a four- loop which consists essentially of a variable declaration – starting value, stopping value, and step-size.  In other words, this is the jump between consecutive values.

Step Into

Make sure the code works as expected so it is generated console application 254.exe.

1.   Lets pin the console window over visual studio.  At this point, you will notice that the cursor is on sub-main.

2.   Let’s hit ‘step into’. So this line is about to be executed, so hit ‘step into’.  As you can see, X has not been created; it is a local variable.  This means local to the current routine only.  It is like an address confined to the neighborhood called ‘sub-main’.  The value is 1 and the type of the variable is integer.

3.   Now, hit ‘step-into’. It checks the condition, which is if the number is divisible by 2: X mod 2 means divisible by 2.  If for some reason this is not the case, then the remainder is not 0.

4.   Therefore, it skips printing, and it goes on to the end of the [F] block.  Then it goes down to the next.  Now take a look very carefully at what happens.  It will say if X mod 2 equals 0 then, that condition does hold, and it goes back.  4 is divisible by 2.  Here you should really execute this code literally line by line, because it can be so subtle, and there are so many things happening.

5.   Now over here on ‘next’, as you can see, it highlights 4 and 2 and steps by 1’s.  Then, you will keep going.

Next, the current value of X is 25; so is 25 divisible by 2?  It is not.  This means this line should get skipped, and so on.  Hopefully you see that the process of a four-loop allows you to iterate through code, and in this case you are beginning X as an integer of value 1.  It is going to go up to 100 and stepping up by 1’s.  That is it!  If you want to, quickly shut this down you can change the step size.  Or, you can do this: you can begin at 100.  If you begin at 100, you can go down to a 2, and the step size can be something negative like a -10.  Now, let’s go down to a 0.  It really does not matter what you choose—you can do whatever you want!

Building the Code

Now that we have gotten to this point, let’s build this code to make sure it works.

1.   De-bug ‘step-into’.  Let’s pin this over visual studios so we can see the output of each step. So now, hit ‘step-into’.  It tells you the value of X is 100 now.  It checks the condition – if that number is divisible by 2.  The number 100 is divisible by 2, which is correct.

2.   Notice the value of X now is 90, so this increment, this step size of -10 is bringing us down this time. Here you can keep going, keep going.  Make sure you execute this code super slowly.  You are literally: checking, printing, ending the blog, going to the next keyword; checking, printing, ending the blog, going to the next key word. Checking– and hitting ‘next’.

3.   And now, at the last step, you will see that it did not go back to the top of the loop because we were down to the 0 here.  It will just exit the loop and that is it.

Remember the Loops!

Remember the four-loop works like this: you have got a variable initialized into a value, specify the stopping value, and specify a step size.  And, within the four-loop, you can specify other loops if you have to.  If not, you can display some code or maybe some lines of writing.  Obviously things can get a little tricky if you do not have much prior experience with VB.net for four-loop.  This course will show you all you need to know to get you into the loop of this problem!