HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Programming Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: A Brief History of Computers
  3. Lesson 3: The Workings of Computers
  4. Lesson 4: A Brief History of Programming
  5. Lesson 5: High-Level vs. Low-Level
  6. Lesson 6: Compiled vs. Interpreted
  7. Lesson 7: Variables
  8. Lesson 8.1: Arithmetic and Operators - Decimal
  9. Lesson 8.2: Arithmetic and Operators - Integer and Boolean
  10. Lesson 9.1: Conditional Statements - The Basics
  11. Lesson 9.2: Conditional Statements - Branching Conditionals
  12. Lesson 10.1: Loops - The Basics
  13. Lesson 10.2: Types of Loop
  14. Lesson 11: Functions
  15. Lesson 12: Strings
  16. Lesson 13: Procedural vs. Object-Oriented

Programming Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 10.1: Loops – The Basics

del.icio.us Digg Blinklist reddit

In our last lesson we took a look at conditional statements, then went more in-depth in discussing the first type: the branching conditional statements. In this lesson we’re going to talk about the other type: the looping conditional statements (also just called loops).

What are Loops?

As I mentioned briefly in Lesson 9, loops basically will “loop” through a portion of code, going through all the code then looping back to the start when it reaches the bottom, as long as the condition is true.

Up until now, all of the code we’ve discussed has moved in a very linear path, straight down, line by line (with the exception of our branching conditional statements, which may skip a few lines). With the addition of loops, we’ll now be able to repeat portions of code multiple times without having to rewrite the code multiple times.

What Are Loops For?

For a simple example, let’s say we wanted to Print 12345. Doing it without loops would look something like this:

Print "1"
Print "2"
Print "3"
Print "4"
Print "5"

With a loop (in this case, a for loop), it’d be something like this:

For Integer i = 1 While i <= 5 Increment i = i + 1
    Print i
End For

With both of those, we would get the same output, but our loop version is a lot easier. You may say “big deal, it’s 2 lines shorter”, but imagine if you wanted to count from 1 to 10,000 or a million. Then the for loop would be several thousands of lines shorter.

Loops are also used when something needs to be done over and over again. In fact, right now there are probably several (if not dozens) of loops being run on your computer at this very moment. For example, on your operating system, each loop your operating system is causing your screen to redraw what it is displaying (if necessary). If you have an instant messenger running, it is constantly looping to check for new messages, see if friends have logged on or logged off, etc. In video games, it will loop around and each loop it will check for user input, make enemies move, etc.

As you can see, loops can be very useful.


In this lesson we talked about what loops are and the premise that they operate on. In the next portion of our lesson we’ll discuss the specifics of three common types (and two non-as-common types) of loops: for, while, do…while, foreach, and do…until.

This entry was posted on Monday, November 9th, 2009 at 3:23 pm and is filed under Programming Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design