Thursday, November 10, 2016

Switch and Loops

     In JavaScript, there are if/else statements and there are switch statements. Switch statements are similar to if/else statements. Both execute actions in which when another event or condition occur to cue it to execute. But unlike if/else statements, switch statements have a default value in which codes executes when nothing happens. Then they have case values that can be as much as you want, which are executed when a case is true. The syntax of a switch statement is:

switch(expression) {
    case n:
        code block        break;
    case n:
        code block        break;
    default:
        default code block
}.


This switch statement is evaluated once. The values of the expressions are compared with the values of each case then the case with the block of code that matches the values executes. Within the end of each case, there will be a "break" keyword to break out of the switch block so it would stop looking at more cases. Then there will be a case all the way on the bottom of all cases with a "default" keyword that will be executed when no cases match. Sometimes, the cases can have the same code block, and the default case does not always have to be at the bottom.

Exercise 1:



in this exercise, it is missing a case for "Apple", so I added a case for it and added the text: " An apple a day keeps you healthy!"

Exercise 2:



in this exercise, it is missing a default case that executes when a unknown fruit is typed into text box, so I added the default case with the text "I have never heard of the fruit".

Exercise 3:



in this exercise, there are syntax errors in the switch statement. It is missing the ":" after the cases and the "break" at the end of each case. After I added the missing parts, it works.

Exercise 4:



In this exercise, I am requested to add 3 cases and a default case shown in the image above.


      Loops are for running the same codes over and over again with different values each time. There are different Kinds of Loops that JavaScript supports. There are for, for/in, while, and do/while loops. for loops through a block of code a number of times. for/in loops through the properties of an object. while loops through a block of code while a specified condition is true. do/while also loops through a block of code while a specified condition is true. The for loop is the most common and have a syntax of:

for (statement 1; statement 2; statement 3) {
    code block to be executed
}


where statement 1 is executed before the code block began, it is to initialize the variable used in the loop, but it doesn't have to be, it's optional, and it can be more than one values. Statement 2 defines the condition for running the loop, it is also optional, and the loop will run until this statement returns false. And Statement 3 is executed each time after the loop has been executed, it is most of the time increasing the value of the initial value, but it is also optional and it can be adding or subtracting to do any math basically.


Exercise 1:


Here, I replaced the values in statement 1 and 3 to make it execute number from 0 to 10.

Exericise 2:



Here, I change the value of statement 1 from 0 to 5.

Excercie 3:



Here, I changed the value of statement 1 into 5 from 0, then change statement 2 from i < 10 to i <= 50, and statement 3 from i++ to i = i + 5.

Exercise 4:


Here, I changed statement 1 from i=0 to i=10, statement 2 from i < 10 to i> 0, and statement 3 from i++ to i--.

Excercise 5:



Here, I added all the statements shown above.

Exercise 6:



Here, I have to create the whole loop from scratch with the instruction give as shown in the image above. 


No comments:

Post a Comment