Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[TUT]Understanding the 'For' Loop[TUT]

Understanding the 'For' Loop


I have seen many members confused on how to use loops. Loops should be the first thing you should learn about any programming language as loops are important in every language. I am writing this tutorial explaining the For Loop. Look for my other tutorials on the other loops.

Step One

Open a new Visual Basic Project. Select Console Application, and name it whatever you want. I named mine "Basic For Loop".
Spoiler (Click to View)

Step Two

You should have a screen now that shows this code:
Spoiler (Click to View)

Don't worry about all of that. We are only going to work in Sub Main for the time being.

Step Three

This is the For Loop code itself:
Spoiler (Click to View)

Code
For i = 1 To 10
    Console.WriteLine(i)
    Console.ReadLine()
  Next i

Code
For i = 1 To 10
- This is the declaration of the For Loop, declaring i is equal to one, and it can be as great as ten.

Code
Console.WriteLine(i)
- This is writing i in the console window (if you started a new console project). Nothing complicated about that.

Code
Console.ReadLine()
- This can best be explained as a pause in the WriteLine. Without the ReadLine the console window would spam the numbers, and then exit before you could even read anything in the console window.

Code
Next i
- This is the actual looping part. Next i means go back to the "For i = 1 to 10" code, and if i is still less than 10, then it will go through all the code again. i will increase by one, be written in the console window, and then paused so you can read it (theoretically). This will happen as long as i has a value less than 10. Once i is equal to 10, the console window will close. End of loop.

Step Four

Voila! You have successfully written, and hopefully understood your Basic For Loop program.

Step Five

Post a reply with what you thought of this tutorial, how well you understood, etcetra. It only takes ten seconds, and I really would love your feedback.

Do Loop: https://hackforums.net/showthread.php?tid=1593501
You should have added "for each" tutorial with this too :P
I am going to a tutorial for all of the loops, so it's no biggy.
There is 3 things that should be in same tutorial always, otherwise people will not know when and when to not use the things.
Personally I find it best to work with for loops, if you have to work with 'keys' in collections or specific positions within an array or whatever.

Well for-, foreach- & while loops should always be in the same tutorial, then use them to compare each other.
I am writing several different tutorials. Writing the Do While Loop right now.
Okay, eXero made me do this over TeamViewer. Yes eXero, I read this earlier. Very good Tutorial for everyone, I wantz moar!
Lol I didn't make you do anything Simran. But thanks for the reply :)
I think you should note that you can have a "counter" that's a floating point... Such as this:
Code
For i As Double = 13.37 To 90.01
    Console.WriteLine(i)
Next i

And also not to forget, the Step argument, like this...
Code
For i As Double = 13.37 To 90.01 Step 3.14
    Console.WriteLine(i)
Next i

And Continue For and Exit For...
Code
For i As Double = 13.37 To 90.01 Step 3.14
    If i = 22.79 Then
        Continue For
    ElseIf i = 69.89 Then
        Exit For
    End If

    Console.WriteLine(i)
Next i

Or I guess no one cares about these.
F# keystroke logger: fialLogger
(Aug 12, 2011 10:46 AM)Yarukinasu Wrote:  
Code
For i As Double = 13.37 To 90.01 Step 3.14
    If i = 22.79 Then
        Continue For
    ElseIf i = 69.89 Then
        Exit For
    End If

    Console.WriteLine(i)
Next i
I care about them a lot, I didn't even know Continue / Exit For even existed. Might help out in the future alot!
[Image: 86251a37e635a21aea7eb7e014b0d820.gif]
I didn't know continue/exit for existed either. Thanks for your reply Yarukinasu.
@eXero, It is a good tutorial. I appreciate your effort but you are missing something. You haven't explained STEP in a FOR...NEXT Loop.
I haven't explained what in a For Next loop? What is STEP?
(Aug 12, 2011 11:20 AM)eXero Wrote:  I haven't explained what in a For Next loop? What is STEP?

Don't you know what is a STEP in a FOR...NEXT loop?

Code
For i = 0 To 10 Step 2
    MsgBox(i)
  Next

Now, this code will first message box 0, then 2, then 4, then 6, then 8 and then 10. It create some type of distance in it.
Bumping my old tutorial in hopes that the noobs will learn something.
Quote:- This is the declaration of the For Loop, declaring i is equal to one, and it can be as great as ten.


No, it can be greater than 10, and if you mean it increments to 10, then for a tutorial to help others understand it's sure unclear. It all depends on the type which i is defined as, and in this case, it's implicitly defined as Integer. So you can go up to the max that a 32 bit signed Integer can go.

How about you use the As clause to define the type which i represents as well to show good habits? Or if you don't know, then put Option Strict On at the top of your code so you can get into good habits as well.

Quote:- This can best be explained as a pause in the WriteLine. Without the ReadLine the console window would spam the numbers, and then exit before you could even read anything in the console window.


"Best" explained as pause? 🤨

http://msdn.microsoft.com/en-us/library/...dline.aspx
MSDN Wrote:Reads the next line of characters from the standard input stream.

I don't think so, it serves a greater main purpose other than "pausing" the console.

Quote:- This is the actual looping part. Next i means go back to the "For i = 1 to 10" code, and if i is still less than 10, then it will go through all the code again. i will increase by one, be written in the console window, and then paused so you can read it (theoretically). This will happen as long as i has a value less than 10. Once i is equal to 10, the console window will close. End of loop.


Wrong, i loops through for these values:

1,2,3,4,5,6,7,8,9, AND 10, because you've specified it to reach 10. It doesn't close at 10, it loops through at 10, then the looping stops and the code after the loop starts.

You haven't even shown how to Step...
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by ohxerxeFeb 4, 2018 03:26 PM 3 306 Feb 4, 2018 03:26 PM
Last Post: matt
[Java] by prodigyhfSep 1, 2017 05:22 PM 0 223 Sep 1, 2017 05:22 PM
Last Post: prodigyhf
[C] by hackaquackAug 1, 2014 03:43 PM 15 351 Aug 1, 2014 03:43 PM
Last Post: Masm4ever