The logic behind the for loop

I understand that the above question is not so specific, and there are response flows related to the loop.

I am new to programming, most of the response stream is more specific.

My question is:

What is the logic of a for loop and how should there be one approach to write a for loop?

In simple words: What do you think and before writing a for loop? Example: which variable is initialized, etc.

I have problems understanding the loop, your answers will surely help novice programmers like me ..

Thanks world

+4
source share
5 answers

The most common form of a for loop uses a new variable as a loop and loop counter from zero to (but not including) the upper limit.

This loop will go from 0 to 9:

 for (int i = 0; i < 10; i++) { // code here } 

You can name a loop variable anything that makes sense. Unless you have a good name, the names i , j and k are the old convention for naming variables for small loops.

The point of using < 10 instead of <= 9 is that you loop more than ten elements. If you iterate over elements in an array, you use the length of the array in the condition:

 for (int i = 0; i < anArray.Length; i++) { // access anArray[i] } 

There are three statements inside the for loop. Initializer, loop condition, and post-update. The loop above creates the same code as if you were executing a loop with while :

 int i = 0; while (i < 10) { // code here i++; } 

You can put whatever you like into three statements in a for loop, but you should try to stick to this general form. This is how the for loop is usually used, and people will easily understand what the loop does.

+2
source

Loop exists to re-execute a specific block of code (consisting of one or more commands).

You can determine how many times a code block will be executed, and inside the loop you can use the loop iterator as a regular local variable.

Whenever I need to run something several times, I just use loop-simple like this.

Usually loop iterators do not get a special name, x , i or n is ok.

These are my two cents, given in simple language - if you have a more specific question, edit your question with it and let us know by comment so that I can edit my answer with the answer.

+1
source

when programming a for loop, it is used in those places where you must repeat the action several times when the number is known.

in C# you do this in syntax like this.

 for(int i = 0; i < 10 ; i++) { //here I am telling C# to run this loop while `i` is less than 10 where I am ////starting i from 0 and incrementing its value after each iteration by 1. so this loop //will run 10 times as i reaches from 0 to 9 and will stop as soon as i turns to 10 } 

you can also use a variable instead of the constant 10

for example, you can ask the user to tell how many times he wants to start the cycle and save it in the variable x , then you can start the cycle x times using the following code

 for(int i = 0 ; i < x ; i ++) { // if x = 5 loop will run five times } 
+1
source

The for-loop construct is used when we want to execute a block of code a certain number of times. For example, suppose I want to print numbers from one to five on the console screen, here is a naive implementation:

 Console.WriteLine(1); Console.WriteLine(2); Console.WriteLine(3); Console.WriteLine(4); Console.WriteLine(5); 

This works great, but what if the requirements change, to say write numbers from one to one hundred? Here the for-loop helps:

 int max = 100; for (int i = 1; i <= max; i++) { Console.WriteLine(i); } 

Destruction:

  • Initializer

    int i = 0; Sets the variable i to 0, which is the number of loops executed by the loop.

  • condition

    i <= max; Perform the following iteration if i less than or equal to max .

  • Step

    i++; Increases i after the code block has been executed, in pre-order for the next iteration.

Hope this helps.

0
source

It is also worth noting that for loops you do not need to count the variable up or down, it can do almost everything that follows the pattern that @aligray describes:

 for(MethodThatDoesSomething(); MethodThatReturnsBool(); MethodThatGetsCalledEveryStep()) { } 

is valid for a loop, for example.

you can even replicate the foreach loop with the for loop:

  var i = new int[ ] { 1, 2, 3 }; for( var enumerator = i.GetEnumerator( ); enumerator.MoveNext( ); ) { enumerator.Current.ToString( ); } 
0
source

All Articles