JavaScript- condition cycle
Conditional statement
- if statement - only if a specified condition is true when the statement is used to execute the code
if ( expression )
{
code is executed when the condition is true
}
- if ... else statement - if the condition is true execute code, if the condition is false when executing other code
if ( expression )
{
code is executed when the condition is true
}
the else
{
code that when executed condition is not true
}
- if ... else if .... else statement - use this statement to select one of the plurality of code blocks is performed
(IF Expression 1)
{
if Condition 1 is code that executes when true
}
the else IF ( expression 2)
{
if condition 2 is true if the code is executed
}
the else
{
executed when condition 1 and condition 2 is true when no Code
}
- S Witch -case statement - use this statement to select one of the plurality of code blocks is performed
default keyword matching provision does not exist
break exit switch statement, or the statement will be executed next
switch(n) {
case 1: the implementation of the code block 1 break;
case 2: execute code block 2 break;
default: the case 1 and the case 2 is not performed at the same time the code
}
loop statement
- for - a certain number of cycles code block, BREAK exit the loop
for ( statement 1; statement 2; statement 3) { code block is executed }
// Statement 1 : variable initialization
// Statement 2 : cycling conditions
// Statement 3: update after executing the expression
- for / in - Attribute loop through the object
var person={fname:"John",lname:"Doe",age:25}; for (x in person) // x 为属性名 { txt=txt + person[x]; }
- the while - when a specified condition is true when the specified code block cyclic
while ( condition ) { code needs to be executed }
- do / while - also when a specified condition is true code blocks specified cycle time
do { code needs to be executed } the while ( condition );
note:
do-while the first iteration of the loop, determines whether or not the condition is satisfied. The first pass through the loop to ensure
while after the first execution determination
Break and continue difference
Break ( exit nearest cycle ):
With a break can flow out statement switch statement thereof, can also use the break statement to terminate the loop layer of the loop structure so that early termination of the present cycle layer.
( 1 ) in the loop body and can switch using the statement vivo BREAK ;
( 2 ) when a break occurs in the body of the loop switch when the statement in vivo, but out of the function switch statement body does not terminate the execution cycle thereof. To forcibly terminates the execution of the loop body can be, but not in the body of the loop switch is provided statement break statement, satisfies certain conditions of the loop out of this layer.
Label:
Cycling conditions
Break label;
Such as:
Lanyuan: // At this point label name lanyuan
for {
for i := 0; i <= 10; i++ {
if i > 5 {
// The outer loop is a loop, using lebel can jump when the lebel layer, so out of an endless loop , jump directly below OK
break lanyuan;
}
}
}
Continue ( skip the next iteration of this cycle to enter ):
continue statement is used to skip the rest of this statement has not been performed in the loop, the next condition determination cycle immediately, only to be understood that the present cycle is ended.
Note: the Continue statement did not make the entire loop terminates
Ignore the current cycle:
Label:
Cycling conditions
Continue label;
Such as:
Lanyuan: // At this point label name lanyuan
for {
for i := 0; i <= 10; i++ {
if i > 5 {
// The outer loop is a loop, using lebel can jump when the lebel layer, so out of an endless loop , jump directly below OK
Continue lanyuan;
}
}
}
Other statements
prompt () statement
- You can pop up a message box with a text box. A prompt dialog box
- The user input section in the text box, the function expects a character as an argument, the text string as a hint of the balloon.
- Content input by the user will be returned as the return value of the function, you can define a variable to hold data.
- Return value string type
- Statement:
var x = prompt ( "title text", "prompt text");
console.log(x);
Timing Console.time
Console.time ( timer name ) to start timing
Console.timeEnd (timer name) has timed out
Math.sqrt ( values ) for prescribing