Results 1 to 5 of 5
-
11-13-2010 #1
Basic C# Tutorials: Iteration[Part 1]
Today I am going to be covering iteration. It is kind of hard for me to explain exactly so here is a definition:
If you don't understand that then just read the first sentence. It is basically doing something over and over until you gaet what you want. In programming there are a few kinds of iteration statements. For loops, while loops, do loops, and foreach loops. I will try and go over all of them. Also in loops there are iteration variables which are the temporary variables that are being changed for each iteration. Ok. Now I will start explaining the loops.
Originally Posted by Wikipedia
For Loop:
A for loop is a loop that goes until some kind of number reaches a preset max value. It can be used to search for something in a file. And the preset value would be the size of the file. Then the loop can go byte by byte over the file and add all instances of what was searched for and where it was found to a list. They are incredibly useful. Here is the basic syntax of one:
BreakDown: the first statement int i = 0; creates the initial number to count to. The next statement i <= 10 is the check statement. Remember decision statements! That is kind of like an if then. If i is not less than or equal to ten then it will evaluate to false and the loop will terminate. Then the last statement i++ adds 1 to i if the previous statement is true. It can also be i-- when you are starting the iteration from a higher value. Here is an example of that:Code:for (int i = 0; i <= 10; i++) { //writes the number that the for loop is currently at to the console Console.WriteLine(i); } Console.ReadLine();
That will do what the first one did in reverse. This one counts down from 10 to 0. I hope you understood that. Next I will talk about the while loop.Code:for (int i = 10; i >= 0; i--) { Console.WriteLine(i); } Console.ReadLine();
What the while loop does is it has a single statement inside its parentheses. If that statement evaluates to false then the loop terminates but if not then it continues. Even simpler than the for loop. Here is a a basic example of how a while loop works. It does not have to be exactly like this but this is how I like to do it.
Its pretty basic so I am not going to explain more. Post if you want to know more about it. The reason I am not explaining more is because this is not a book and if you really want to learn a ton then go to a bookstore and buy a book. If you want recommendations then PM me.Code:bool condition = false; while (!condition) { if (checkStatementHere) { condition = true; //loop terminates } else { //loop Continues } }
The last loop I will talk about is the foreach loop. The name is pretty verbatim. For each item in a collection it will iterate with statements for that specific item. I use it a ton when using X360 and FATx so I can do statements for each partions in all the partions. Here is an example. I will comment this a little more because it is a little more advanced:
Post if you want to know anymore. You can also use generics and have a generic list of strings. I will go into those in the future.Code://the following variable is an array of strings. each quotation set in it //is a seperate string. string[] texts = new string[] { "Hello ", "This ", "Is ", "A ", "Test" }; //the following loop will iterate through it with the variable x holding the //temporary string that is currently being used. foreach (string x in texts) { //the following will write the strings to the console Console.WriteLine(x); }I see you read my signature.
-
The Following User Says Thank You to Matt For This Useful Post:
C4D3N (11-13-2010)
-
11-13-2010 #2
Re: Basic C# Tutorials: Iteration[Part 1]
I hope this gets STICKIED! Good contribution. =D
I will answer any and all questions regarding my big toe.
-
The Following User Says Thank You to Skull Kid For This Useful Post:
Matt (11-13-2010)
-
11-13-2010 #3
Re: Basic C# Tutorials: Iteration[Part 1]
Thanks. :) I will make an index later with links to all the lessons. The first one was. I will tomorrow because I just got home from the gym and am ready to just chill and play black-ops. :)
I see you read my signature.
-
11-13-2010 #4
-
11-13-2010 #5
Re: Basic C# Tutorials: Iteration[Part 1]
Lol. Thanks. You were the inspiration for starting them. :D And I am not sure on what the next one will be. I am thinking either string manipulation or generics. String manipulation, I am just going to going to talk about modifying them, reversing them and some other stuff. And with generics it is kind of boring but very important. It will be about the List<Type> and the type in that is the type of variable you would like the list to be of. I think string manipulation but IDK. Lol.
I see you read my signature.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)



LinkBack URL
About LinkBacks


Reply With Quote




Bookmarks