www.webdeveloper.com
Results 1 to 6 of 6

Thread: Explain: particular WHILE condition

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    4

    Exclamation Explain: particular WHILE condition

    I don't seem to understand the while condition and would like to know if someone has a better explanation. I understand the while loop will only execute if the condition is true. So now I ask you how can this code "run" successfully if I enter the number 11? the while loop has a condition that checks if topNumber(11) < 1 or topNumber > 100. How can code/statements below the while loop execute if 11 is not less than 1 or greater than 100 which will make this a false condition. Maybe I'm going around circles but I hope someone else has a better explanation as the javascript runs perfectly fine.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">	
    	<title>Sum the Numbers</title>
       	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> 	
       	<script>
            alert("Sum the Numbers from 1 through entry");
            
            do {
            	var topNumber = prompt("Enter top number to sum", 5);
            	topNumber = parseInt(topNumber);
            	if (isNaN(topNumber) || topNumber < 1 || topNumber > 100) {
            		alert("Top number must be greater than zero and less than 100");
            	}
            }
            while (isNaN(topNumber) || topNumber < 1 || topNumber > 100);
            
            var sum = 0;
            for (i = 1; i <= topNumber; i++) {
            	sum += i;
            }
            alert("Sum of numbers from 1 through " + topNumber + " = " + sum);
            
        </script>
    </head>
    <body>
        <section>
            <h1>This page is displayed after the JavaScript is executed</h1>
        </section>
    </body>
    </html>

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    250
    It's because this is not a "true" while loop. This is do while loop. It's used rarely in JS because in fact there's no such thing that you cannot do without it. This is the syntax of Do While loop:

    Code:
    do
    { //some code }
    while (condition) //specifies condition
    This is how while loop looks like:

    Code:
    while (condition)
    {
        //do something
    }
    The main difference is that the while loop executes only if prior to the code specified condition is true. On the other hand do while loop makes sure that your code inside "do while" statement gets executed at least once regardless of specified condition being true or false.

    This is the reason why your code gets executed only once (in case of 11), calculates the sum and then exits.
    Last edited by tech_soul8; 11-07-2013 at 08:14 AM.

  3. #3
    Join Date
    Nov 2013
    Posts
    4
    Thank you very much it helps a lot! I think I was going around circles with this issue and now it's clear as water.

    If I enter a number less than 1 or greater than 100 an alert message will appear as "top number must be greater than..." and program will exit and will not enter while loop. On the other hand if I enter a number such as 11 it will enter the while loop successfully and return the sum because the do loop must execute ONCE correct?

    Thanks for your help. I really appreciate it.

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    250
    No, if you enter number less then 1 or greater than 100 the code inside the loop will continue executing as long as this condition is true. It will alert a message "Top number must be greater than zero and less than 100" and will keep you prompting for another input as long as you keep entering numbers less than 1 or greater than 100. On the other hand if you enter number greater then 1 and less than 100 code inside the loop will execute just once and exit. (It wont' return sum, sorry for this, didn't take a look at your code. The sum is calculated later on)

    Do while loop is just like the while loop the only difference is that with the do while loop you get sure that your code will get executed at least once regardless of condition specified.

    Consider this example:

    Code:
    var blnLoop = true; //specifies boolean for the while loop
    
    while (blnLoop == true)
    {
        var n = prompt("Enter a number: ","") //enter some number to stop the loop
        if (!(isNaN(n)))
            blnLoop = false; //if a number was entered stops the loop
    }
    With the above code if you want your code inside the while loop to get executed you have to define blnLoop variable. On the other hand take a look at the following code:

    Code:
    do //code will execute at least once
    {
        var n = prompt("Enter a number: ","")
    }
    while (isNaN(n)) //code inside the loop statement will continue to execute as long as specified condition is true
    As you can see the code do the same stuff only with the Do while loop it wasn't necessary to specify blnLoop variable for loop to get executed.
    Last edited by tech_soul8; 11-07-2013 at 03:58 PM.

  5. #5
    Join Date
    Nov 2013
    Posts
    4
    so the do loop *must execute once and if a number from [1,100] is not entered then the appropriate if condition will be accessed followed by the corresponding message. and the prompt message will ask again for another number between 1 and 100 correct? and then program will exit successfully if a *correct number is entered, for example 11?

    I hope I'm right.

    Does the while loop reinforce the Do loop to have a successful execution? For example, it will continue to prompt user for a number until a number from 1-100 is entered and then it will exit.

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    250
    1. Yes.

    2.

    Code:
    do {
        // code inside the curly braces that will execute at least once!!
    }
    while (condition) //specifies condition that if evaluated to TRUE will force the code inside the do statement to execute as long as the specified condition is TRUE
    You can't have do statement without the while statement that comes exactly after the set of curly braces. They work in tandem. On the other hand it's completely legal to have while statement that stands "alone" (that doesn't include do statement).

    These are two different loops namely do while and while respectively.
    Last edited by tech_soul8; 11-11-2013 at 01:40 PM.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles