September 20, 2017
C# examples of if-else, switch statement, nested case, nested for loop, while loop, jump statements, string arrays, array searching
C# examples of control structures, arrays, and searching
////////////// Nested IF - Else example using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("Type in a string"); string input; input = Console.ReadLine(); if (input == "") { Console.WriteLine("You typed in an empty string"); } else if (input.Length < 5) { Console.WriteLine("The string had less than 5 characters."); } else if (input.Length < 10) { Console.WriteLine("The string had at least 5 but less than 10 characters"); Console.WriteLine("the string was " + input); } } } } ============================= END OF PROGRAM ================ ///////////// SWITCH STATEMENT EXAMPLE ///////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("enter an integer: "); string input = Console.ReadLine(); int num; if (int.TryParse(input, out num) == false) { Console.WriteLine("incorrect input"); return; } switch (num) { case 1: Console.WriteLine("integer = 1"); break; case 2: Console.WriteLine("integer = 2"); break; default: Console.WriteLine("Some other value entered"); break; } } } } ================= END OF PROGRAM ============ //////////////// NESTED CASE ///////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("enter a word: "); string input = Console.ReadLine(); switch (input) { case "one": case "two": Console.WriteLine("this is first type"); break; case "three": Console.WriteLine("this is three"); break; default: Console.WriteLine("Some other value entered"); break; } } } } =================== END OF PROGRAM ================================= ///////////////// NESTED FOR LOOP EXAMPLE ////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { for (int i = 0; i < 100; i+=10) { for (int j = i; j < i + 10; j++) { Console.Write(" " + j); } Console.WriteLine(); } } } } ====================== END OF PROGRAM ========================== //////////// WHILE LOOP EXAMPLE //////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int i = 10; while (i > 0) { Console.WriteLine(i--); } } } } ==================== END OF PROGRAM ===================================================== ////////// LOOPS EXAMPLE /////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] strarray = { "one", "two", "three", "four", "five" }; foreach (string s in strarray) { Console.WriteLine(s); } for (int i = 0; i < strarray.Length; i++) { Console.WriteLine(strarray[i]); } int a = 0; while (a < strarray.Length) { Console.WriteLine(strarray[a++]); } } } } =================== END OF PROGRAM ================================ //////// JUMP STATEMENTS --- EXAMPLE OF CONTINUE ///////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] strarray = { "one", "two", "three", "four", "five" }; foreach (string s in strarray) { if (s == "three") continue; Console.WriteLine(s); } } } } ==============END OF PROGRAM =================== ////////////// SEARCHING A STRING //////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { bool found = false; Console.WriteLine("Enter a string to search: "); string input = Console.ReadLine(); string[] strarray = { "one", "two", "three", "four", "five" }; foreach (string s in strarray) { if (s == input) { found = true; break; } } if (found == true) { Console.WriteLine("item found"); } else { Console.WriteLine("Not found"); } } } } ========== END OF PROGRAM ============= ///////////// EXAMPLE TO SEARCH WITHIN AN INTEGER ARRAY ////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { bool found = false; Console.WriteLine("Enter a number to search: "); int input = Convert.ToInt32(Console.ReadLine()); int[] intarray = {10,20,30,40,50 }; foreach (int s in intarray) { if (s == input) { found = true; break; } } if (found == true) { Console.WriteLine("item found"); } else { Console.WriteLine("Not found"); } } } } //=========== END OF PROGRAM ======================== //////////////// Program that takes as input an array of 5 numbers from user.///////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] intarray; intarray = new int[5]; for (int i = 0; i < 5; i++) intarray[i] = Convert.ToInt32(Console.ReadLine()); foreach(int x in intarray) Console.WriteLine(x); } } } //======================= END OF PROGRAM ======================================