![]() |
|
[.NET] [TUT]Understanding the 'For' Loop[TUT] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] [TUT]Understanding the 'For' Loop[TUT] (/showthread.php?tid=1593443) Pages: Pages
1
2
|
[TUT]Understanding the 'For' Loop[TUT] - eXero - Aug 12, 2011 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 10Code For i = 1 To 10Code Console.WriteLine(i)Code Console.ReadLine()Code Next iStep 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 * - Deceptive - Aug 12, 2011 You should have added "for each" tutorial with this too :P * - eXero - Aug 12, 2011 I am going to a tutorial for all of the loops, so it's no biggy. * - BaussHacker - Aug 12, 2011 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. * - eXero - Aug 12, 2011 I am writing several different tutorials. Writing the Do While Loop right now. * - IllBeBackSoon - Aug 12, 2011 Okay, eXero made me do this over TeamViewer. Yes eXero, I read this earlier. Very good Tutorial for everyone, I wantz moar! * - eXero - Aug 12, 2011 Lol I didn't make you do anything Simran. But thanks for the reply :) * - Yarukinasu - Aug 12, 2011 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.01And also not to forget, the Step argument, like this... Code For i As Double = 13.37 To 90.01 Step 3.14And Continue For and Exit For... Code For i As Double = 13.37 To 90.01 Step 3.14Or I guess no one cares about these. * - Tedd - Aug 12, 2011 (Aug 12, 2011 10:46 AM)Yarukinasu Wrote:I care about them a lot, I didn't even know Continue / Exit For even existed. Might help out in the future alot! * - eXero - Aug 12, 2011 I didn't know continue/exit for existed either. Thanks for your reply Yarukinasu. * - hav0c1995 - Aug 12, 2011 @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. * - eXero - Aug 12, 2011 I haven't explained what in a For Next loop? What is STEP? * - hav0c1995 - Aug 12, 2011 (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 2Now, 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. * - eXero - Aug 19, 2012 Bumping my old tutorial in hopes that the noobs will learn something. * - 0xDEAD10CC - Aug 19, 2012 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/system.console.readline.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... |