Any code below the default statement will be executed if none of the other cases get a match. This keyword acts a lot like an ELSE in an if statement structure. If you examine the switch statement above you'll also notice the default keyword. Personally I've run into my fair share of bugs in programs I've written from forgetting to put break statements in. The break keyword tells JavaScript that we're done with the switch statement, and it should break out of it and move on. In the case of a match, I want the switch statement to stop checking the other cases. This is different from an ELSE IF structure where once a condition is true it stops checking the ones below.īecause JavaScript will automatically check every case no matter what, we need a way to prevent this (most of the time you want to prevent this). One thing that's important to keep in mind with switch statements is that by default, JavaScript will try to check all of them for equality, even if it gets a match. If there's a match then the code directly below the case statement will be executed. JavaScript will compare myGrade to each of the values in the case statements ('A' and 'F') for equality. These case statements are essentially the right side of a boolean expression for equality. Now that we have our value, we want to set up a series of case statements. Unlike if statements which need boolean expressions inside their parenthesis, with the switch statement we're just gonna pass in a value. Instead of resorting to an if statement we can employ the switch. Essentially we want to print out whether the student passed or failed given their grade (A or F). Take a look at a simple switch statement below to get a handle on the syntax in JavaScript:Īs you can see above, we start with a value myGrade. This structure allows you to easily map a value to a piece of code that should be executed. When one of these boolean expressions is true then a specific piece of code will execute. Programmers realized this and thus Switch Statements were born! Switch Statement StructureĪ switch statement takes a single value, then compares it to a bunch of other values using a boolean expression. ![]() Using an if statement, your program would soon become a mess of if statement syntax, and it wouldn't be very read-able or clear what's going on. Normally this could be covered with an if statement, but what if you want to account for 5, 10, even 20 possible situations? Sometimes, when infusing logic into your programs you'll run into a situation where given the value of a certain piece of data, you'll want to do something different. In fact, a switch statement is essentially a specialized version of an if statement which would otherwise have an excessive amount of ELSE IF type statements. Much like if statements, which give your program the ability to make decision, switch statements can be used to infuse a bit of intelligence into any program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |