Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

JavaScript:Tutorial, Break Out Of Loops

loop

  • Please log in to reply
No replies to this topic

#1 TcM

TcM

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 7563 posts

Posted 08 December 2006 - 03:35 AM

Introduction:-
To understand this tutorial make sure that before you see this tutorial JavaScript :Tutorial, Loops and JavaScript :Tutorial, Your First Game! here we will elaborate a little more on these!

Explanation:-
In this tutorial the Explanation goes before the code too.

while (something)
{
do something else
if (something(while looping))
{
end the loop
}

I hope this explained the basic concept on how to break out of loops

Solution:-
Now here are some practical codes! Here

<SCRIPT LANGUAGE="JavaScript">
var usrno = 0;
while (usrno!=50)
{
usrno = window.prompt('Guess the number, enter end to finish',0);
if (usrno=='end')
{
alert('Game Ended');
break;
}
if (usrno<50)
{
alert('too low');
}
if (usrno>50)
{
alert('too high');
}
}
</SCRIPT>

Explanation Of Code:-
usrno = window.prompt('Guess the number, enter end to finish',0);
if (usrno=='end')
{
alert('Game Ended');
break;
}
This is the most important code. Here we are promoting the user ( see this tutorial JavaScript :Tutorial, Prompt ) and asking him to enter a number but if he instead writes 'end' without the ' ' the game will display an alert and we will break out of the loop, because of the 'break;' command!!

A Preview:-
Posted Image

Conclusion:-
As Always Feedback is welcome and the full source is attached!!

Attached Files


  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download