answersLogoWhite

0

Why for loops is used?

Updated: 8/9/2023
User Avatar

Wiki User

11y ago

Best Answer

The for loop is used when you want to do a procedure, a certain amount of times.

The for loop is used when you already know how many times the loop will be repeated.

for example... you want to scan an array.

you can do something like this.

for(i=0;i<myArray.length;i++){

if(myArray[i] == 5){

alert("This array position contains the number five.");

}

}

the code above is javascript.

But the idea is the same in any language.

This is one use, but as I said. You use it when you already know the amount of times the loop will be repeated.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

The "FOR" loop allows the programmer to determine how many times a program will loop through a specific section of code.

In a typical C Programming FOR loop, the FOR statement allows the programmer to:

Initialize variables.

Test variable condition.

change variable values.

Example:

For(i=0;i<100;i++)

{

(do something important here)

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A for-loop is a shorthand construct for a while-loop: A loop variable or condition is initialized before the loop is entered for the first time, a condition is examined to determine whether the loop needs to continue with the next iteration, and steps are taken to update the loop variable prior to the next iteration.

While for-loops are often used for counted access to index items, such as iteration over all elements of an array, this loop construct is not limited to this use case.

A for-loop can always be replaced with a while-loop. In many cases, the for-loop construct appears more intuitive and more self-explanatory as the while-loop, as the three steps (initialization, condition and update) are grouped together in one expression.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A loop is a cycle, a repetition. A computer is often used to do repetitive tasks, like sending out 1000 emails. To do that, a programmer would have to write "sendEmail" 1000 times. Besides not being very practical, often you don't know upfront for how many times a certain task has to be done - you want to execute a certain task while a certain condition is true.

As there are different reasons for a loop, there are also different types of a loop,

for example the while loop (while condition true), the for loop (for x-times) or the do while loop (do while condition is true, the condition is checked at the end and not at the beginning of the loop).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The for loop is especially useful for flow control when you already know how many times you need to execute the statements in the loop's block. The for loop declaration has three main parts, besides the body of the loop:

• Declaration and initialization of variables

• The boolean expression (conditional test)

• The iteration expression

The three for declaration parts are separated by semicolons. The following two examples demonstrate the for loop. The first example shows the parts of a for loop in a pseudocode form, and the second shows a typical example of a for loop.

for (/*Initialization*/ ; /*Condition*/ ; /* Iteration */) {

/* loop body */

}

Ex:

for (int i = 0; i<10; i++) { System.out.println("i is " + i); }

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Yes, the for loop is a type of loop.

Other loop type: while.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why for loops is used?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What kind of negative loops are used to regulate chemical pathways?

Negative Feedback Loops


How can the word loops be used in a sentence?

Her garden appeared to be edged with loops of wrought iron.


Can arrays be used in for loops?

yes


What are the loops on the floor of a transit van called?

The loops on the floor of a transit van are called loop anchors. The loops are used to safely secure wheelchairs in the van.&Ecirc;


What are loops in C?

In very simple terms, Loops in any language are used to perform a task repetitively.


Why you use Loops?

Loops are used to repeat the execution of the same set of instructions again and again in a program.


What are loops in c language?

In very simple terms, Loops in any language are used to perform a task repetitively.


What technique is used to prevent bridge loops?

The mahmadur will get you


Which technology can be used in distance vector routing protocols to prevent routing loops?

Which two technologies can be used in distance vector routing protocols to prevent routing loops?


Which two technologies can be used in distance vector routing protocols to prevent routing loops?

Which two technologies can be used in distance vector routing protocols to prevent routing loops?


How can loops be used to process arrays give examples?

How_can_loops_be_used_to_process_arrays


Paper loops adhesive tabs and foam tabs are used on what?

Bitewing