0 index
1 Overview
2 Logic
3 Example
4 Evaluate
5 Branching - If Statement
6 Simple JavaScript
   -If Statement
7 The Else Clause
8 JavaScript Example
9 Else Clause
10 Multiple Branching
11 Multiple 'If' Statements
12 The Switch Structure
13 Loops
14 Sentry variables and Initialization
15 Example
16 Preventing Endless Loops
17 Example
18 Endless Loops
19 The For Loop
20 For Loop
21 Example

outline
created using slideshow.cgi by Andy Harris















CSCI N341 Web Programming: Conditions
1. 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



































CSCI N341 Web Programming: Conditions
2. 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



































CSCI N341 Web Programming: Conditions
3. 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



































CSCI N341 Web Programming: Conditions
4. Evaluate
  • What is cool to the computer?
  • Translate the vague concept 'cool' into a condition the computer can understand



































CSCI N341 Web Programming: Conditions
5. Branching - If Statement
  • Which way the train goes is dependent on a switch in the track.
  • The conditional statement is the switch.



































CSCI N341 Web Programming: Conditions
6. 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.");



































CSCI N341 Web Programming: Conditions
7. 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



































CSCI N341 Web Programming: Conditions
8. 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



































CSCI N341 Web Programming: Conditions
9. 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!



































CSCI N341 Web Programming: Conditions
10. 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



































CSCI N341 Web Programming: Conditions
11. Multiple 'If' Statements

  • You can use three different 'if' statements

    OR


    You can use 'nested if' statements



































CSCI N341 Web Programming: Conditions
12. 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



































CSCI N341 Web Programming: Conditions
13. 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.



































CSCI N341 Web Programming: Conditions
14. 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



































CSCI N341 Web Programming: Conditions
15. 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!!");



































CSCI N341 Web Programming: Conditions
16. 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



































CSCI N341 Web Programming: Conditions
17. 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!!!



































CSCI N341 Web Programming: Conditions
18. 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



































CSCI N341 Web Programming: Conditions
19. 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



































CSCI N341 Web Programming: Conditions
20. 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



































CSCI N341 Web Programming: Conditions
21. Example
  • var lap = 1;

    for (lap = 1; lap <= 10; lap ++){
       alert ("Now on lap " + lap);
    }// end for

    alert ("Good race!!");



































outline

Overview

Logic

Example

Evaluate

Branching - If Statement

Simple JavaScript
   -If Statement

The Else Clause

JavaScript Example

Else Clause

Multiple Branching

Multiple 'If' Statements

The Switch Structure

Loops

Sentry variables and Initialization

Example

Preventing Endless Loops

Example

Endless Loops

The For Loop

For Loop

Example