Skip to content

Commit ad1e5ce

Browse files
Added Files
1 parent 24e2c35 commit ad1e5ce

File tree

40 files changed

+1517
-2
lines changed

40 files changed

+1517
-2
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
In this tutorial, you will learn about the break statement with the help of examples.
3+
4+
The break statement is used to terminate the loop immediately when it is encountered.
5+
6+
The syntax of the break statement is:
7+
8+
break [label];
9+
Note: label is optional and rarely used.
10+
11+
Working of JavaScript break Statement
12+
Working of break statement in JavaScript
13+
Working of JavaScript break Statement
14+
Example 1: break with for Loop
15+
// program to print the value of i
16+
for (let i = 1; i <= 5; i++) {
17+
// break condition
18+
if (i == 3) {
19+
break;
20+
}
21+
console.log(i);
22+
}
23+
Run Code
24+
Output
25+
26+
1
27+
2
28+
In the above program, the for loop is used to print the value of i in each iteration. The break statement is used as:
29+
30+
if(i == 3) {
31+
break;
32+
}
33+
This means, when i is equal to 3, the break statement terminates the loop. Hence, the output doesn't include values greater than or equal to 3.
34+
35+
Note: The break statement is almost always used with decision-making statements. To learn more, visit JavaScript if...else Statement.
36+
37+
To learn more about for loop, visit JavaScript for loop.
38+
39+
Example 2: break with while Loop
40+
// program to find the sum of positive numbers
41+
// if the user enters a negative numbers, break ends the loop
42+
// the negative number entered is not added to sum
43+
44+
let sum = 0, number;
45+
46+
while(true) {
47+
48+
// take input again if the number is positive
49+
number = parseInt(prompt('Enter a number: '));
50+
51+
// break condition
52+
if(number < 0) {
53+
break;
54+
}
55+
56+
// add all positive numbers
57+
sum += number;
58+
59+
}
60+
61+
// display the sum
62+
console.log(`The sum is ${sum}.`);
63+
Run Code
64+
Output
65+
66+
Enter a number: 1
67+
Enter a number: 2
68+
Enter a number: 3
69+
Enter a number: -5
70+
The sum is 6.
71+
In the above program, the user enters a number. The while loop is used to print the total sum of numbers entered by the user.
72+
73+
Here the break statement is used as:
74+
75+
if(number < 0) {
76+
break;
77+
}
78+
When the user enters a negative number, here -5, the break statement terminates the loop and the control flow of the program goes outside the loop.
79+
80+
Thus, the while loop continues until the user enters a negative number.
81+
82+
To learn more about while loop, visit JavaScript while loop.
83+
84+
break with Nested Loop
85+
When break is used inside of two nested loops, break terminates the inner loop. For example,
86+
87+
// nested for loops
88+
89+
// first loop
90+
for (let i = 1; i <= 3; i++) {
91+
92+
// second loop
93+
for (let j = 1; j <= 3; j++) {
94+
if (i == 2) {
95+
break;
96+
}
97+
console.log(`i = ${i}, j = ${j}`);
98+
}
99+
}
100+
Run Code
101+
Output
102+
103+
i = 1, j = 1
104+
i = 1, j = 2
105+
i = 1, j = 3
106+
i = 3, j = 1
107+
i = 3, j = 2
108+
i = 3, j = 3
109+
In the above program, when i == 2, break statement executes. It terminates the inner loop and control flow of the program moves to the outer loop.
110+
111+
Hence, the value of i = 2 is never displayed in the output.
112+
113+
JavaScript Labeled break
114+
When using nested loops, you can also terminate the outer loop with a label statement.
115+
116+
However labeled break is rarely used in JavaScript because this makes the code harder to read and understand.
117+
118+
If you want to learn more about the labeled break statements, visit labeled break.
119+
120+
The break statement is also used with switch statements. To learn more, visit JavaScript switch statement.
121+
122+
123+
124+
125+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// let table = 2
2+
3+
// for (let a = 1; a <= 10; a++) {
4+
// console.log(`${table} x ${a} = ${table * a}`);
5+
// }
6+
7+
// let table = 2;
8+
// let i = 1;
9+
// /* A while loop that will run until the condition is met. */
10+
// while (i <= 10) {
11+
// console.log(`${table} x ${i} = ${table * i}`);
12+
// i++;
13+
// }
14+
15+
// let table = 2;
16+
// let i = 1;
17+
18+
// do {
19+
// console.log(`${table} x ${i} = ${table * i}`);
20+
// i++;
21+
// } while (i <= 10);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let table = 2;
2+
3+
for (let a = 1; a <= 10; a++) {
4+
console.log(`${table} x ${a} = ${table * a}`);
5+
if (a == 9) {
6+
break;
7+
}
8+
// break;
9+
}
10+
11+
console.log("Hello ");
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
The continue statement is used to skip the current iteration of the loop and the control flow of the program goes to the next iteration.
3+
4+
The syntax of the continue statement is:
5+
6+
continue [label];
7+
Note: label is optional and rarely used.
8+
9+
Working of JavaScript continue Statement
10+
Working of continue statement in JavaScript
11+
Working of JavaScript continue Statement
12+
continue with for Loop
13+
In a for loop, continue skips the current iteration and control flow jumps to the updateExpression.
14+
15+
Example 1: Print the Value of i
16+
// program to print the value of i
17+
for (let i = 1; i <= 5; i++) {
18+
19+
// condition to continue
20+
if (i == 3) {
21+
continue;
22+
}
23+
24+
console.log(i);
25+
}
26+
Run Code
27+
Output
28+
29+
1
30+
2
31+
4
32+
5
33+
In the above program, for loop is used to print the value of i in each iteration.
34+
35+
Notice the continue statement inside the loop.
36+
37+
if(i == 3) {
38+
continue;
39+
}
40+
This means
41+
42+
When i is equal to 3, the continue statement skips the third iteration.
43+
Then, i becomes 4 and the test condition and continue statement is evaluated again.
44+
Hence, 4 and 5 are printed in the next two iterations.
45+
Note: The continue statement is almost always used with decision making statements. To learn more, visit JavaScript if...else Statement.
46+
47+
To learn more about for loop, visit JavaScript for loop.
48+
49+
Note: The break statement terminates the loop entirely. However, the continue statement only skips the current iteration.
50+
51+
continue with while Loop
52+
In a while loop, continue skips the current iteration and control flow of the program jumps back to the while condition.
53+
54+
The continue statement works in the same way for while and do...while loops.
55+
56+
Example 2: Calculate Positive Number
57+
// program to calculate positive numbers only
58+
// if the user enters a negative number, that number is skipped from calculation
59+
60+
// negative number -> loop terminate
61+
// non-numeric character -> skip iteration
62+
63+
let sum = 0;
64+
let number = 0;
65+
66+
while (number >= 0) {
67+
68+
// add all positive numbers
69+
sum += number;
70+
71+
// take input from the user
72+
number = parseInt(prompt('Enter a number: '));
73+
74+
// continue condition
75+
if (isNaN(number)) {
76+
console.log('You entered a string.');
77+
number = 0; // the value of number is made 0 again
78+
continue;
79+
}
80+
81+
}
82+
83+
// display the sum
84+
console.log(`The sum is ${sum}.`);
85+
Run Code
86+
Output
87+
88+
Enter a number: 1
89+
Enter a number: 2
90+
Enter a number: hello
91+
You entered a string.
92+
Enter a number: 5
93+
Enter a number: -2
94+
The sum is 8.
95+
In the above program, the user enters a number. The while loop is used to print the total sum of positive numbers entered by the user.
96+
97+
Notice the use of the continue statement.
98+
99+
if (isNaN(number)) {
100+
continue;
101+
}
102+
When the user enters a non-numeric number/string, the continue statement skips the current iteration. Then the control flow of the program goes to the condition of while loop.
103+
When the user enters a number less than 0, the loop terminates.
104+
In the above program, isNaN() is used to check if the value entered by a user is a number or not.
105+
106+
To learn more about the while loop, visit JavaScript while loop.
107+
108+
continue with Nested Loop
109+
When continue is used inside of two nested loops, continue skips the current iteration of the inner loop. For example,
110+
111+
// nested for loops
112+
113+
// first loop
114+
for (let i = 1; i <= 3; i++) {
115+
116+
// second loop
117+
for (let j = 1; j <= 3; j++) {
118+
if (j == 2) {
119+
continue;
120+
}
121+
console.log(`i = ${i}, j = ${j}`);
122+
}
123+
}
124+
Run Code
125+
Output
126+
127+
i = 1, j = 1
128+
i = 1, j = 3
129+
i = 2, j = 1
130+
i = 2, j = 3
131+
i = 3, j = 1
132+
i = 3, j = 3
133+
In the above program, when the continue statement executes, it skips the current iteration in the inner loop and control flow of the program moves to the updateExpression of the inner loop.
134+
135+
Hence, the value of j = 2 is never displayed in the output.
136+
137+
138+
139+
140+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let start = 1;
2+
3+
for (start; start <= 5; start++) {
4+
if (start == 3) {
5+
console.log(start);
6+
7+
console.log(`Strating from 3 and Contiue rest of it`);
8+
continue;
9+
}
10+
console.log(start);
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let a = 1;
2+
3+
while (a <= 6) {
4+
console.log(a);
5+
6+
a++;
7+
if (a == 3) {
8+
console.log(`Strating from 3 and Contiue rest of it`);
9+
continue;
10+
}
11+
}

0 commit comments

Comments
 (0)