Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /*WHILE Statements
- While (expression)
- statement
- A simple Eg:
- */
- var count = 0;
- while(count < 10) {
- console.log(count);
- count++;
- }
- /*DOWHILE
- do
- statement
- while (expression)
- In this the body is executed once then expression is ran
- Eg:
- */
- function printArray(a) {
- if(a.length==0)
- console.log("Empty Array");
- else {
- var i=0;
- do {
- console.log(a[i]);
- } while(++i < a.length);
- }
- }
- a = ["First Element","Second Element",5,10];
- printArray(a); //prints the array contents, otherwise will just print "Empty Array"
- /*FOR Statements
- For(initialise ; test ; increment)
- statement
- Route of passage is like this:
- initialise
- while(test) {
- statement;
- increment;
- }
- Rules
- You can use var in for loops as initialising
- Test statement is processed before each loop
- The increment expression then performed. Usually ++ -- or an assignment of sorts
- You would rewrite the 1st while program like so
- */
- var sum = 0;
- for(i=0 ; i<10 ; i++) {
- console.log(i);
- }
- //Or you could do more complex for loop
- for(i=0,j=10; i<j ; i++) {
- sum += i+j;
- console.log(sum); //prints with each iteration as i increases is added to previous value 10,21,33,46,60,75,91,108,126,145
- }
- /*FOR IN Statements
- For(variable in object)
- statement
- You can display an objects value of its property using object[variable name]
- For loop to display objects names and properties Eg:
- */
- {
- var colour,diameter=100,size=(Math.PI * Math.pow((diameter/2), 2)).toFixed(2);
- }
- var ball = {
- 'colour' : "Blue",
- 'diameter': diameter,
- 'size' : size
- };
- //***VERY USEFULL***
- for (var property in ball) {
- console.log("name: "+property+"; value: "+ball[property]);
- }
- var myObject = {x:10, y:23, z:32};
- var myArray = [];
- var k = 0;
- for(myArray[k++] in myObject) { //Adds the objects properties to the array
- }
- console.log(myArray);
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/*WHILE Statements
- While (expression)
- statement
- A simple Eg:
- */
- var count = 0;
- while(count < 10) {
- console.log(count);
- count++;
- }
- /*DOWHILE
- do
- statement
- while (expression)
- In this the body is executed once then expression is ran
- Eg:
- */
- function printArray(a) {
- if(a.length==0)
- console.log("Empty Array");
- else {
- var i=0;
- do {
- console.log(a[i]);
- } while(++i < a.length);
- }
- }
- a = ["First Element","Second Element",5,10];
- printArray(a); //prints the array contents, otherwise will just print "Empty Array"
- /*FOR Statements
- For(initialise ; test ; increment)
- statement
- Route of passage is like this:
- initialise
- while(test) {
- statement;
- increment;
- }
- Rules
- You can use var in for loops as initialising
- Test statement is processed before each loop
- The increment expression then performed. Usually ++ -- or an assignment of sorts
- You would rewrite the 1st while program like so
- */
- var sum = 0;
- for(i=0 ; i<10 ; i++) {
- console.log(i);
- }
- //Or you could do more complex for loop
- for(i=0,j=10; i<j ; i++) {
- sum += i+j;
- console.log(sum); //prints with each iteration as i increases is added to previous value 10,21,33,46,60,75,91,108,126,145
- }
- /*FOR IN Statements
- For(variable in object)
- statement
- You can display an objects value of its property using object[variable name]
- For loop to display objects names and properties Eg:
- */
- {
- var colour,diameter=100,size=(Math.PI * Math.pow((diameter/2), 2)).toFixed(2);
- }
- var ball = {
- 'colour' : "Blue",
- 'diameter': diameter,
- 'size' : size
- };
- //***VERY USEFULL***
- for (var property in ball) {
- console.log("name: "+property+"; value: "+ball[property]);
- }
- var myObject = {x:10, y:23, z:32};
- var myArray = [];
- var k = 0;
- for(myArray[k++] in myObject) { //Adds the objects properties to the array
- }
- console.log(myArray);
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement