Eighth Course focus (JS statement: conditions, loops)

Source: Internet
Author: User

1. Conditional statement:

(1) comparison operator:

= =: Judging whether it is equal

! =: Judging if not equal

>: Judging whether it is greater than

>=: Judging if it is greater than or equal

<: Judging if it is less than

<=: Judging if it is less than or equal


"Example 1"

if (inum1==inum2)//If INUM1 is equal to iNum2
{
INUM3=INUM1; Assigns the value of the INUM1 to INUM3
}
else{//otherwise
inum3=inum2; Assigns the value of the iNum2 to INUM3
}


"Supplemental" can also be written:

INUM3 = inum1==inum2? Inum1:inum2


This is a three-mesh operator with the following structure:

A = judgment? Value 1: Value 2

This means that the "judgment" is judged first, if the result is true, then the a= value is 1; if False, the A= value is 2.


Therefore, the formula can be understood as, first judge whether "Inum1==inum2" is true: if true, then inum3=inum1; if False, then inum3=inum2.

(2) Logical operators:

&&: With operations (a && b:a and B, which represent both A and B, the judgment is true)

|| : OR operation (A | | B:a or B, which means satisfying one of the judgements as long as it satisfies a or satisfies B)

! : Non-operation (!a: Non-A, opposite to a, meaning that if a is true, then!a is false; A is false, then!a is true. )


"Example 2"

if (inum1>inum2 && inum1>inum3)//When inum1>inum2 and inum1>inum3 are satisfied at the same time,
{
Alert ("First value maximum"); Output "first value Max"
}
if (inum1>inum2 | | inum1>inum3)//When inum1>inum2 or INUM1>INUM3,
{
Alert ("The first value is not a minimum"); Output "The first value is not a minimum"
}
if (! ( inum1>inum2 | | INUM1>INUM3))//Take (inum1>inum2 | | inum1>inum3) the opposite meaning,
{
Alert ("First value maximum"); Output "First value min"
}

(3) If statement:

if (condition i)
{
...... Behavior One
}

else if (condition two)
{
...... Act II
}

else if (condition three)
{
...... Behavior Three
}

......

Else
{
...... Behavior N
}


This statement can be interpreted as:

If it is "condition one", then "act One";

In addition, if it is "condition two", then "Act Two";

In addition, if it is "condition three", then "act three";

...... (else if can have more than one)

Except for the above conditions (neither "condition one" nor "condition two" nor "condition three" ...). ), "Behavior n" is performed.


Added


if (condition i)
{
...... Behavior One
}

if (condition two)
{
...... Act II
}

Else
{
...... Behavior Three
}

If the if is followed by else, then this else is only for the most recent if.

This statement can be interpreted as:

If it is "condition one", then "act One";

If it is "condition two", then "Act Two";

In addition, "behavior three" (where else is irrelevant to the first if) except for "condition two"


2. Popup box:

(1) message box:alert ()


(2) Input box:prompt ()

* Input in the input box, the program is read as a string.

"Example 3"

var sinput=prompt ("Please enter a number between 1-10"); In the pop-up input box, enter the string and deposit in Sinput


(3) Inquiry box:confirm ()

* Pop up a query box with "OK" and "Cancel" buttons. The result returned is a Boolean type, True or false.

"Example 4"

if (Confirm ("Are you sure you like blue?") ")//Inquiry box ask for content
{
Alert ("You like Blue"); Select OK to eject this box
}
else{
Alert ("You Don't like Blue"); Select "Cancel" to eject this box
}


(4) string to numeric: Number()

* The contents of input boxes are read in string form. What if we enter a number and want to read it digitally? You can use number ()

"Example 5"

var sinput=prompt ("Please enter a number between 1-7");
var ninput=number (sInput);

The purpose of this example is to read the string information entered in the input box into the sinput, and then use number () to convert the string information in the Sinput into numbers and store it in Ninput.


(5) Judging whether it is a number:IsNaN ()

* Use number () can only be turned to numbers, so we need to use a statement to determine whether we enter a number, at this time with isNaN ()

IsNaN () means: is not a number, ' not a digit '.


"Example 6"

var sinput=prompt ("Please enter a number between 1-7");

var ninput=number (sInput);

if (IsNaN (ninput))//If Ninput is not a number
{
Alert ("You're not typing a number"); The output "You are not entering a number"
}
else//otherwise
{
Alert ("You have entered a number"); Output "You entered a number"
}

(6) On this basis, you can also determine whether the input is an integer, at this time with parseint (), converted to an integer:

"Example 7"

var sinput=prompt ("Please enter a number between 1-7");

var ninput=number (sInput);

if (IsNaN (ninput))
{
Alert ("You're not typing a number");
}
Else
{
if (Ninput==parseint (ninput))//Determine the value to be converted to a number equal to the value it takes after rounding
{
Alert ("You entered an integer");
}
Else
{
Alert ("You are not entering an integer");
}

}


(7) On this basis, you can also determine whether the input is between the 1-7 range, this time with a logical operator :

"Example 8"

var sinput=prompt ("Please enter a number between 1-7");

var ninput=number (sInput);

if (IsNaN (ninput))
{
Alert ("You're not typing a number");
}
Else
{
if (Ninput==parseint (ninput))
{
if (ninput>7 | | ninput<1)//Judging if this number is greater than 7 or less than 1
{
Alert ("You entered is not between 1-7")//Output This box
}
}
Else
{
Alert ("You are not entering an integer");
}

}

(8) On this basis, you can also determine the value of each input corresponding to perform a behavior, with switch. Case:

"Example 9"

Switch (ninput)//Determine the value of Ninput
{
Case 1://if 1

Alert ("Monday"); Then output "Monday"

Break Then jump out of the loop


Case 2://If 2

Alert ("Tuesday"); Then output "Tuesday"

Break Then jump out of the loop


Case 3://etc
Alert ("Wednesday");
Break

Case 4:
Alert ("Thursday");
Break

Case 5:
Alert ("Friday");
Break

Case 6:
Alert ("Saturday");
Break

Case 7:
Alert ("Sunday");
Break

Default://default is to execute a case without a match , default is not required

Statement

Break

}

* with switch. The advantage of case is that it is more efficient to exit the loop once you have to judge it once.


"Supplemental" break is the meaning of jumping out of the loop.

3, the Loop statement:


(1) while

While (..) When... When
{
.. Do this
}


(2)Do...while

Do
{
.. Do this
}
While (..) When... When


* The above two types, are when ... When the ... Operation


"Example 10"

var I=1;

var isum=0;

while (i<101)//When i<101
{
Isum+=i; Isum=isum+i

i++; I=i+1
}
alert (isum);

Interpretation:

Answer: isum=1+2+3+. +100=5050



(3) forloop : This loop uses the most

"Example 11"

var isum=0;

for (Var i=1;i<101;i++)
{
Isum+=i;
}
alert (isum);

Interpretation: The same example 10

(4)break: Jump out of the entire for loop body

"Example 12"

var isum=0;

for (Var i=1;i<101;i++)
{
if (i==5)
{
Break
}
Isum+=i;
}
alert (isum);

Interpretation: When i==5, break jumps out of the for loop body, so the isum=1+2+3+4=10 is directly output


(5)continue: Jump out of the loop under current conditions and proceed backwards

"Example 13"

var isum=0;

for (Var i=1;i<101;i++)
{
if (i==5)
{
Continue
}
Isum+=i;
}
alert (isum);

Interpretation: When i==5, continue jump out of this cycle, directly into the next loop i=6, so isum=1+2+3+4+6+......+100=5045


(6)return: There are two ways to use


The first is: End the entire method body function{}

"Example 14"

function

{

var isum=0;

for (Var i=1;i<101;i++)
{
if (i==5)
{
Return
}
Isum+=i;
}
alert (isum);

}

Interpretation: Directly out of the method body, directly appear at the end of the}, so it is not executed.

The second type is: Returns a value

"Example 15"

var isum=0;

for (Var i=1;i<101;i++)
{
if (i==5)//i=5,
{
I=b (i); I= calls the value of method B (i) =10
}
Isum+=i;
}
alert (isum);

}

function B (i)
{
i+=5; i=i+5=10
return i;
}

Interpretation: When i=5, the value returned by the i= call method = 10, so isum=1+2+3+4+10+11+12+. +100


(7) 99 multiplication table:

    

Eighth Course focus (JS statement: conditions, loops)

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.