What is "for" used for | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

What is "for" used for

9th Sep 2019, 6:58 AM
Moises Mariscal
Moises Mariscal - avatar
41 Answers
+ 7
A lot of language are using this for loop. Example. c# for ( int i = 0; i < 3; i++) { Console.WriteLine("Hello World); } // output is Hello World Hello World Hello World It's a loop and print a hello World 3 times.
9th Sep 2019, 7:05 AM
Mohammed AL-Balawi
Mohammed AL-Balawi - avatar
+ 6
For repeating tasks.
10th Sep 2019, 3:24 AM
Sonic
Sonic - avatar
+ 5
ย computer science, a for-loopย (or simply forย loop) is a control flow statement for specifying iteration, which allows code to be executed repeatedly. ... For-loopsย are typicallyย used whenย the number of iterations is known before entering theย loop. ๏ฟผ
9th Sep 2019, 7:06 AM
KfirWe
KfirWe - avatar
+ 5
It is mainly used for accessing items in array and sometimes accessing each letter in String e.g : Accessing each element; var a = [2,4,3,51,6] for(let i = 0;i < a.length;i++){ console.log(a[i]); } //2,4,3,51,6 Accessing each letter in String. var s = "Hello"; for(let i = 0;i < s.length;i++){ console.log(s.charAt(i)); } //Hello
10th Sep 2019, 2:09 AM
Roopesh
Roopesh - avatar
+ 4
Almost everything...
9th Sep 2019, 7:03 AM
KfirWe
KfirWe - avatar
+ 4
Moises Mariscal The classic use of for loop for beginners is like run over an arr to check or change or just look for something in the elements
9th Sep 2019, 7:08 AM
KfirWe
KfirWe - avatar
+ 4
"for" loop is used to repeat a set of of statements on the basis of condition. It is often used when you know the no of iteration in advance like how many time will loop execute
9th Sep 2019, 4:53 PM
Hamza khan
Hamza khan - avatar
+ 4
for loop is basically used for repeating task while specifying the initial value of a variable with a condition to be satisfied and the increment/decrement factor. for loop can be nested to create different patterns. There are various uses of for loop.
10th Sep 2019, 5:28 PM
Indrayudh Mandal
Indrayudh Mandal - avatar
+ 3
For loops will basically just squeeze 3 of typical while loop's statements into 1 line. For loops make it easier to make loops, where the amount of iterations is known. While loops can be used for loops whose exit conditions might be more arbitrary.
9th Sep 2019, 7:13 AM
Seb TheS
Seb TheS - avatar
+ 3
For loop is used when we know how many times when we want to execute the code inside for
10th Sep 2019, 10:07 AM
Simran Singh Rathore
Simran Singh Rathore - avatar
+ 3
It is a kind of loop which is used to iterate a set of operation. It makes the code efficient.
11th Sep 2019, 2:02 AM
Manoj
Manoj - avatar
+ 2
Do you have an example?
9th Sep 2019, 7:04 AM
Moises Mariscal
Moises Mariscal - avatar
+ 2
Thanks
9th Sep 2019, 7:07 AM
Moises Mariscal
Moises Mariscal - avatar
+ 2
To repeat
9th Sep 2019, 10:56 AM
Da Ven
Da Ven - avatar
+ 2
Hello am new here, hoping to learn alot here
9th Sep 2019, 9:22 PM
Nana Kwame
+ 2
For repeating things many times how many you want
10th Sep 2019, 7:54 AM
Muhammad Mahzaib
Muhammad Mahzaib - avatar
+ 2
In Python it is for loop for iteration over a iterator like x = [1,2,3,4,5] For i in x: print(i) This code will give the following output 1 2 3 4 5
10th Sep 2019, 8:37 AM
Akhilesh Singh Bhadauriya
Akhilesh Singh Bhadauriya - avatar
+ 2
It is used for repeating given argument for the limited number of times say n
10th Sep 2019, 10:54 AM
Ankush Mavi GUJJAR
Ankush Mavi GUJJAR - avatar
+ 2
Easy! Use the "for" loop when you need to do a repeating task and you know the number of repetitions. E.g. i want to repeat this thing 4 TIMES. for(i = 0; i < 4; i++) { do something } But if you want to repeat and you don't know how many times, use the "while" loop. E.g. I want repeat this the number of times the user of my app input previously (even 0 times). x = input( a number ) while ( i < x ) { do something } But if you want to do something that repeats itself and you want to do it once at least, use "do while" loops. do { something } while ( i < x ) I hope this can help you. Sorry if my english isn't the best๐Ÿ˜…
10th Sep 2019, 12:48 PM
Maxx
Maxx - avatar
+ 2
Moises Mariscal When you want to repeat any sentence or task for number of times so you use ( for loop) When you want to repeat any task or sentence for infinity till the condition gets right you use ( while loop )
10th Sep 2019, 4:09 PM
Yash๐Ÿ๐Ÿ”˜
Yash๐Ÿ๐Ÿ”˜ - avatar