Jump statements are used to interrupt the normal flow of program.
Here we will discuss following Jump Statements
The break
statement is used inside loop or switch statement. When compiler finds the break
statement inside a loop, compiler will abort the loop and continue to execute statements followed by loop.
Example:
var a = 1; while(a <= 10) { if(a==5) break; a++; } console.log("Value of a is " + a); // Value of a is 5
The continue
statement is also used inside loop. When compiler finds the continue
statement inside a loop, compiler will skip all the followling statements in the loop and resume the loop.
Example:
var a = 0; while(a < 10) { a++; if(a == 5) continue; console.log(a + " "); } // prints 1 2 3 4 6 7 8 9 10
Try the following example in the editor below.
You are given an integer N, print all the odd values, for all i, where 0 <= i < N. Print each values on a seperate line.
Note: Use continue to avoid even numbers and break to get out of the loop.
Sample Input:
6
Sample Output:
1
3
5