Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

01 September 2013

Difference between the break and continue in programming language


BREAK
Break
 The break statement will terminate iteration of the loop and continue executing if there is any code that follows after the loop. Example of Break statement in JavaScript:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<script>

x=0;

for(x=0;x<10;x++)
{
if(x==3)
{
break;    
}
document.writeln("x=" + x + "<br />");

}


</script>
Output: x=0 x=1 x=2

Continue
Continue

The continue statement will terminate the current execution of loop and resume the loop with the next value if any. Example of Continue statement in JavaScript:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<script>

xx=0;

for(xx=0;xx<5;xx++)
{
if(xx==3) 
{
continue;
}
document.writeln("xx=" + xx + "<br />");
}

</script>
Output: xx=0 xx=1 xx=2 xx=4
Beginning Programming All-In-One Desk Reference For Dummies

29 August 2013

What are the difference between the while and do…while loop in computer programming?

To better understand the difference of while loop & do..While loop, you have to know the syntax of this loop.

The syntax of While loop is as follows:
while (variable<=end_value)

{

//Code to be executed writes here

}


while loop
While Loop

While loop Example in JavaScript:
1
2
3
4
5
6
7
8
<script type="text/javascript">
var1=0;
while (var1<=5)
{
document.write("The number is " + var1+"<br />");
var1++;
}
</script>

do while loop
Do While Loop

The syntax of do…while is as follows:
1
2
3
4
5
6
7
8
9
do

{

//code to be executed writes here

}

while (variable<=end_value);

do.. while Example in JavaScript:
1
2
3
4
5
6
7
8
9
<script type="text/javascript">
var1 = 0;
do
{
document.write("The number is " + var1+ "<br />");
var1++;
}
while (var1 <= 5)
</script>

  • So, In the case of the while loop, the condition is checked first, if the condition is false, the block will not be executed. 
  • On the other hand, In case of the do…while loop, the condition is checked after the block is executed; therefore the block is always executed at least once.

Programming: Computer Programming for Beginners: Learn the Basics of Java, SQL & C++ (Coding, C Programming, Java Programming, SQL Programming, JavaScript, Python, PHP)

Featured Post

How to Write PHP code and HTML code within a same file

A PHP file can have both PHP code and HTML code together. Any HTML code written outside of PHP <?php //php code here… ?> Code is ig...

Popular Posts