Three structures in the control flow of JavaScript programs (1)

Source: Internet
Author: User

Are you familiar with the use of the control flow of JavaScript programs? Here we will describe the common control flow structures and statements of JavaScript programs, including the if condition statements, for statement and while LOOP statement.

JavaScript Program Control Flow

In any language, the control flow of a program is required. It can reduce the entire program and make it run smoothly in a certain way. The following are common JavaScript program control flow structures and statements:

1. if Condition Statement

Basic Format:

If Statement)

Statement segment 1;

......

Else

Statement Segment 2;

.....

Function: If the expression is true, statement segment 1 is executed; otherwise, statement Segment 2 is executed.

NOTE: if-else statements are the most basic control statements in JavaScript. They can be used to change the execution sequence of statements. The relational statement must be used in the expression for judgment. It is used as a Boolean value.

To estimate. It converts zero and non-zero numbers to false and true, respectively. If the statement after if has multiple rows, you must enclose it with curly brackets.

Nested format of if statement:

If Boolean) Statement 1;

Else Boolean value) Statement 2;

Elseif Boolean value) Statement 3;

......

Else Statement 4;

In this case, the Boolean expression of each level is calculated. If it is true, the corresponding statement is executed. Otherwise, the else statement is executed. Let's look at an instance.

Ifyuju.htm

 
 
  1. <Html>
  2. <Head>
  3. <Title> JavaScript program control flow If statement applet </title>
  4. <Script
  5. Language = "JavaScript">
  6. Varactivedate = newDate ();
  7. Activehour = activedate. getHours ()
  8. If (activehour <12 ){
  9. Document. write ("good morning" + "<br> ")
  10. } Else {
  11. Document. write ("Good afternoon" + "<br> ")
  12. }
  13. </Script>
  14. </Head>
  15. <Body>
  16. </Body>
  17. </Html>

Note: In this example, we first define a variable activedate and use the newDate function to get the current time, and then use the variable activehour to get the current hour, compare with 12 to determine whether it is "Morning" or "Afternoon" and then use the If statement to execute the corresponding statement program, that is, "good morning" or "Good afternoon ".


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.