Results 1 to 6 of 6

Thread: Explain: particular WHILE condition

  1. #1
    Join Date
    Nov 2013

    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.

    <!DOCTYPE html>
    <html lang="en">
    	<meta charset="UTF-8">	
    	<title>Sum the Numbers</title>
       	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></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);
            <h1>This page is displayed after the JavaScript is executed</h1>

  2. #2
    Join Date
    Oct 2012
    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:

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

    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 09:14 AM.

  3. #3
    Join Date
    Nov 2013
    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
    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:

    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:

    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 04:58 PM.

  5. #5
    Join Date
    Nov 2013
    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
    1. Yes.


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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09181 seconds
  • Memory Usage 2,900KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates