Loops

Loops

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

Prerequisite

This page assumes you have completed the Coding Intro lesson.

Intro

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.

Python

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

C#

var x=0;
while(x<5){
    BrainPad.System.Println(x);
    x=x+1;
}
BrainPad.System.Println("Loop is over");

JS

let x=0;
while(x<5){
    await BrainPad.System.Println(x);
    x=x+1;
}
await 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.

Python

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

C#

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

JS

let x=0;
while(x<5){
    await BrainPad.System.Println(x);
    x=x+1;
}
await BrainPad.System.Println("Loop is over");

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 a for loop.

Python

nameList = ['Bob', 'Lisa','Gus','Tina']

for name in nameList:
    BrainPad.System.Println(name)

C#

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

foreach (string name in nameList){
    BrainPad.System.Println(name);
}

JS

let count = 0;
while(true) {
    await BrainPad.System.Println(count);
   count =  count + 1;
   await BrainPad.System.Wait(1000);
}
//This line of code is never reached.
await BrainPad.System.Println("You will never see this!");

For Loop

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

C#

for (var i=0; i<5; i=i+1){
    BrainPad.System.Println(i); 
}

JS

for (let i=0 ; i<5 ; i=i+1){
    await BrainPad.System.Println(i); 
}
await BrainPad.System.Println('Loop is over'); 

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):
  BrainPad.System.Println(x)

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.


BrainStorm

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?

Python

x=0
while x<100:
    BrainPad.System.Println(x)
    x=x+2

C#

var x=0;
while(x<100){
    BrainPad.System.Println(x);
    x=x+2;
}

JS

let x=0;
while(x<100){
    await BrainPad.System.Println(x);
    x=x+2;
}

Content Licensing
Newsletter

Twitter Feed
Hot off the press!