Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="description" content="[Control Flow]">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>Control Flow</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /* Control Flow is about *controlling* the *flow* of activity within our
- * code through conditional statements. There are several types of conditional
- * statements; let's go through them together! */
- // Let's start with the if statement; it's pretty simple.
- // An if statement executes a block of code **if** a certain condition is true.
- // Example:
- var ageDavid = 33;
- var ageClassAvg = 25;
- if (ageDavid > ageClassAvg) {
- itsTrue = "David is an old fogey";
- }
- console.log(itsTrue); // prints "David is an old fogey" --- hey, watch it
- // But if the if statement isn't true, it throws an error.
- var heightDavid = 72;
- var heightClassAvg = 68;
- if (heightDavid < heightClassAvg) {
- heShort = "David is also teeny-tiny";
- }
- console.log(heShort); // throws an error
- // This is where an if-else statement comes in handy...it gives you multiple
- // options for controlling that flow!
- if (heightDavid < heightClassAvg) {
- heShort = "David is also teeny-tiny";
- }
- else if (heightDavid > heightClassAvg) {
- heShort = "At least David is tall"
- }
- console.log(heShort); // prints "At least David is tall"
- // if-else statements can provide several options for which code to execute.
- var timeFly; // minutes until flight leaves
- timeFly = 20;
- if (timeFly > 180) {
- dadMood = "Not worried yet";
- }
- else if (timeFly > 60) {
- dadMood = "We need to get to the airport...";
- }
- else if (timeFly > 30) {
- dadMood = "We're gonna miss our flight!";
- }
- else if (timeFly > 10) {
- dadMood = "Two thousand dollars worth of tickets, down the drain";
- }
- else if (timeFly >= 0) {
- dadMood = "Next time, I'll just go by myself";
- }
- console.log(dadMood);
- // prints "Two thousand dolars worth of tickets, down the drain"
- /* The very last "else if" in the example above would be better written as an
- * else statement. An else statement essentially examines all the if and else
- * if conditions and tells the computer, "If none of the above is true, just do this."
- *
- * The last else if could be written:
- * else {
- * dadMood = "Next time, I'll just go by myself
- * }
- *
- * Here's a full-blown example: */
- var numCats; // Number of cats someone owns
- numCats = 3
- if (numCats === 0) {
- friendsThink = "General concern";
- }
- else if (numCats === 1) {
- friendsThink = "A pet is good when you're going through a break up";
- }
- else if (numCats === 2) {
- friendsThink = "Well they can each have a friend!";
- }
- else if (numCats === 3) {
- friendsThink = "Should we be worried?";
- }
- else {
- friendsThink = "We should recommend a therapist.";
- }
- console.log(friendsThink); // prints "Should we be worried?"
- // A switch statement is a more elegant way of writing out an if-else if-else
- // statement. It relies on cases, breaks, and a default rather than conditionals.
- // I'm sticking with if-else structure for now because you never forget your
- // first love. But here's a switch example for when hit mid-life crisis stage:
- var dessert = "cookies";
- switch(dessert) {
- case "cake":
- console.log("Is it someone's birthday?");
- break;
- case "cookies":
- console.log("Get some milk");
- break;
- case "ice cream":
- console.log("Don't let it melt");
- default:
- console.log("No dessert, I'm on a diet")
- }
- //prints "Get some milk"
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/* Control Flow is about *controlling* the *flow* of activity within our
- * code through conditional statements. There are several types of conditional
- * statements; let's go through them together! */
- // Let's start with the if statement; it's pretty simple.
- // An if statement executes a block of code **if** a certain condition is true.
- // Example:
- var ageDavid = 33;
- var ageClassAvg = 25;
- if (ageDavid > ageClassAvg) {
- itsTrue = "David is an old fogey";
- }
- console.log(itsTrue); // prints "David is an old fogey" --- hey, watch it
- // But if the if statement isn't true, it throws an error.
- var heightDavid = 72;
- var heightClassAvg = 68;
- if (heightDavid < heightClassAvg) {
- heShort = "David is also teeny-tiny";
- }
- console.log(heShort); // throws an error
- // This is where an if-else statement comes in handy...it gives you multiple
- // options for controlling that flow!
- if (heightDavid < heightClassAvg) {
- heShort = "David is also teeny-tiny";
- }
- else if (heightDavid > heightClassAvg) {
- heShort = "At least David is tall"
- }
- console.log(heShort); // prints "At least David is tall"
- // if-else statements can provide several options for which code to execute.
- var timeFly; // minutes until flight leaves
- timeFly = 20;
- if (timeFly > 180) {
- dadMood = "Not worried yet";
- }
- else if (timeFly > 60) {
- dadMood = "We need to get to the airport...";
- }
- else if (timeFly > 30) {
- dadMood = "We're gonna miss our flight!";
- }
- else if (timeFly > 10) {
- dadMood = "Two thousand dollars worth of tickets, down the drain";
- }
- else if (timeFly >= 0) {
- dadMood = "Next time, I'll just go by myself";
- }
- console.log(dadMood);
- // prints "Two thousand dolars worth of tickets, down the drain"
- /* The very last "else if" in the example above would be better written as an
- * else statement. An else statement essentially examines all the if and else
- * if conditions and tells the computer, "If none of the above is true, just do this."
- *
- * The last else if could be written:
- * else {
- * dadMood = "Next time, I'll just go by myself
- * }
- *
- * Here's a full-blown example: */
- var numCats; // Number of cats someone owns
- numCats = 3
- if (numCats === 0) {
- friendsThink = "General concern";
- }
- else if (numCats === 1) {
- friendsThink = "A pet is good when you're going through a break up";
- }
- else if (numCats === 2) {
- friendsThink = "Well they can each have a friend!";
- }
- else if (numCats === 3) {
- friendsThink = "Should we be worried?";
- }
- else {
- friendsThink = "We should recommend a therapist.";
- }
- console.log(friendsThink); // prints "Should we be worried?"
- // A switch statement is a more elegant way of writing out an if-else if-else
- // statement. It relies on cases, breaks, and a default rather than conditionals.
- // I'm sticking with if-else structure for now because you never forget your
- // first love. But here's a switch example for when hit mid-life crisis stage:
- var dessert = "cookies";
- switch(dessert) {
- case "cake":
- console.log("Is it someone's birthday?");
- break;
- case "cookies":
- console.log("Get some milk");
- break;
- case "ice cream":
- console.log("Don't let it melt");
- default:
- console.log("No dessert, I'm on a diet")
- }
- //prints "Get some milk"</script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement