CONDITIONAL
- if-else
- if-else if-else
- switch
LOOP
repetition a set of actions until a certain condition fails
1. for loop
Example 1:
for(var a = 0; a <= 10; a++){
console.log(a);
}
Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Example 2:
for(var a = 2; a <= 10; a+= 2){
console.log(a);
}
Output: 2, 4, 6, 8, 10
Example 3:
for(var a = 1; a <= 10; a+= 2){
console.log(a);
}
Output: 1, 3, 5, 7, 9
Example 4:
for(var a = 1; a > 0; a++;){
console log(a);
}
Output: unlimited, chances of computer crash
2. while loop: condition check first
Example
var i = 0;
a `while..true` loop would run forever, right?
while (true) {
stop the loop?
if ((i <= 10) === false) {
break;
}
console.log( i );
i = i + 1;
}
0 1 2 3 4 5 6 7 8 9 10
Comments
Post a Comment