15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Arrays 493<br />

A variation of the WHILE loop is the DO-WHILE loop, which looks like this:<br />

do {<br />

Command;<br />

} while (condition);<br />

The main difference between the two loops is that the WHILE loop may run<br />

zero or more times, but the DO-WHILE loop will always run at least once.<br />

Somewhere inside a WHILE and DO-WHILE loop, you must have a command<br />

that can change the condition from True to False; otherwise, the loop will<br />

never end, and your program will appear to hang or freeze.<br />

Book V<br />

Chapter 3<br />

JavaScript<br />

Creating Functions<br />

In JavaScript, every subprogram is a function that can return a value. (A<br />

function that returns a null value simply acts like a procedure in other <strong>programming</strong><br />

languages.) The <strong>for</strong>mat of a typical function looks like this:<br />

function functionname (Parameter list)<br />

{<br />

Commands;<br />

return value;<br />

}<br />

The two parts of a JavaScript function are<br />

✦ Parameter list: Defines any data and their data types that the function<br />

needs to work. If the function doesn’t need to accept any values, the<br />

parameter list can be empty.<br />

✦ Return: Defines a value to return.<br />

If a function doesn’t return a value or accept any parameters, it might look<br />

like this:<br />

function myfunction ()<br />

{<br />

Command;<br />

}<br />

Using Arrays<br />

JavaScript offers two ways to create an array. First, you can define an array<br />

and the elements inside that array by using square brackets like this:<br />

var myarray = [data1, data2, data3];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!