Hoang Phi Hong, you must have heard a lot about this name, especially those who have a bit of blood in fighting and kicking will know very well. This master has a technique called Shadowless Kick. Roughly it is a series of consecutive kicks. It is repetition. In programming we also have a similar technique, also performing the repetition of a certain piece of code. That's the charge loop, oh my…the loop structures.
In C#, we have loop types like while loop, do-while loop, for loop, and foreach loop. Within the scope of this article, I only present the concept, structure, block diagram, and usage (just at a simple level) of each of the above iterations.
First, we need to understand iteration constructs that will allow us to execute a statement or group of statements n times when the loop condition is true.
1. While loop
Is a simple loop structure, allowing statements to be executed until the value of the conditional expression is still true (condition expression = true).
Syntax:
While (condition)
{
Statement(s);
}
For example:
Run the above code, will print the screen values the values of n from 0 -> 6.
We see the structure of the while loop that will consider the value of n before executing the code inside the loop. With the while loop structure, we do not specify the number of iterations, the number of iterations depends on the value of the conditional expression. So it is very likely that the statements inside the loop will not be executed at all.
The flowchart diagram, which can be represented as follows:
2. Do-while loop
The do-while construct is similar to the while construct. It allows executing statements when the value of the conditional expression is true. It also cannot know the exact number of iterations in advance, it depends on the value of the conditional expression. But the difference from the while structure is that the conditional expression is placed at the bottom of the loop. This means that even if the conditional expression is false, the loop will be executed at least once. You can think of it as something like Better a glorious minute than a hundred years.
The structure of do-while is as simple as while:
do
{
Statement(s);
}
while(condition)
Let see the below code:
Here's the result
Even though the conditional expression is false, the loop's code is still executed once. It is a remarkable feature when we use the do-while structure.
When studying the two structures above, we see that they are both structures with an indefinite number of iterations. Next, we will learn two iterative constructs, which we can predetermine the number of iterations.
3. For loop
This structure, allows statements to be executed when the value of the conditional expression is still true.
The difference with the above two structures is that the for loop has 3 components: the initialization expression for the loop, the conditional expression, and the counter variable. This helps fraternal coders completely determine the exact number of iterations they want.
Its structure is as follows
for(init-expr; cond-expr; count-expr)
{
Statement(s);
}
Example:
Here's what we got
Here's the flowchart:
The for construct is very useful in case we need to iterate a certain number of times.
4. Foreach loop:
We have a collection of type arrays or any kind of collection. The usual way we use for traverses from 0 -> array.length; There is nothing wrong with this, but in C# there is a loop type that helps us iterate through the elements of an array or any type of collection. It's foreach.
We have a syntax:
foreach(elementtype
element in collection)
{
Statement(s);
}
For example, we have an array of list of courses, we want to go through this list and print the name of each element in the list, we can use this trick as follows:
Here we got
One thing worth noting about foreach is that we traverse the array sequentially, using only the element in question, and don't care about the number of elements currently in the array. In contrast, with for, we can do random access while traversing the array, but need to know the number of elements in the array.
Each iterative structure has its own advantages and disadvantages, and how to use it to achieve the desired results is up to each coder. But first, having a good understanding of iterative constructs, and practicing it in specific situations over and over again, will help us to use them more effectively.







