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 picks a random number between 1 and 10.
2. Give the user 4 chances to guess the number.
3. If the user guesses the number, display “You won".
4. Otherwise, display “You lost".
1. Write a program that picks a random number between 1 and 10.
2. Give the user 4 chances to guess the number.
3. If the user guesses the number, display “You won".
4. Otherwise, display “You lost".
Here are some helpful hints to accomplish this lesson
1. Refresh your self with your knowledge of what is in the Random Class lesson. (this will allow you to create a Random number)
Click Here to go to the Random class lesson
2. Use an input statement like the one that you used in the Try and Catch lesson, to get numbers from your user. (Refresh your knowledge in the Try and Catch lesson)
Click here to go to the Try and catch lesson. In this lesson we convert a string variable to a byte.
The better variable to use is an int. Here is a line of code to help you convert to int
var guess = Convert.ToInt32(Console.ReadLine());
Click Here to go to the Random class lesson
2. Use an input statement like the one that you used in the Try and Catch lesson, to get numbers from your user. (Refresh your knowledge in the Try and Catch lesson)
Click here to go to the Try and catch lesson. In this lesson we convert a string variable to a byte.
The better variable to use is an int. Here is a line of code to help you convert to int
var guess = Convert.ToInt32(Console.ReadLine());