Tuesday, October 18, 2016

Loops

For loops

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

namespace ForLoopExcercise
{

    class Program
    {
        /*This program will explore
         * repetition with for loops
         * for loops are best for counted loops
         * where you know how many loops 
         * you want to do.
         *Steve Conger 10/18/2016
         */


        static void Main(string[] args)
        {
            int number = 0;
            long number2 = 0;
            Console.WriteLine("How many times do you want to loop?");
            number = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the number to get exponents");
            number2 = long.Parse(Console.ReadLine());
            Console.WriteLine();

            for (int i = 1; i <= number; i++)
            {
                number2 *= number2; // number=number * number
                Console.WriteLine(number2);
            }

            Console.WriteLine();
            for(int i=0;i<=10;i++)
            {
                Console.WriteLine(i);
                  
            }

            for (int i = 0; i <= 10; i+=2)
            {
                Console.WriteLine(i);
            }

            //infint loop
            //for (int i = 1; i > 0; i++)
            //{
            //    Console.WriteLine(i);
            //}

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

While Loops

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

namespace WhileLoopExample
{
    class Program
    {
        /*
         * This program shows while loops
         * the first provides a menu of 
         * opens and loops until the user 
         * chooses 4 for exit.
         * Within the loop is another loop
         * that loops until the user enters
         * a valid integer menu choice
         */
        static void Main(string[] args)
        {
            int choice = 1;
            //loop until they choose 4 to exit
            while (choice != 4)
            {
                Console.WriteLine("Hello:\t\t1");
                Console.WriteLine("Hello:\t\t2");
                Console.WriteLine("Hello3:\t\t3");
                Console.WriteLine("Goodbye:\t4");


                bool goodChoice = false;

                //loop until they enter a valid integer
                while (!goodChoice)
                {
                    Console.WriteLine("Enter your choice");
                    goodChoice = int.TryParse(Console.ReadLine(), out choice);

                    if(!goodChoice)
                    {
                        Console.WriteLine("Enter an integer 1 to 4");
                    }
                    
                }

                switch(choice)
                {
                    case 1:
                        Console.WriteLine("Hello One");
                        break;
                    case 2:
                        Console.WriteLine("Hello two");
                        break;
                    case 3:
                        Console.WriteLine("Hello three");
                        break;
                    case 4:
                        Console.WriteLine("Goodbye");       
                        break;
                    default:
                        Console.WriteLine("Invalid choice");
                        break;
                }//end switch
            
            }//end loop

            /*
             * do loops do the same thing as
             * while loops with one difference
             * they test the condition at the 
             * end of the loop. That means that
             * they always run at least once.
             * whereas a while loop may never 
             * run if the initial condition is false
             */
            string continues= "yes";
            do
            {
                Console.WriteLine("continue yes/no");
                continues = Console.ReadLine();
                continues = continues.ToLower();
            }while(continues.Equals("yes"));

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }//end main
    }//end class
}//end namespace

No comments:

Post a Comment