Looping
A loop is a programming language construction that allows the programmer to instruct the computer to perform a certain instruction, or set of instructions over and over again.
There are many different types, but some are omitted from programming languages by virtue of the fact that they are variations on an existing loop type, and as such the same behavior can be achieved without offering a specific loop type to cover it.
However, we cover the three types which are common to most programming languages:
Condition Tested LoopsOf these, only the condition tested loop is vital; the other two just give a more convenient way of achieving the same effect.
Counted Loops
Endless Loops
For Loop
Program that uses for "int i" loops [C#] using System; class Program { static void Main() {While Loop//// Shows five 'for int i' loops.//Console.WriteLine("--- For 1 ---"); for (int i = 0; i < 10; i++) { Console.WriteLine(i); } Console.WriteLine("--- For 2 ---"); for (int i = 10 - 1; i >= 0; i--) { Console.WriteLine(i); } Console.WriteLine("--- For 3 ---"); for (int i = 0; i < 10; i += 2) { Console.WriteLine(i); } Console.WriteLine("--- For 4 ---"); for (int i = 10 - 1; i >= 0; i -= 2) { Console.WriteLine(i); } Console.WriteLine("--- For 5 ---"); for (int i = 0; i < (20 / 2); i += 2) { Console.WriteLine(i); } } } Output --- For 1 --- 0 1 2 3 4 5 6 7 8 9 --- For 2 --- 9 8 7 6 5 4 3 2 1 0 --- For 3 --- 0 2 4 6 8 --- For 4 --- 9 7 5 3 1 --- For 5 --- 0 2 4 6 8
Program that uses while loop with condition [C#] using System; class Program { static void Main() {Do While Loop// Continue in while loop until index is equal to ten.int i = 0; while (i < 10) { Console.Write("While statement ");// Write the index to the screen.Console.WriteLine(i);// Increment the variable.i++; } } } Output While statement 0 While statement 1 While statement 2 While statement 3 While statement 4 While statement 5 While statement 6 While statement 7 While statement 8 While statement 9
Program that uses do while loop [C#] class Program { static void Main() { int[] ids = new int[] { 6, 7, 8, 10 };Foreach Loop//// Use do while loop to sum numbers in 4-element array.//int sum = 0; int i = 0; do { sum += ids[i]; i++; } while (i < 4); System.Console.WriteLine(sum); } } Output 31
Program that uses foreach over array [C#] using System; class Program { static void Main() {// Use a string array to loop over.string[] ferns = { "Psilotopsida", "Equisetopsida", "Marattiopsida", "Polypodiopsida" };// Loop with the foreach keyword.foreach (string value in ferns) { Console.WriteLine(value); } } } Output Psilotopsida Equisetopsida Marattiopsida Polypodiopsida
If Statement
The if statement in the C# language is a selection statement. It is implemented with intermediate language instructions
called branches. With if, we can make a logical decision based on a parameter or input from a user. Here, we look at if.
This program simply computes the value of an expression and then tests it in an if-statement. The condition inside the if-statement is evaluated
to a boolean value and if the value is true, the inner block is executed.
Program that uses if-statement [C#] using System; class Program { static void Main() {// Call method with embedded if-statement three times.int result1 = Test(0); int result2 = Test(50); int result3 = Test(-1);// Print results.Console.WriteLine(result1); Console.WriteLine(result2); Console.WriteLine(result3); } static int Test(int value) { if (value == 0) { return -1; } else if (value <= 10) { return 0; } else if (value <= 100) { return 1; } else// See note on "else after return"{ return 2; } } } Output -1 1 0