What is "for" used for | SoloLearn: Learn to code for FREE!

+9

What is "for" used for

9/9/2019 6:58:03 AM

Moises Mariscal

42 Answers

New Answer

+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.

+6

For repeating tasks.

+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. 

+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

+4

Almost everything...

+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

+4

I think " for " was made exclusively to handle arrays, if I'm not mistaken

+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

+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.

+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.

+3

For loop is used when we know how many times when we want to execute the code inside for

+3

It is a kind of loop which is used to iterate a set of operation. It makes the code efficient.

+2

Do you have an example?

+2

+2

To repeat

+2

Hello am new here, hoping to learn alot here

+2

For repeating things many times how many you want

+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

+2

It is used for repeating given argument for the limited number of times say n

+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😅