Loops, Curly Brackets and the Power of Repeating Code

In week 2, Nick explained “for loops” and how they allow simple commands of code to do much larger things. Part of the learning process included recognizing two major grammatical clues or code syntax, the curly brace, { , and the double forward slash, //.

If you have ever looked at a page of code and wondered what all these dangling wiggly lines are for then you are not alone. This curly bracket, or brace, begins a code function called a loop. In particular we were introduced to a “for loop” which repeats a given piece of code over and over for a defined set of items until it sees a right brace, }. In our case, it was for all the pixels in a digital image. Last week, we learned how to manipulate the color of one pixel at a time and the “for loop” enabled us to do this for all the pixels modifying the whole image.

The full example would look something like this:

image = new SimpleImage(“51020-banana.png”);
for (pixel: image) {
// your code here
pixel.setRed(pixel.getRed() * 20);
pixel.setGreen(pixel.getGreen() * 5);
pixel.setBlue(pixel.getBlue() * 10);


I now understand that the brackets contain code that is meant to modify the Red, Green, and Blue values for one pixel and repeat it until all the pixels are changed in the image.

Also important to know is that the two forward slashes  //  signify a comment that is purely for human eyes and will be ignored by computers until the next line.

Understanding these two “grammar” points of computer code have helped me to make more sense of what I see when I look at code.

This entry was posted in Programming, Syntax and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s