While, Foreach, or For Loop…which one do I want to use?


This page assumes you have completed the Coding Intro lesson.


Loops are handy tools in programming because they allow repetitive tasks to run over and over until a condition is met. That condition can be a count, to loop/run for a specific number of times. Here we will demonstrate three kinds of loops and which one to use to get your code to do what you want it to do.

While Loop

The while loop is a conditional loop. This type of loop runs until a specific condition is met.

var x=0;
BrainPad.System.Println("Loop is over");
while x<5:
BrainPad.System.Println("Loop is over")

In this code example, the while loop only runs while x <5. Once x reaches 5, the loop is terminated.

The while loop is also handy for creating loops that NEVER END and continue running forever. We create this type of loop by just adding the word True to the condition. Since the condition is always true, it keeps running.

This next example is a code for a seconds counter. We are using Wait(1) to force the program to pause (wait) for one second.

var count = 0;
while(true) {
   count =  count + 1;
//This line of code is never reached.
BrainPad.System.Println("You will never see this!");
count = 0;
while True:
    count = count + 1;
#This line of code is never reached
BrainPad.System.Println("You will never see this!")

Foreach Loop

We can use a foreach loop to loop through a list of values, like an array of names. Similar code is achieved in Python using for loop.

var nameList = new string[] {"Bob","Lisa","Gus","Tina"};

foreach (string name in nameList){
nameList = ['Bob', 'Lisa','Gus','Tina']

for name in nameList:

For Loop

We have seen how Python’s for loop works very similarly to C#’s foreach loop. However, for loop for numbers work differently between C# and Python.

for (var i=0; i<5; i=i+1){

The C# for loop has three sections. First is the initialize section that runs when the loop starts, which is var i=0 in the example above. The second section is what condition to check for in each loop (similar to the while-loop), and that is i<5. Finally, the last section is what change will happen to the variable in each loop. In this case it is i=i+1, which will increment the variable by one.

Python can do something similar using the range statement: range(5) is 0 to 4, for example.

for x in range(5):

Since for loops are not very similar between languages, we will stay away from using them and use while loops whenever possible.

What’s Next?

Loops are great, but to get the full power of coding we need to check and run code based on a specific condition to handle the program’s Flow Control.


Loops help computers in repeating tasks. To print 1 to 3, we can just input three lines, but how do we count to 100? Can you make a loop that prints all even numbers up to 100?

var x=0;
while x<100:

Content Licensing

Twitter Feed
Hot off the press!
November 24, 2023
November 20, 2023
November 15, 2023
November 15, 2023
November 9, 2023
November 9, 2023
October 31, 2023
October 24, 2023