+ 13

How to use for loop?

21st Oct 2016, 12:56 PM
//Lana Wilson
//Lana Wilson - avatar
5 Antworten
+ 8
By using a for loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to false. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate. In the following example, the value of i is written to the console and incremented by 1 during each iteration of the loop. C# class ForLoopTest { static void Main() { for (int i = 1; i <= 5; i++) { Console.WriteLine(i); } } } /* Output: 1 2 3 4 5 */ * source: https://msdn.microsoft.com
21st Oct 2016, 1:05 PM
James Flanders
+ 3
As others have said on here to use a for loop you need an initializer (i.e. int i = 0;), an exit condition ( I > arr.Length;), and an incrementor (i++) *this is not required in for loop declaration, but is general practice. And that's how a for loop is used.
23rd Oct 2016, 3:55 PM
Bryan
Bryan - avatar
+ 2
It is used when you want execute block of code for many times. Its syntax is :- for(initialization, condition, increment/decrement){} However, you can skip the first and last if you are stating them at other places
22nd Oct 2016, 11:03 AM
Ritesh Ghorse
Ritesh Ghorse - avatar
+ 1
welcome first the for loop we have 2 type for loop we almost now by using interval for (int i =0;i <size;i++) { //Statment you want put in loop } int i declar first time i <size i use this way to go away from -1 becuse the count start from 1 not from 0 i++ it for interval step second type of for for (string value : arrayString) { System.out.println (value); } it's like automatically make interval
22nd Oct 2016, 5:40 PM
Mahmoud Akram
Mahmoud Akram - avatar
0
Thats great thank you
14th Mar 2017, 11:48 AM
Craig Toontas