Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Control Flow
- Flow Control is important because it allows your program to follow a strict flow using if, if-else, or if-else-if-else statements. This shows how code will react or flow if a certain condition or conditions is or is not met.
- If Statement:
- if (condition){
- Execute code if statement is true
- }
- If-Else Statement:
- if (condition){
- Execute code if statement is true
- }
- else{
- Execute code if statement is false
- }
- If-Else If Statement:
- if (condition){
- Execute code if statement is true
- }
- else if (condition){
- Execute code if statement is true instead
- }
- else{
- Execute code if all statements are false
- }
- */
- function isRich(money) {
- if (money > 500.00) {//first condition
- return "I'm rich!";
- } else if (money < 500.00 && money > 100.00) { //second condition
- return "Get a job."
- } else { //default condition
- return "It's over."
- }
- }
- console.log(isRich(32.00)); //prints "It's over." Does not meet first two coniditions.
- /*
- Switch Statements:
- You can also use Switch Statements to control the flow of code when many routes depend on one condition.
- switch (expression)
- {
- case condition 1: statement(s)
- break;
- case condition 2: statement(s)
- break;
- ...
- case condition n: statement(s)
- break;
- default: statement(s)
- }
- */
- var Car = 'Subaru';
- switch (Car) {
- case 'Toyota':
- case 'Subaru':
- case 'Chevy':
- console.log('This car is within budget.');
- break;
- case 'Lexus':
- default:
- console.log('This car is outside of budget.');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement