JavaScript with Sean Doyle

Lesson 2: Control Structures

Control Structures

The Big 5 Concepts

  1. Control structures allow us to direct the “flow” of our code by adding branches of code to the main code.
  2. A logic check will test a value, then enter a code block branch based on the results of that test: true = go this way; false = go that way.
  3. If a logic check is not met, it does not cause an error, it simply skips that branch of the code.
  4. Comparison operators (===  !==  >  >=  <  <=) and Logical operators (&& ||) allow us to compare two (or more) values or conditions.
  5. The else block with if statements, and default block with switch statements handle anything that does not match as true to the logic check.

Code Samples

//The structure of an if block...
if (thisValue === true) {
    //run this code in here
}
//If not true, ignore that block and keep going.

//Handling a false condition with an else block...
if (thisValue === true) {
    //run this code in here
    alert("It's true!");
} else {
    //run this code in here instead
    alert("It's NOT true!!")
}
//NOTE: Above, I did not have to say if(thisValue === false). The else block handled that condition.

//else if lets you do additional logic checks...
if (name === "Bernie"){
    alert("Hi Bernie");
} else if (name === "Joanna"){
    alert("Hi, Joanna!");
} else if (name === "Sean"){
    alert("Hi, Sean!");
} else {
    alert("I don't recognize your name");
}
//You don't have to use an else block, but if you do, it has to be last, acting as a catch-all.


//switch blocks are for checking set, known values. They take in a variable and compare it to each case until they find a match. If no match is found, the default block is executed.
switch( payForm ){
    case "CHEQUE":
      openChqInterface();
      break;

    case "CREDIT":
      openCreditInterface();
      break;

    default:
      alert("We do not accept that form of payment.");
      break;
}