Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Learning JavaScript tutorial – JavaScript while(loop), do…while(Loop) statement


Learning JavaScript tutorial – JavaScript while(loop), do…while(Loop) statement

JavaScript while statement

In JavaScript, while loop statement allows us to perform some action on a piece of code till the condition is satisfied or true. The JavaScript while loop statement check condition at the start of the execution of the loop. If the expression evaluates to true, the while loop statement execute the body of the loop; means the piece of code between the curly brackets.

JavaScript while statement Syntax

while(conditional expression is true)
{
  statement block executed while conditional expression
  evaluates to true(satisfied).
}

Here, If the resulting value of conditional expression is true or it evaluated to true, while loop statement block enclosed within the Curly braces – {} is executed.

JavaScript While loop Statement Flowchart
do while loop statement flowchart

JavaScript while loop statement repeat the execution of code till the conditional expression satisfied. This cycle continues until expressions evaluates to false. When javaScript while loop statement expression evaluates to false, while statement ends and JavaScript moves on to the next statement in the program. If the proper care is not taken to evaluate expression value javaScript while loop statement goes in an infinite loop till the statement expression evaluates to true.

<script type="text/javascript" language="javascript" >
var iCount=5;
while(iCount<8)
  {
  alert("iCount is " +iCount);
  iCount++;
  }
</script>

In the above code, the value of iCount is initially set to 5, and then we enters in the while loop, while loop evaluates the condition, if true the value of iCount is output, and the iCount value is incremented by 1. The process is repeats until iCount reaches 8, at which point the while loop conditional expression becomes false and while loop statement ends and JavaScript moves on to the next statement in the program.

JavaScript do while statement

JavaScript do while loop is similar to the while loop except the execution of the loop statement is done at the start and then the condition checked . If the conditional expression evaluates to true, the while loop statement execute the body of the loop again else not. Means whenever we use JavaScript do while loop statement executed at least once .

JavaScript do while statement syntax

do
{
  statement block
}
while(conditional expression is true(satisfied) execute statement block again)

JavaScript Do While loop Statement Flowchart
do while loop statement flowchart

<script type="text/javascript" language="javascript" >
var iCount=5;
do
{
  alert("iCount is " +iCount);
  iCount++;
}
while(iCount<8);
</script>

In the above code, the value of iCount is initially set to 5, and then we enters in the do while loop, the value of iCount is output and the iCount value is incremented by 1. Now the do while loop evaluates the condition, if it is true the process is repeats until iCount reaches 8, at which point the do while loop conditional expression becomes false and do while loop statement ends and JavaScript moves on to the next statement in the program.

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] This post was mentioned on Twitter by pixhtml, Mattias Grenhall. Mattias Grenhall said: RT @webdesignmagz2011Learning JavaScript tutorial – JavaScript while(loop), do…while … http://bit.ly/hMO11N #javascript [...]

Pingback by Tweets that mention Learning JavaScript tutorial – JavaScript while(loop), do...while(Loop) Statement | Tips n Tracks -- Topsy.com on February 9, 2011 @ 8:12 am

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image