Conditions
n341.tplt
Overview
Can computers think?
Jacquard's loom
- Sequential logic
- beginning, middle and end
Example:
Morning weather report
- If it's cool out today, I'll wear a sweater
Logic
Humans understand different kinds of syntax to get to the semantics
Computers - compare some variable to a value or another variable
Conditions - evaluated as true or false
Example
temperature < 65
temperature is a variable with a starting value
(<) is a comparison operator
Last part of the conditional statement is often a value
- value must be same type as the variable
- could also be a variable
Evaluate
What is cool to the computer?
Translate the vague concept 'cool' into a condition the computer can understand
Branching - If Statement
Which way the train goes is dependent on a switch in the track.
The conditional statement is the switch.
Simple JavaScript
-If Statement
//set up my variable
var temperature = 0;
temperature = prompt ("What is the temperature?");
//be sure we have an integer to work with
temperature = parseInt(temperature);
//check the temperature
if (temperature < 65){
//this will ONLY happen when temp is less than 65
alert ("Put a sweater on!!");
}//end if
//no matter what the temp, we're wearing pants
alert ("Put some pants on.");
The Else Clause
If statement useful, but could be improved.
Example:
-if we do not wear a sweater, we should wear something
If it is cool out today,
wear a sweater
otherwise,
wear a shortsleeve shirt
JavaScript Example
var temperature = 0;
temperature = prompt("What is the temperature?");
temperature = parseInt(temperature);
if (temperature < 65){
alert ("Wear a sweater!!);
} else {
alert ("Wear short sleeves!!");
} // end if
Else Clause
Happens only when the condition is evaluated as FALSE.
Else doesn't make sense without an 'if' statement.
If-else commonly used.
"{…}" used to combine lines, do not use a semicolon at the end of the statement using the brackets
- this causes your condition to be ignored!
Multiple Branching
Sometimes your decisions will be more complex than a simple 'if' statement.
Example:
if it's warm outside,
wear short sleeves
if it's cool,
wear a sweater,
if it's cold,
wear a jacket
Multiple 'If' Statements

You can use three different 'if' statements
OR
You can use 'nested if' statements
The Switch Structure
Some languages support a special 'multiple-branch' structure
Basic-like languages use "select case"
Many C-like languages use the switch statement
Loops

Allows certain parts of code to repeat based on the status of a condition
New syntax -- while statement.
The while statement checks the condition and repeats the code between {…} symbols if the condition is TRUE.
Sentry variables and Initialization
Start by creating a variable
Giving the variable an initial value is initializing the variable
Variables used in loops are often known as sentry variables
Example
//Note that I initialized stillRacing incorrectly
var stillRacing = "N";
//This condition will only be checked once, and it will be false
while (stillRacing == "Y"){
//the body of this loop will NEVER be activated
alert ("zooming around the track.");
//see if we're still on
stillRacing = prompt("Is the race still on? (Y/N)");
}//end while
alert ("Nice race!!");
Preventing Endless Loops
Inside a loop, we MUST ensure that there is some way of exiting the loop
We have to make it possible for the sentry variable to change values
Example
var stillRacing = "Y";
while (stillRacing == "Y"){
alert("zooming around the track");
//forgot to check if we are still racing!!!
}// end while
alert ("Nice race!!");
This program will go on FOREVER!!!
Endless Loops
The condition statement checks to see if the condition is TRUE
In the previous example, the condition is true
Because the condition is true, the body of the loop still thinks we are "zooming around the track"
Whenever you make a loop, it is your obligation to ensure that the sentry variable is changed so it is possible to exit the loop
The For Loop
Variation of the loop we have already used
Difference - we have added a counter
Needs four pieces of information:
- name of counting variable
- starting value of the variable
- ending value of the variable
- how to increment the variable
For Loop
The for statement needs three parts:
1. initialize the variable
2. a condition
3. how to increment the variable
Once the loop is set up, we don't have to worry about it in the body - it automatically increments
Example
var lap = 1;
for (lap = 1; lap <= 10; lap ++){
alert ("Now on lap " + lap);
}// end for
alert ("Good race!!");