Skip to main content
Menu
CodeBabes
  • Create new account
  • Request new password

or

  • Sign in with Twitter
  • Code
  • Babes
  • Sign Up
  • Log In

Looks like you missed a quiz. Go back and pass that shit!


NO STRIP FOR YOU!

Share the Love

  • Tweet

Programming with Loops

In programming there is a lot of repetition. That’s the thing about computers, they can do stuff over and over again really efficiently. When you're writing code for a program, there is a shortcut for doing things over and over again. It’s called a loop.

Let’s say you want a piece of code repeated a bunch of times, that’s what a loop can do for you.

We’ll start with the most basic loop, called the while loop. It’s like the if-statement where if something is true it will do something, except it will do that thing over and over again.

Start with while, then parentheses, inside the parentheses, we put our condition. Then, braces, this is where we put the statements or functions we want to execute.

Let’s set a variable counter equal to 0. Then in our while loop parentheses we’ll check if counter is less than or equal to 10. Then let’s add console log “I’ll be back” as the statement to run while the counter is less than or equal to 10. If we just ran it like this it would crash the browser since it would be an infinite loop. The counter value would never be greater than 10, so, the condition would never be false. Let’s log the counter variable, you'll see it prints zero forever.

What we need to do is increment the counter. Remember the shortcut operator for this? It’s the plus plus operator. Let’s add that to the block.

Let’s add an alert that will run after the loop stops, we’ll do alert "hasta la vista baby" and concatenate the counter variable onto the end. Let’s run this shit!

And boom, hasta la vista baby, and the counter is equal to 11. During the last loop the counter value was 10, but got one more added on so the loop stopped and the alert was executed.

Loops are cool, but when their powers are combined with arrays they become really cool, because now we can loop through the entire array and do something to each element.

Let’s set an array, we’ll do var hunks equal to an array of hunky dudes.

To hit up every element, or iterate over an array, we can use a FOR loop. The for loop just combines elements of the while loop into one line. So we set up the index (beat), check a condition (beat) and then increment the index.

We run this and see that it prints out each index value, plus the statement “so hot right now.” What if we add another hunk though? Let’s add Zach Galifinakis, nothing hotter than a funny guy with a nice beard. We run it again and see that the loop doesn't think so, he doesn’t get printed. The reason is the index is limited to less than 3, so it only runs 3 times, and doesn’t make it to Zach.

What we can do is change this to something smarter. Let's use the length of the array as our condition. We do this by writing the array name, hunks then dot length. The dot is how we can access properties of the array. We put that in as the condition, so now the condition will adjust automatically with the length of the array, we can add more hunks, like Channing Tatem, and Dane Cook, just kidding, Dane Cook sucks.

Alright, next we’re going to talk about functions and you might need to take a cold shower.

Questions or Comments?

Lessons

  • Variables in Programming
  • Programming Intro Conditional Statements
  • Programming Intro to Arrays
  • Programming with Loops
  • Programming with Functions
Programming Virgin with Javascript

Downloads

SignUp & Subscribe

What the What?

Lessons: 41

Much more to come!

50/50 Entertainment and Education. Learning doesn't have to be boring :)

Our Philosophy

Contact

  • support
  • Tweets!
  • Youtube
  • Google Plus
  • Facebook

Stuff

  • Blog
  • Sign Up
  • All Babes
  • All Courses
  • Become a CodeBabe!
  • MAKE A LESSON WITH CODEBABES!

Recent Courses

Internet Virgin
SEO Virgin
HTML Virgin
CSS Virgin
Programming Virgin with Javascript

Learn to Code with Babes

Copyright © 2025 CodeBabes.com • Made with

  • Privacy Policy
  • Terms of Use
Close Menu
  • Blog

  • All Courses
  • Android Virgin
    • Android Studio Tour
    • App Layout in Android Studio
    • Java Variables, Java Data Types & Android Views
    • Java Event Handling in Android
    • Android Intents, OOP This, Variable Scope
    • Passing the Data with Android Intents & Finish
    • Complete the Android App with Launcher Icons & APK
    • Android Happy Ending
  • Internet Intro
    • Backend Development
    • Front End Web Development
    • Programming Tools you Need
    • Hosting and Domain Names
  • HTML Virgin
    • HTML Text Tags
    • HTML Links and Images
    • HTML Layout with HTML 5
    • HTML Tables
  • CSS Virgin
    • Where to Stick your CSS
    • CSS Properties, Sizes and Colors
    • CSS Selectors
    • CSS Box Model
  • Programming Virgin
    • Variables in Programming
    • Programming Intro Conditional Statements
    • Programming Intro to Arrays
    • Programming with Loops
    • Programming with Functions
  • PHP Virgin
    • PHP Variables
    • PHP Conditionals
    • PHP Arrays
    • PHP Loops
    • PHP Functions
  • SEO Virgin
    • Make Google Love You
    • Understanding Keyword SEO
    • Optimizing for SEO
    • Technical SEO