Loops - C#
Hello again!
Today I will talk you more about basic of algorithms.
How often you need to find something? How often you want to check something from list of task?
For searching inside containers we are using loops, look for few examples: The most often in use as I can imagine is for loop:
for (int i = 0; i < strArray.length; i++) {
print( strArray[i]); //print all elements from table
}
inside bracket you have initialization and set value of variable - on start, next before you start doing algorithm you want to check is we need to do it - question "yes or no", at least what we do after algorithm was done - prepare values.
On site: https://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm
you have nice diagram - how it works. It is from C++ course, but it works the same way.
Next loop is while - it mean do while condition is true.
while( condition ) {
//instructions
}
While loop is using when you no need to prepare data or making iterations. Very important, but not all of you know it, but you can think how to use it for your own. If condition is failed on start then algorithm never launch this algorithm.
Do while - is similar loop, but it has one change, even when condition is false algorithm will is running one time, because condition is at the end of loop. The most important - algorithm is launched one time or more.
do
{
//instructions
} while (condition);
And last which main task is too browse containers is for each. Do something for each element:
string[] strArray = {"one", "two", "three"};
foreach(string str in strArray) {
print(str);
}
And that is all for main loops in C# in next material we can talk more about variables. Thank you for reading and I am waiting for your proposition of topics.