Thursday, November 10, 2016

Switch and Loop

Kevin Martinez
Web Client Programming
I.T.H.S
11/10/16

                                             JavaScript Switch Statement

            JavaScript Switch Statement is a code that we will use in JavaScript and it is used to perform different actions that is based on different conditions. We will use the Switch Statement to call on one of the blocks to be used. How to use Switch Statement is like this first, you will evaluated the expression once. Then the expression is going to be compared to the values you put in each case in the code. Lastly once there is a match between the expression and the case value then the block of code is going to be executed. When you use this code you got to make sure you use the word "break" in order for it to work. The keyword is break because once the code runs and it hits the work "break" it will break out of switch break and then the expression is going to compare with the case values to see if theres a match. if theres no match it will go on to the next break and repeat this process again until their is a match. Here is an example we got from w3schools (Image): --------------------------->
The example of the code Switch Statement would be used in situations like when you are trying to find out Today is...... and this code will give you the result Today is Thursday by looking and finding a match with the case.  


   JavaScript For Loops

   JavaScript for Loop is another code, JavaScript Loops can execute a block of code a number of times. When JavaScript Loops run it has the ability to run  the same code over and over again, each time with a different value. From reading the article in w3schools I learned that their are 4 different kinds of Loops the first is for - for loops through a block of code a number of times. The second one is for/in - for/in loops through the properties of an object. The third while - while loops through a block of code while a specified condition is true. The fourth do/while - do/while also loops through a block of code while a specified condition is true. These are the four different types of Loops I learned from reading the article. Here is an example from w3schools of 
JavaScript Loops: ----------------------------------> 
The code is being used the call up three things from var 1. cars , 2. text and , 3. i . All these var work together if for example you take out what is in var text then your code won't call var cars because for var cars to be called you need what's inside var text and for var tex to work you need what's inside var i , if you take out one of these var then your code won't work and noting would show up. 

No comments:

Post a Comment