|
Decision making is the most important part of making any programming or scripting language interactive. When the user types in some text in a text box, there may be certain criteria that the text has to meet. The most simlpest form of decision making is the if() statement. For example, say the user types in their password. The password would be stored in an array. If the password is in the array, and it is typed correctly, then the user will be allowed into the Web site. Consider the lines below: var Password = new Array() {templton}; var User; if(User == Password[]) Notice that this if() statement has only one line to process. We put the document.write() statement on a separate line to make it easier to read. You may put statements like this on the same line as the if() statement if you choose to. If there were other lines before or after document.write("Welcome");, you would have to enclose them in { } curly braces. This is what is called a command block. Now, say an if() statement wasn't enough. You have two conditions that you want to test against and only one statement. This is where the if...else statement is very useful. With the if...else statement, the if() value is returned as true and the else value is returned as false. If you have multiple variables that you need to test, you can put if...else statements within if...else statements. These are called nested if...else statements. Always remember: an if() statement can be constructed without an else clause, but the else clause can only be used with the if() statement. This seems to be making your program more open to a wider array of variables. However, this also seems like a more tedious way of testing your data. You can make data testing simpler by using switch statements. With a switch statement, instead of testing every expression, you can test the ones you want to be tested. Other programming languages like Java and C++ require all case labels within a switch to be of the same data type. Consider this, you have a program that requires the user to enter their astrological sign to find out what their horoscope is. Instead of the program going from sign to sign to see if this is the one they chose, the switch statement searches for the sign the user entered from it's list of case labels and then executes the program to print the horoscope. For example: if(sign == Aquarius) You could write: switch (sign) { The line break; "kicks out" of the program as soon as the right sign is found. This creates efficientcy in programming and makes programs work faster, especially with slower modems and smaller amounts of RAM or slow processors. The point of programming in ANY language is to make the program efficient. If the user has to wait for their computer to catch up to yours, chances are you won't have too many users, only surfers. NOTE: In actual programming, you would leave out the "[" and "]" |
|
Question: WOW!! I had no idea that there are different reasons for using if...else statements and switch statements. Speaking of "efficiency", are there any other statements that I can use to test the data I'm using?
Answer: There are several. Next, we will look at repetition statements that include the while, do...while, for, for...in, and with statements.
If you have a questions about any of the lessons, feel free to ask.