Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Directive break and continue

The Break Statement

It was used to "jump out" of a switch() statement.

The break statement can also be used to jump out of a loop.

The break statement breaks the loop and continues executing the code after the loop (if any):

for (i=0;i<10;i++)

{ if (i==3)

{ break; }

x=x + "The number is " + i + "<br>"; }

Since the if statement has only one single line of code, the braces can be omitted:

for (i=0;i<10;i++)

{ if (i==3) break;

x=x + "The number is " + i + "<br>"; }

The Continue Statement

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 3:

Example

for (i=0;i<=10;i++)

{ if (i==3) continue;

The break and the continue statements are the only JavaScript statements that can "jump out of" a code block.

Syntax:

break labelname;

continue labelname;

The continue statement (with or without a label reference) can only be used inside a loop.

The break statement, without a label reference, can only be used inside a loop or a switch.

With a label reference, it can be used to "jump out of"

 

 

39.The switch.The JavaScript Switch Statement

Use the switch statement to select one of many blocks of code to be executed.Syntax:

switch(n)

{ case 1: execute code block 1

break; case 2:

execute code block 2

break; default:

code to be executed if n is different from case 1 and 2 }

This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.Example:

Display today's weekday-name. Note that Sunday=0, Monday=1, Tuesday=2, etc:

var day=new Date().getDay();

switch (day)

{ case 0: x="Today it's Sunday";

break; case 1:

x="Today it's Monday";

break; case 2:

x="Today it's Tuesday";

break; case 3:

x="Today it's Wednesday";

break; case 4:

x="Today it's Thursday";

break; case 5:

x="Today it's Friday";

break; case 6:

x="Today it's Saturday";

break;

}

The result of x will be: Today it's Tuesday

 

 

Functions

A function is a block of code that will be executed when "someone" calls it:

Example

<!DOCTYPE html>

<html><head><script>

function myFunction()

{ alert("Hello World!");

} </script></head>

<body><button onclick="myFunction()">Try it</button>

</body></html>

JavaScript Function Syntax

A function is written as a code block (inside curly { } braces), preceded by the function keyword:



function functionname()

The function keyword must be written in lowercase letters, and the function must be called with the same capitals as used in the function name.

Calling a Function with Arguments

When you call a function, you can pass along some values to it, these values are called arguments or parameters.

These arguments can be used inside the function.

You can send as many arguments as you like, separated by commas (,)

myFunction(argument1,argument2)

Declare the argument, as variables, when you declare the function:

function myFunction(var1,var2)

{some code }

The variables and the arguments must be in the expected order. The first variable is given the value of the first passed argument etc.

Recursion

Recursion is a programming technique you can apply as a natural, simple solution to a whole range of situations that would otherwise be difficult to solve. It is a powerful technique, which unfortunately is not easy to understand at first.

A recursive function is the function which at some point of execution calls itself.

Look at the example below. The function DoSomething() calls itself, so it's a recursive function.

function DoSomething (n) { … DoSomething (n-1);}

The recursive algorithm usually has the following pattern:

if we reached the base case(s) solve the problem
else simplify the problem using recursion

There are two ways to code recursive functions in JavaScript: 1)by directly calling the function from within itself and second by using an indirect call.An example of how to call the function directly:

function recursMe(param) { if (param < 0) { //base case return -1; } else {//some code here recursMe(param);} }

2)Calling a javascript recursive function indirectly:

function recursMe(param) { if (param < 0) { //base case return -1; } else { //some code here setTimeout(“recursMe(“ + param + “)”, 1); } }

Date: 2016-01-03; view: 1016


<== previous page | next page ==>
Comparison operators, and logical values | Methods and properties.
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.01 sec.)