Lesson 9: Learning to work with loops in #C

Lesson 9: Learning to work with loops in #C. Learn to work with Loops loop iteration structures in #C Another major technique when writing software is the ability to create loops grammatically.

This type of command allows you to repeat blocks of code as many times as you want.

For example we want A set of commands is repeated until the value of an example variable is less than 11, in which case the loop must be To use in C #.

In the C # language, we have four main types of loops, each of which we will describe below with practical examples:

: While loop loop


While loop is the easiest type of loop in C # language and for this reason learn loops from this loop we start. Loop While loop sets the set of commands set for it until the condition is set Is true for the correct loop, executes. The following code shows a simple example of using the While loop In the following, we will explain the code more:

using System;
 namespace ConsoleApplication1
 {
 class Program
 {
 static void Main(string[] args)
 {
 int number = 0;
21
 while (number < 5)
 {
 Console.WriteLine(number);
 number = number + 1;
 }
 Console.ReadLine();
 }
 }
 }

Run the program. A list of numbers from 1 to 4 will be displayed for you. The number variable that actually The While loop counter is also an example, starting with the number 1 and every time the loop commands are executed once, Its value increases by one unit.

As long as the value of the variable number is less than 5 and not 5 itself, the execution of the commands will continue until the number is reached number 5, loop execution stops and the program jumps to the code line after the While loop,

Ring
The function of the loop do loop is slightly different from the While loop loop.

In the loop do loop, first run the loop body commands
Runs once and the loop condition is checked at the end. If the condition is true, repeat the loop commands again
Becomes.

The important thing about a loop do loop is that even if the loop condition is not true from the beginning, the loop statements Run at least once. The following code shows a practical example of working with a loop do loop:

do
22
{
 Console.WriteLine(number);
 number = number + 1;
} while(number < 5);

The loop do output of the above example prints the numbers 1 to 5 and stops when the loop number reaches 6.

Be.

: for loop ring The loop for mechanism is slightly different from previous loops. This ring is best used when . We know approximately how many times the loop may be repeated.

The loop for loop has a number as a counter Each time the loop is executed, its value decreases or increases by the specified unit.

Run the loop until the condition It is correct, continued, and the condition is tested at the beginning of the loop run. The following code is a practical example of a for loop Shows the loop:

using System;
 namespace ConsoleApplication1
 {
 class Program
 {
 static void Main(string[] args)
 {
 int number = 5;
 for (int i = 0; i < number; i++)
 Console.WriteLine(i);
28
 Console.ReadLine();
 }
 }
 }

The above example code generates output like a While loop, but as you can see, this loop is a bit
It is denser than the previous rings.

The for loop consists of three main parts:
First we define and set a variable value to count the execution of the loop, a condition for the execution of the loop The basis of the counter variable is specified and an increase or decrease step to count the execution of the loop and control its step, such as ++ or – is specified.

In the example code above, in the first step we have defined the ring counter as i and set it to 1, this section The loop runs only once when the loop starts.

The next two parts of the loop are executed each time the loop is repeated. Each time, the value of variable i with numeric variables number is compared and if it is smaller, the loop commands are executed again and the value of i is one The unit increases.

Important Note: If you do not specify an increase or decrease step for the loop variable, if the initial condition of the loop is true, The for loop runs indefinitely.

Tip 2: The step of increasing or decreasing the loop counter instead of the front parentheses for, is also inside the body of the loop commands.
Can be placed.

29

: foreach loop ring
The last loop we will look at in this section is the loop for eachloop. From this ring usually when working Used with a set of items such as arrays or list variables.

In the example ring code foreach We use a list variable called ArrayList Note the practical code of the loop foreach loop:

using System;
 using System.Collections;
 namespace ConsoleApplication1
 {
 class Program
 {
 static void Main(string[] args)
 {
 ArrayList list = new ArrayList();
 list.Add("John Doe");
 list.Add("Jane Doe");
 list.Add("Someone Else");
 foreach (string name in list)
 Console.WriteLine(name);
 Console.ReadLine();
 }
 }
33
 }

At the beginning of the example, first create an instance of an ArrayList variable called list and then create several text values As members, we have added to our array.

Using the loop foreach loop, go inside each of the array items and make the value of that member equal to the variable We have put name.

Then we print the value of the name variable in the output. As you can see in the example code, we have declared the foreach loop that the name variable is of text type is a string. When working with the foreach loop, you must specify the type of data you want the loop to contain Announce slow motion.

In cases where the data type of your array members may be different, it is better than a class object variable Use the class object to move inside the array members.

Note: Be sure to use the foreach loop when working with array variables. Because the structure is much simpler Compared to other circles in C #.

%d bloggers like this: