JavaScript Syntax Basics-Control statements

Source: Internet
Author: User

Four JavaScript control statements

(a) If-else

The If-else statement is the most basic control statement in JavaScript, which allows you to change the order in which statements are executed.

Expressions must use relational expressions or logical expressions to make judgments, and the result of an expression is usually used as a Boolean value to judge.

The number of 0 or nonzero in the result of an expression is converted to false and true respectively.

The statement within the curly braces {} allows multiple lines to be included if only one statement allows the curly braces {} to be used.

(ii) switch basic format

switch (expression)

{

Case value 1: statement 1; Break ;

Case value 2: statement 2;break;

Case Value 3: statement 3;break;

Default: Statement 4;

}

Here the code can practice, note that if the switch is written directly x instead of parseint (x), it will only execute the default content!!

<! DOCTYPE html> withA JS page</title><script type= "Text/javascript" >functionguess () {varX=prompt ("Guess What::", 1); Switch(parseint (x)) { Case1: Break;  Case2: Break;  Case3: document.write ("Winner!");  Break; default: Alert ("Don ' t give up!"); }}guess ();//Call the Funtion,be careful on the capital and small</script>page Content</body>

(c) For loop

*for Loop Basic Format

for (initialize; condition; increment)

{

Statement 1; ...

}

Instance:

for (Var i=1;i<=7;i++)

{

document.write ("

document.write ("<br>");

}//Loop output H1 to H7 font size

(d) While

*while Loop Basic Format

while (condition)

{statement 1; ...}

Instance:

var I=1;

while (i<=7)

{

document.write ("

document.write ("<br>"); i++;

}//Loop output H1 to H7 font size

JavaScript Syntax Basics-Control statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.