Thursday, November 10, 2016

Switch and Loops Reflection Journal

JavaScript Switch Statement:





    The switch statement is used to perform different actions based on different cases. The value is compared to the values of each case. You can have as many cases as you want for the switch statement. The switch statement has a default in case if none of the cases are applied. You have to be careful when writing your code because it is case sensitive so you have to make sure that it is written exactly as it should be like for these statements in all lowercase. The default for the switch statement does not necessarily have to be the last case in the switch statement, though it is preferred. Also the break in the switch statement tells the computer to stop testing the case after it gets the match and saves a lot of time. The switch expression is evaluated at once.
This is the syntax of the switch statement:
switch(expression) {
case n:
code block break;
case n:
code block break;
default:
default code block
}



JavaScript for Loop:











     
A loop executes a block of code for a certain number of times. It is useful when you want to repeat the same code over again but for a different values as in an array. There are 4 types of loops: for, for/in, while, and do/while. The for loop is used when you want to execute a block of code a number of times. The for/in loop is used when you want to loop through the properties of an object and the while loop is used when you want to loop through a block of code while a specified condition is true. The do/while loop is the same as the the while loop. 
The for loop syntax is:

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

The while loop syntax is:

while(condition) {
code block to be executed
}

The do while loop syntax is:

do {
code block to be executed
}
while(condition)


No comments:

Post a Comment