JS/JavaScript

[JS] 06. Loop

밍글링글링 2017. 8. 28.
728x90

06_Loop.html

<script>
for ( var i = 0; i < 5; i++ ) {
    // Logs "try 0", "try 1", ..., "try 4".
    console.log( "try " + i );
}

for (var i = 0, limit = 5; i < limit; i++) {
    // This block will be executed 5 times.
    console.log( "for " + i );
    // Note: The last log will be "Currently at 4".
}

var i = 0;
while ( i < 5 ) {
    // This block will be executed 5 times.
    console.log( "while " + i );
    i++; // Increment i
}

var i = -1;
while ( ++i < 5 ) {
    // This block will be executed 5 times.
    console.log( "while " + i );
}

do {
 // Even though the condition evaluates to false
 // this loop's body will still execute once.
 console.log( "Hi there!" );
} while ( false );

//Stopping a loop
for ( var i = 0; i < 10; i++ ) {
 if ( i>=5 ) {
  break;
 }
 console.log("hello");
}

//Skipping to the next iteration of a loop
for ( var i = 0; i < 10; i++ ) {
 if (i<5) {
  continue;
 }
 // The following statement will only be executed
 // if the conditional "something" has not been met
 console.log( "I have been reached" );
}
</script>

 

 

728x90

'JS > JavaScript' 카테고리의 다른 글

[JS] 08. Object  (0) 2017.08.28
[JS] 07. Array  (0) 2017.08.28
[JS] 05. Conditional_Code  (0) 2017.08.28
[JS] 04. Operator  (0) 2017.08.28
[JS] 03. Type  (0) 2017.08.28

댓글