The document provides an overview of loops and arrays in C#, explaining different types of loops such as while, do/while, for, and foreach, along with their syntax and use cases. It also covers the concept of arrays, including how to declare them, access their elements, and perform operations like sorting using built-in methods. Lastly, it highlights the use of the System.Linq namespace for advanced array operations like finding minimum, maximum, and summing elements.
Loops
Loops can executea block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
Programming languages provide various control structures that allow for
more complicated execution paths.
A loop statement allows us to execute a statement or group of statements
multiple times and following is the general from of a loop statement in
most of the programming languages:
3.
While Loop
The whileloop loops through a block of code as long as a specified condition is True:
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
while (condition)
{
// code block to be executed
}
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
4.
The Do/While Loop
Thedo/while loop is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax:
Example:
do
{
// code block to be executed
} while (condition);
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);
5.
For Loop
When youknow exactly how many times you want to loop through a block of code, use the for loop instead
of a while loop:
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
Syntax
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Example
Statement 1 is executed (one time) before the execution
of the code block.
Statement 2 defines the condition for executing the code
block.
Statement 3 is executed (every time) after the code
block has been executed.
6.
The foreach Loop
Thereis also a foreach loop, which is used exclusively to loop through elements in an array:
The example outputs all elements in the cars array, using a foreach loop:
foreach (type variableName in arrayName)
{
// code block to be executed
}
Syntax
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
Example
7.
Arrays
Arrays are usedto store multiple values in a single variable, instead of declaring separate variables for each
value. To declare an array, define the variable type with square brackets:
We have now declared a variable that holds an array of strings. To insert values to it, we can use an array
literal - place the values in a comma-separated list, inside curly braces:
To create an array of integers, you could write:
string[] cars;
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
8.
Access the Elementsof an Array
You access an array element by referring to the index number. This statement accesses the value of the first
element in cars:
To change the value of a specific element, refer to the index number:
To find out how many elements an array has, use the Length property:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars[0]); // Outputs Volvo
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]); // Now outputs Opel instead of Volvo
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars.Length); // Outputs 4
Example
9.
Loop Through anArray
You can loop through the array elements with the for loop, and use the Length property to specify how
many times the loop should run. The following example outputs all elements in the cars array:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}
Example
10.
The foreach Loop
Thereis also a foreach loop, which is used exclusively to loop through elements in an array:
The example below can be read like this: for each string element (called i - as in index) in cars, print out the
value of i. If you compare the for loop and foreach loop, you will see that the foreach method is easier to
write, it does not require a counter (using the Length property), and it is more readable.
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
Example
11.
Sort Arrays
There aremany array methods available, for example Sort(), which sorts an array alphabetically or in an
ascending order:
// Sort a string
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Array.Sort(cars);
foreach (string i in cars)
{
Console.WriteLine(i);
}
Example 1
// Sort an int
int[] myNumbers = {5, 1, 8, 9};
Array.Sort(myNumbers);
foreach (int i in myNumbers)
{
Console.WriteLine(i);
}
Example 2
12.
System.Linq Namespace
Other usefularray methods, such as Min, Max, and Sum, can be found in the System.Linq namespace:
using System;
using System.Linq;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int[] myNumbers = {5, 1, 8, 9};
Console.WriteLine(myNumbers.Max()); // returns the largest value
Console.WriteLine(myNumbers.Min()); // returns the smallest value
Console.WriteLine(myNumbers.Sum()); // returns the sum of elements
}
}
}
Example 1
13.
Other Ways toCreate an Array
// Create an array of four elements, and add values later
string[] cars = new string[4];
// Create an array of four elements and add values right away
string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"};
// Create an array of four elements without specifying the size
string[] cars = new string[] {"Volvo", "BMW", "Ford", "Mazda"};
// Create an array of four elements, omitting the new keyword, and without specifying the size
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};