using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LoopExamples { class Program { static void Main(string[] args) { //random is a built in object //it is not static so we have to make it new //making it new loads it into memory //and runs the objects construction Random rand = new Random(); //basic for loop (three parts) //declare a counter variable and give it an initial value //give it a terminal value //increment the counter for(int i=0;i<10;i++) { //this returns a random number //between 1 and 500 int number = rand.Next(1, 501); Console.WriteLine(number); } for (int i = 10; i >=0; i--) //decrements { int number = rand.Next(1, 501); Console.WriteLine(number); } Console.WriteLine("***********************"); int number2 = 3; for (int i = 0; i < 10;i++ ) { //same as number2 = number2 + i; //other shorcut operators // +=, -=, *=, /= number2 += i; Console.WriteLine(number2); } //can use a variable for the end condition Console.WriteLine("How many loops?"); int numberOfLoops = int.Parse(Console.ReadLine()); for (int i = 1; i <= numberOfLoops; i++) { //same as number2 = number2 + i; // +=, -=, *=, /= number2 *= i; Console.WriteLine(number2); } // a while loop loops until the condition //is no longer true string go = "yes"; int counter = 0; while (go.Equals("yes") || go.Equals("Yes")) { counter++; Console.WriteLine("You have done {0} loops", counter); Console.WriteLine("Do you want to do another: Yes or no?"); go = Console.ReadLine(); } // a do loop evaluates the condition //at the end--guarantees at least one //loop occurs do { }while (go.Equals("Yes")); Console.ReadKey(); } } }
Wednesday, October 8, 2014
Loops(Evening Class)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment