CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2013
    Posts
    2

    [Tutorial] Switch..Case, Loops and 1D Arrays *Heavily Commented*

    I've been learning C# for a while now and I just though this might be helpful to someone that wants to learn about these specific areas of C# although very simple stuff can be hard to understand to someone learning.

    This also demonstrates a int to string work around

    I can do more upon request but if not, I hope this helps someone .

    WITHOUT COMMENTS
    Code:
    class Program
        {
            static void Main(string[] args)
            {
                int[] ints = new int[5] {1, 4 , 11, 19, 22};
    
                string input;
                int inputText;
    
                input = Console.ReadLine();
    
                inputText = int.Parse(input);
    
                switch (inputText)
                {
                    case 1:
                        {
                            for (int i = 0; i < ints.Length; i++)
                            {
                                Console.WriteLine("for Loop ints array {1}: {0}",ints[i] , i);
                            }
                            break;
                        }
                    case 2:
                        {
                            foreach (int i in ints)
                            {
                                Console.WriteLine("foreach Loop ints array: {0}", i);
                            }
                            break;
                        }
                    case 3:
                        {
                            int i = 0;
                            do
                            {
                                Console.WriteLine("do...while Loop ints array: {0}", ints[i]);
                                i++;
                            } while (i < 5);
                            break;
                        }
    
                    case 4:
                        {
                            int i = 0;
                            while (i < 5)
                            {
                                Console.WriteLine("while Loop ints array: {0}", ints[i]);
                                i++;
                            }
                            break;
                        }
                }
                Console.ReadLine();
            }
        }
    WITH COMMENTS
    Code:
    class Program
        {
            static void Main(string[] args)
            {
                int[] ints = new int[5] {1, 4 , 11, 19, 22}; // int array with 5 elements, each element is pre defined.
    
                string input; //input is the string we type into the console before pressing "enter"
                int inputText; // inputText is an int value that we will put the string representation of an int inside this after we parse it
    
                input = Console.ReadLine(); // input now equals whatever we put into the console
    
                inputText = int.Parse(input); // inputText (int) now equals the parsed string (input) as an int32 value
    
                switch (inputText) // switch can called by anything such as string, boolean, char ect.. in this case, it's an int value (inputText
                {
                    case 1: // case 1 is int value of 1, so in the console we type "1" to call this case
                        {
                            for (int i = 0; i < ints.Length; i++) // define i as an int; if i is less-than ints array length; increment i by 1 every pass
                            {
                                Console.WriteLine("for Loop ints array {1}: {0}",ints[i] , i); // write to console every pass with ints array i value followed by i value
                            }
                            break; // break out of the switch..case and run the rest of the code, you can use goto statement here too
                        }
                    case 2: // case can also be a string or char or boolean so it could be "case "foreach": but the switch input needs to be set to string
                        {
                            foreach (int i in ints) // for each integar in ints array, there are 5 elements in the array with 5 integars of [1], [4], [11], [19], [22]
                            {
                                Console.WriteLine("foreach Loop ints array: {0}", i); // print the value of i each pass, first pass will be 1, second 4.. ect..
                            }
                            break; // again, break out of switch..case.
                        }
                    case 3: 
                        {
                            int i = 0; // define i equals 0
                            do // do the bottom statement
                            {
                                Console.WriteLine("do...while Loop ints array: {0}", ints[i]); // print the value of ints array using the value of i to find the array element
                                i++; // increment (add) by 1
                            } while (i < 5); // do the statement WHILE i is less than 5 (5 elements in the array)
                            break; // :)
                        }
    
                    case 4:
                        {
                            int i = 0; // i equals 0 again, each i in each case is destroyed after each use and only created while in use so you can use int i all you like
                            while (i < 5) // while i is less than 5 do the bottom statement.
                            {
                                Console.WriteLine("while Loop ints array: {0}", ints[i]); // same as do..while (case 3)
                                i++; // increment i by 1 each pass
                            }
                            break;
                        }
                }
                Console.ReadLine(); // finally readline() just so we can see the result of the case we chose so console don't close instantly!
            }
        }

  2. #2
    Join Date
    May 2013
    Posts
    2

    Re: [Tutorial] Switch..Case, Loops and 1D Arrays *Heavily Commented*

    I forgot to mention I'm using Visual Studio 2012 with .NET 4.5.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [Tutorial] Switch..Case, Loops and 1D Arrays *Heavily Commented*

    Thanks, but the forums aren't the place to post tutorials.

    Please submit this as an article to the main codeguru site.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured