Any advanced coder know how to use 'for' loops?

we are planning on using a for loop for our code. our calendar is crammed, we dont have much time to solve the problem.

In order to help, please specify what task you need to perform. Simply put, a for loop can be used to run a specific piece of code a certain amount of times. On the other hand, while loops can be used to run a specific piece of code until a condition is met.

While loops are written like this:

 while ( condition ) {

For Loops are written like this:

 for( init; condition;  iterator){

The init is usually a definition and is ran once at the start of a loop
The condition is what declares whether the loop shall run again
The iterator alters the variable defined in the init statement


 for( int i=0;  i<5;  i++){

This code segment initializes a variable i to equal 0. It then checks whether the condition is true, in this case, the condition would be i<5. Since 0<5 the loop runs until the end. After completion of the code within the loop, i is altered by the designated amount, in this case, we used i++, which adds 1. Now that i is 1, it checks the condition again, since 1<5 it will run the code within the loop until completion. It then adds 1 to i again and continues the process. Then, once i=5, 5 is no longer less than 5, thus the condition 5<5 is false, and the loop stops running, thus making the loop run 5 times.


Since this was posted in a VEX IQ category, i’ll post a solution for VEXCode IQ Blocks.

For loops can be represented with the “repeat” and “repeat until” blocks.

“repeat” will repeat the code the number of times in the text box next to it, and “repeat until” repeats until a certain condition is meant.

If using RobotC, use what 293X said above.


Scratch doesn’t have a pre-built for loop.

All of these would do basically the same thing… On the edge case the last one wouldn’t run the 10th loop. But still lots of ways to get it done.


Again, if you give us more context our answers will get better.


wow! i never knew you could do that! thanks!