This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Conditional statement

If statement

This is similar to Java, divided into:

  • If statement – Use this statement to execute code only if the specified condition is true

    if ( flag ) {
        // This command is executed when flag is true
    }
    Copy the code
  • if… Else statement – Executes code when the condition is true and other code when the condition is false

    if ( flag ){
      // This command is executed when flag is true
    }else {
      // This command is executed when flag is false
    }
    Copy the code
  • if… else if…. Else statement – Use this statement to select one of multiple code blocks to execute

    if ( flag ){
      // This command is executed when flag is true
    }else if ( flag2 ){
      // When flag2 is false
    }else {
      // Execute when both conditions are false
    }
    Copy the code

A switch statement

  • Switch statement – Use this statement to select one of multiple code blocks to execute

    Not every case needs to contain a break. If the case statement does not contain a break, the control flow will continue with subsequent cases until a break is encountered.

    switch(flag){
      case f1:
        // When flag=f1, the command is executed
        break;
      case f2:
        // The command is executed when flag is f2
       	break;
      default:
        // Execute when flag does not match in the above case
           }
    Copy the code

Looping statements

The for loop

  • Classical for loop

    for ( init; condition; increment ){
        statement(s);
    }
    
    / / sample
    var i:number =0;
    for (i ; i<10; i++){
        console.log('hello! ')}Copy the code

    It consists of three parts, init, condition, increment

    Init will be executed first and only once

    If the condition is true, the loop is executed. If it is not, the loop is closed. Each time the loop is executed, the condition is evaluated

    So once you’ve finished executing the code in the body of the loop you’re executing the increment statement, this can be empty, as long as you keep the last semicolon

  • for… In circulation

    for… The in statement is used to iterate over a collection or list of values.

    // List output
    var arr:number[] = [2.5.6.7]
    for (var index in arr){
      	// index The index starts from 0
        console.log(arr[index])
    }
    Copy the code
  • for… Of circulation

    for… The of statement creates a loop to iterate over iterable objects. Introduced in ES6 for… Of loop instead of for… In and forEach(), and support for the new iteration protocol. for… Of allows you to iterate through iterable data structures like Arrays, Strings, Maps, Sets, and so on.

    / / traverse map
    var map = new Map([['name'.'greycode'],
        ['desc'.'Working man']]);for (var key of map.keys()){
        console.log(map.get(key))
    }
    
    // Go through the group
    var arr:number[] = [2.5.6.7]
    for (var value of arr){
        console.log(value)
    }
    
    // Iterate over the string
    var n:string = 'greycode'
    for (var c of n){
        console.log(c)
    }
    Copy the code
  • The forEach loop

    The circulation body is not supported to break out of the circulation

    var arr:number[] = [2.5.6.7]
    arr.forEach((val,index,array) = >{
        console.log(val)
        console.log(index)
        console.log(array)
        }
    )
    Copy the code

    The above three parameters val,index, and array represent the value, the array index, and the array itself. You can you can omit the index and array parameters to query the value separately. For example:

    var arr:number[] = [2.5.6.7]
    arr.forEach((val) = >{
        console.log(val)
        }
    )
    Copy the code
  • Every cycle

    Supports the loop body to break out of the loop and must be set to return a Boolean value

     var arr:number[] = [2.5.6.7]
     arr.every((val) = >{
         console.log(val)
       	// The loop continues when true is returned, equivalent to continue
       	// Return false to end the loop, equivalent to break
       	// The loop ends when no Boolean value is returned
         return true})Copy the code
  • Some cycle

    The loop body can break out of the loop, but unlike every, the Boolean value is optional. The Boolean value has the opposite meaning of the every loop

    • Returning true equals break and ends the loop
    • Returning false is equivalent to continue, entering the next loop
    var arr:number[] = [2.5.6.7]
    arr.some((val) = >{
        console.log(val)
        }
    )
    Copy the code

The while loop

The basic syntax is as follows:

while(flag)
{
   // This command is executed when flag is true
}

Copy the code

do… The while loop

Unlike while, it determines the loop condition at the tail, so the do block is executed first

do
{
   // Execute the code block
}while( flag );

// When flag is true, the do block is executed again
Copy the code

An infinite loop

// for infinite loop
for(;;) { 
   / / statements
}

// While infinite loop
while(true) { 
   / / statements
} 

Copy the code