Post #5 Repeated action syntax
Loops: Today we will be talking about loops. I unfortunately do not mean fruit loops. Loops are a repeated action in programs until a condition is completed. If there is no condition restraining a loop and no break in the loop than you have created a forever loop. This will kill most programming and probably crash your computer. If this shall happen than close or exit the program or turn off your computer. There are four loops in C#. These are while, do, for, and foreach.
While Loops: While loops are super simple. They repeat until a condition is false.
Do Loops: Do is similar to while only it can and will go forever. Visual studio and C# are smart of enough to error you out so that this doesn't happen. A do loop will keep going until its while is false. As the previous sentence stated do loops have a while condition. While condition is false it will keep going.
For Loops: For loops are another kind of loop that usually depend on integers. For a variable, while something is false, variable will increase or decrease. That is the base format for a for loop.
Foreach Loops: These are weird. These use lists. Let me just give the format. Foreach (type variable in list){ do something }. I know this seems a little confusing but I think it basically runs through your list and checks for the value given unless you say value because then it goes though everything and does whatever you said. If you wanted it to say something for every 2 in a list of integers, this is the ideal method for how you would do so without having to write your own method like usual.
Now lets give code for loops:
using System;
namespace Repeat
{
class Program
{
static void Main(string[] args)
{
int fred = 0;
do
{
fred++;
Console.WriteLine(fred);
} while (fred < 10);
Console.WriteLine("We have enough Fred");
for(int i=0; i<100; i+=10){
Console.WriteLine(i);
}
foreach(int value in new[] {1, 2, 3}){
Console.WriteLine(value);
}
}
}
}
This program just shows how loops work at a basic level. It creates an int fred. It
goes through a do loop which uses a while condition. The while condition shows how
while loops work. Each time it goes through the loop it adds 1 to fred and prints.
After that loop ends it says we have enough fred. There is a for statement. That
creates i as a variable adding 10 each time after it prints until we hit 100. The
foreach loop you will notice something interesting. If you just say value(int value in
this case) as I did here it will do this for every number, string, or etc. If I had
just said 2, it would only print once because there is one 2. Next thing you might
want to see how I created a list. Nowhere above that did I make a list. I created the
list in the foreach statement. You can create a list there as long as you specify
values. You can of course also use a list created elsewhere in a foreach statement.
This is the end of the explanation on loops.
I used the book The C# Programming Language Fourth Edition By Anders Hejlsberg, Mads
Torgersen, Scott Wiltamuth, and Peter Golde for information in this post.
Resources: Same Resources
MICROSOFT VISUAL C# .NET by Mickey Williams
PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde
PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde
Visual Studio Code Environment:
Link to get started:
Comments
Post a Comment