Chapter 3: Using Conditional Statements


In this chapter, things get kicked up a notch and become really fun!

Conditional statements tell the computer to make a choice depending on the evaluation of a condition. Often, evaluating the condition means checking to see what the user selected—so conditional statements allow you to create programs that are interactive. What the computer does, in other words, depends on what the human interacting with the computer does.

Without conditional statements, it’s hard to use a computer as anything much more than a really good calculation engine—so conditionals are crucial to understanding the true potential of computer programs.

In this chapter, I explain how JavaScript conditional expressions work. Next, I show you the three forms of if statements that you can use in JavaScript. To have some fun with if statements, I show you how to write a program that plays a game—Rock, Scissors, and Paper—with the computer against the human race.

The last topic in this chapter is using the switch statement. Switch statements can be a powerful way to code multiple if statements as a single evaluative statement—thus greatly simplifying code, which is always a good thing.

I also show you how to recode the Rock, Scissors, and Paper game more simply, using two switch statements rather than the many if statements in the original version.


To accomplish the goals of this chapter, particularly creating the Rock, Scissors, and Paper game, we’ll have to get a little ahead of ourselves. Some of the code in this chapter uses conceptual material not explained in full detail until later in this book. Don’t be scared; I explain things as we go, and nothing in this chapter should give you great difficulty.

Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: