Thursday, October 9, 2014

For Loops (Morning)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoopExamplesMorning
{
    class Program
    {
        static void Main(string[] args)
        {
            int prime=1; 
            ////a basic for has 3 parts
            ////declare a counter
            ////declare the stop point
            ////increment the counter
            for (int i = 1; i < 5; i++)
            {
                //a mathematical oddity that lists 41 prime number
                //it is only here to show you can do things in
                //a loop and you can use the counter
                prime = i * i - i + 41;

                Console.WriteLine("this is loop {0}", prime);

            }//end of for

            //this loop decrements--that is it counts backwards
            //
            for (int i = 10; i >= 0; i--)
            {

                Console.WriteLine(i);
                //Console.ReadKey();

            }//end for

            //this declares a new random object
            Random rand = new Random();
            // +=, *=, /=, -+, %=

            for (int i = 1; i <= 10; i ++)
            {

                //the Next method of random
                //returns a random number
                //you can specify the lower and upper bounds
                int number = rand.Next(1, 5);

                Console.WriteLine(number);
            }

            //while loops are good for looping
            //when you don't know how many loops
            //you want to do. they keep looping
            //until the condition in parenthesis
            //is no longer true
            string keepLooping="yes";
            int counter = 0;

            while(keepLooping.Equals("yes"))
            {
                counter++; //just counting the loops
                Console.WriteLine("this is loop {0}", counter);
                //get user input on whether to continue
                //or quit the loop
                Console.WriteLine("Do you want to continue; yes, no");
                keepLooping = Console.ReadLine(); 
                //put the answer all in lower case
                keepLooping = keepLooping.ToLower();
                //check the answer. We will only exit
                //if it equals "no
                if (!keepLooping.Equals("no"))
                {
                    keepLooping = "yes";
                }

            }

            do
            {
            } while (keepLooping.Equals("yes"));

            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment