For Loop
Every loops has a number pf the same basic elements in it.
1. A beginning
2. An important block of code that needs to be run.
3. A testing condition or statement that judges whether or not to exit the loop.
4. An end
A For loop changes the recipe from the While loop slightly. In the beginning of the For loop we initialize everything thing. I love this structure because it allows me to make sure that I am getting everything in the loop at the beginning so that I don't forget a piece later. It then runs along and treats the block of code as it own element.
1. A beginning
2. An important block of code that needs to be run.
3. A testing condition or statement that judges whether or not to exit the loop.
4. An end
A For loop changes the recipe from the While loop slightly. In the beginning of the For loop we initialize everything thing. I love this structure because it allows me to make sure that I am getting everything in the loop at the beginning so that I don't forget a piece later. It then runs along and treats the block of code as it own element.
Here is what the for loop looks like in code:
using System;
class forLoopDemo
{
public static void Main()
{
for (int x = 1; x <= 4; x++)
Console.WriteLine("DigitalfxtBook.com is amazing");
}
}
using System;
class forLoopDemo
{
public static void Main()
{
for (int x = 1; x <= 4; x++)
Console.WriteLine("DigitalfxtBook.com is amazing");
}
}
Explanation of the code
So lets look at exactly what this code is doing:
1. We create our organization with the Class (forLoopDemo) and the Method (public static void Main())
2. We then initialize the loop with three statements
1. int x = 1 (this is our counter)
2. x <= 4 (this is our condition testing to see if our loop has run for at least 4 times)
3. x++ (this increments our counter up by one)
Soooo in essence this one statement gets everything set up.
The establishment of our counter only happens once.
Every time the loop cycles through the test is run to make sure that the loop is going to happen one more time.
At the end of the loop the counter gets incremented.
3. Now that the loop has been initialized we can run the important block of code inside of it.
1. We create our organization with the Class (forLoopDemo) and the Method (public static void Main())
2. We then initialize the loop with three statements
1. int x = 1 (this is our counter)
2. x <= 4 (this is our condition testing to see if our loop has run for at least 4 times)
3. x++ (this increments our counter up by one)
Soooo in essence this one statement gets everything set up.
The establishment of our counter only happens once.
Every time the loop cycles through the test is run to make sure that the loop is going to happen one more time.
At the end of the loop the counter gets incremented.
3. Now that the loop has been initialized we can run the important block of code inside of it.
Assignment
So in this lesson what you are going to create is:
1. Write a program that prints out ever odd number between 0 and 30.
1. Write a program that prints out ever odd number between 0 and 30.
Here are some helpful hints to accomplish this lesson
1. In the video above you were supposed to print out every even number
2. we need it to print out every number not even....
2. we need it to print out every number not even....