JS Basics-Branching structure-loops-arrays

Source: Internet
Author: User

1. Branching structure
1.if structure
Grammar:
if (condition) {
statement block;
}
Attention:
1. If the condition is as Boolean as possible, if it is not Boolean, the following condition value will be treated as false
if (0) {}
if (0.0) {}
if ("") {}
if (undefined) {}
if (null) {}
if (NaN) {}
Ex
if (35) {...} True
The {} After 2.if can be omitted
When omitted, if only the first statement below is controlled
It is recommended not to omit {}
2.if...else ... Structure
Grammar:
if (condition) {
statement block;
}else{
statement block;
}
3.if...else If...else ... Structure
Grammar:
if (condition) {
Statement Block 1;
}else if (condition) {
Statement Block 2;
}else if (condition) {
Statement Block 3
.. s
}else{
statement block N;
}
Practice:
Enter a 1-7 number from the pop-up box, representing Monday to Sunday, respectively,
Input 1: Print out "Monday eat pork"
2: "Tuesday braised pork Ribs"
3: "Wednesday Eat braised Meatballs"
4: "Thursday Eat braised eggplant"
5: "Friday braised fish with brown sauce"
6 and 7: "Weekend weight Loss"
4.switch...case structure
1. Function: equivalent judgment (= = =)
2. Syntax:
switch (value/expression) {
Case value 1:
Statement Block 1;
Break End switch structure, optional
Case Value 2:
Statement Block 2;
Break
....
Default
statement block N;
Break
}
3. Special usage
When performing the same operation:
switch (value/expression) {
Case value 1:
Case Value 2:
Case Value 3:
statement block;
}
2. Cycle
1. Features
1. Cycle conditions: The start and end of a cycle
2. Loop operation: The same or similar statement to be executed
2. Cyclic-while
Grammar:
while (condition) {
Loop body-cyclic operation
Update loop condition
}
Problem: Print output 1-10 number

Exercise 1: Print all numbers between 1-100
3. Cycle Flow Control
1.break
Function: Terminates the operation of the entire cycle
2.continue
Function: Terminates the cycle and continues the next cycle

Problem:
The loop enters information from the pop-up box and prints until exit is entered.
Practice:
Calculates all the even numbers between 1-100 and
4. Cyclic-do...while
Grammar:
do{
Loop body
}while (conditions);
Execution process:
1. Perform the Loop body first
2. Re-judging the cycle conditions
If the condition is true, the loop body will continue to execute
If the condition is false, jump out of the loop operation
Practice: Revision Cycle entry operation
1. Loop through the data in the popup box and print it in the console
2. Enter exit (exit not output)
Exercise 2:
1. Randomly generate a number between 1-100
Math.random (); Generates a random number between 0-1
var r=parseint (Math.random () *100) +1;
2. Let the user enter a number
If the number entered is larger than the random number, the hint guessed big;
If the input number is smaller than the random number, the hint guess is small;
Otherwise, guess right!
Allow the loop to guess the number
3. Enter exit to exit the game.
5.for Cycle
1.ex:
var I=1; Expression 1
while (i<=100) {//Expression 2
Console.log (i);
i++; Expression 3
}
2.for syntax
for (expression 1; expression 2; expression 3) {
Loop body
}
Ex
for (Var i=1;i<=100;i++) {
Console.log (i);
}
Expression 1: Declaration of a cyclic condition
Expression 2: The judgment of the cyclic condition
Expression 3: Updating the loop variable
Execution process:
1. Execute expression 1 First
2. Determine the result of expression 2 (Boolean type)
3. If the condition is true, execute the loop body, or exit
4. After execution completes the loop body, then executes the expression 3
5. Judging the result of expression 2
Practice:
Define a function to print any row in the multiplication table
Pass the parameter, pass a few lines to print.
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
...
Ex: Pass 4, Print line 4th
Cycle conditions: From 1 onwards to line end
Cyclic operation: Cyclic variable *line= loop variable *line
3.for three special usages of expressions
for (expression 1; expression 2; expression 3) {}
1. Omitting an expression
Three expressions can be omitted arbitrarily, and semicolons are not omitted.
But be sure to complement the expression inside or outside the loop.
2. Expression 1 and expression 3 allow multiple expressions to be written, separated by commas between multiple expressions.
Ex
function sum () {
for (Var i=1,sum=0;i<=100;i++) {
Sum+=i;
}
Console.log (sum);
}
4. Loop nesting
1. Loop nesting
In one loop, another loop appears
for (Var i=1;i<=10;i++) {//Outer loop
for (Var j=1;j<=10;j++) {//Memory loop
Loop body
}
}
The outer loop walks once, the inner layer loops round
Exercise 1: Print in the console like
*
**
***
****
*****
Exercise 2: Print a 99 multiplication table
3. Arrays
1. What is an array
Save multiple data in a variable
Arrays are arranged in Linetype order-line structure
2. Declaring an array
1. Syntax
1.var array name =[];
Ex
var unames=[];
2.var array name =[element 1, Element 2, element 3];
var unames=["TOM", "Lilei", "Lucy";
3.var array name =new array ();
4.var array name =new Array (element 1, Element 2, element 3);
Ex
var names=new Array ("Lin Daiyu", "Jia Baoyu", "Granny Liu");
Practice:
1. Create an array that holds the names of the 3 learners
2. Create an array that holds the age of 3 trainees
3. In the console, print the values in the output array, respectively.
2. Use of arrays
Both the value and assignment operations are used: array name [subscript]
1. Assigning a value to an array element
Array name [subscript]= value;
2. Get the elements in the array
Array name [subscript];
3. Get the length of the array
Array Length: The number of elements in the array
Property: Length
Syntax: array name. length
1.length represents the length of the array, and it can also indicate the subscript of the element that is about to be inserted
Ex
var unames=["Tom", "Lucy", "Lilei"];
Unames[unames.length]= "Monkey King";
Unames[unames.length]=prompt ();

2. Iterate through each element in the array
for (Var i=0;i<=unames.length-1;i++) {
I: indicates subscript for each element
Unames[i]: Take each element
}

JS Basics-Branching structure-loops-arrays

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.