Home > Articles > Open Source > Ajax & JavaScript

JavaScript by Example: Under Certain Conditions

📄 Contents

  1. 6.1 Control Structures, Blocks, and Compound Statements
  2. 6.2 Conditionals
  3. 6.3 Loops
  4. 6.4 What You Should Know
  5. Exercises
  • Print
  • + Share This
Ellie Quigley shows you how to work with Control Structures, Blocks, Compound Statements, Conditionals, and Loops in this chapter from her book, JavaScript by Example, 2nd Edition.
This chapter is from the book

This chapter is from the book

6.1 Control Structures, Blocks, and Compound Statements

If you were confronted with the above signpost, you'd have to decide which direction to take. People control their lives by making decisions, and so do programs. In fact, according to computer science books, a good language allows you to control the flow of your program in three ways. It lets you

  • Execute a sequence of statements.
  • Branch to an alternative sequence of statements, based on a test.
  • Repeat a sequence of statements until some condition is met.

Well, then JavaScript must be a good language. We've already used programs that execute a sequence of statements, one after another.

Now we will examine the branching and looping control structures that allow the flow of the program's control to change depending on some conditional expression.

The decision-making constructs (if, if/else, if/else if, switch) contain a control expression that determines whether a block of statements will be executed. The looping constructs (while, for) allow the program to execute a statement block repetitively until some condition is satisfied.

A compound statement or block consists of a group of statements surrounded by curly braces. The block is syntactically equivalent to a single statement and usually follows an if, else, while, or for construct.

  • + Share This
  • 🔖 Save To Your Account