+ Reply to Thread
Results 1 to 1 of 1

Thread: JavaScript:Tutorial, Break Out Of Loops

  1. #1
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    JavaScript:Tutorial, Break Out Of Loops

    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.

    Code:
    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

    Code:
    <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:-
    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:-


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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Othello program!! 24 hours left!
    By siren in forum C and C++
    Replies: 1
    Last Post: 06-14-2007, 11:18 AM
  2. MySQL Loops
    By reachpradeep in forum Database & Database Programming
    Replies: 0
    Last Post: 03-04-2007, 09:12 AM
  3. JavaScript:Tutorial, Loops
    By TcM in forum Javascript
    Replies: 4
    Last Post: 12-13-2006, 08:53 AM
  4. Loops
    By Sionofdarkness in forum C and C++
    Replies: 7
    Last Post: 07-28-2006, 07:53 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts