Module 6 Statements and Loops

These are examples of Conditional Statements and Loops.



Understanding Conditional Statements

Conditional Statements is a statement that you can use to execute a bit of code based on a condition or to do something else if that condition is not met. You can think of conditional statements as cause and effect statements.

There are 2 types of conditional statements, if/else and switch statements. If/Else is used mostly for simple comparison of the values. If something is true then yes, if false then no. Almost a too simple comparison but you get the picture. The switch statement allows you to take a single variable value and execute a different line of code based on the value of the variable. If you wish to check for a different number of values, this can be an easier method then the use of a set of nested if/else statements. For this module we will only be looking at if/else statement

As an example of an if/else statement, lets say that in order for me to go to a NASCAR race, I must have credentials. If I have credentials, I will go to the race, if I do not have credentials, I will not go to a race. Using the variable credentials and applying a "yes" to the if on the credential variable, it would simply return this statement in a document.write.

I have credentials to Daytona, therefore

The same would hold true by changing the if to a "no" thus calling the else statement into play.

I do not have credentials for California, therefore

Now lets say I have a lot of tracks I will receive credentials for. I can still use the if/else but narrow my document.write to only tracks that I will receive credentials from. If I will not receive credentials, I simply say I am not going without having to list the tracks.

I will receive credentials from Atlanta, Nashville, Kentucky, Milwaukee, Chicago, Indianapolis, Michigan, Charlotte and Memphis so
If I do not receive credentials,

Understanding Loop Statements

A loop is a block of code that allows you to repeat a section of code a certain number of time, perhaps changing certain variable values each time the code is executes. By doing this, you are often able to shorten certain tasks into a few lines of code, rather then writing the same line over and over again within the sscript and tiring your fingers (and brain). The 3 types of loops are for, while and do while loops. For this module we will be looking at the for and while loops only.

To use the for loop in JavaScript, you need to know how to code the basic structure of the loop and how to to nest a for loop within another loop. The structure of a for loop is very similar to that of a conditional block. The major difference is the loop serves a different purpose therefore the first line is different.

For a plain stuctured loop, you would write for with the variables showing how long the loop will run. So I wanted to tell you 12 times how much I love racing, the variable would be (count=1;count<13;count+=1). Let's see how it looks written out.

Now we want to add a nested loop to the plain one. We need to add a for statement inside the first document.write of I LOVE NASCAR! and add the curly brackets {} inside also. I will nest my favorite driver to the loop!

Or I can simply add that statement one time at a certain point by adding the if/else statement nested in the for.

A while loop just looks at a short comparison and repeats it until the comparison is no longer true. You place your variable with an assigned value to count the loop and the while statement begins with a comparion. A count variable is adjusted so that you do not end up with an endless loop.

So now we want to get ready for something that is going to repeat until it reaches the "less then" number I assign. Am I going to be mean and make you read to eternity or nice and just give you a small headache?

So there you have it, Conditional Statements and Loops! And I wasn't even overly mean to your eyes, was I now.

Return to the top of the page.