I want to break my for loop that I have 10 numbers in a line.
for(var i = 100; i <=300; i++){
console.log(i);
}
I want to break my for loop that I have 10 numbers in a line.
for(var i = 100; i <=300; i++){
console.log(i);
}
I guess by "that I have 10 numbers in a line." you mean to break the line after 10 printed numbers. It seems not to be possible to log to the console without a linebreak, so you have to concatenate the output until 10 outputs are concatenated.
var line = ''; // initialize line variable
for(var i = 100; i <=300; i++) {
line += i // append current value to the line without printing it
if ((i%10) === 0) { // check, if the current iteration is dividable by 10
console.log(line); // output the collected output
line = '' // reset the line var
}
}
if (line !== '') console.log(line); // if the total count was not dividable
// by 10, output the left over
In response to your statement, "My loop starts from 100, when it gets to 110, it breaks and skip a line. And continue reading when it reaches 120, skips a line:
for(let i = 100; i <=300; i++){
if(i % 10 == 0) {
continue;
}
console.log(i);
}
A break
will exit the loop completely, a continue
will skip to the next iteration. This skips every loop iteration that's divisible by 10.
you can use if condition inside your for loop lets say if you want to break the loop after 10 numbers printed just add this line in your for loop
if(i==10)
{
break;
}
The break condition will bring you out of the for loop.