while loop doesn't stop in js

while loop example
while loop java
while loop python
while loop not working python
do while loop
while loop c++
while loop in c pdf
what is for loop

I want to use function with while to print only 10 numbers starting from the number I choose.

But while loop doesn't stop looping.

id = prompt('Write any number.');
function numbering(a) {
    var i = a; 
    var j = a + 10;  
    while (i < j) {
       document.write(i);
       i++;
    }
};
numbering(id);

When you use function numbering(a) {, the variable a is passed as string.

This results in i and j being set as string.

Taking example: Suppose you pass 2 as input, your variables will be set as a="2", i="2" and j="210". So according to your condition, it'll print starting from 2 till 209.

You can change your code to parse a as number to achieve your result; something like:

function numbering(a) {
    a = parseInt(a);  //parse as Int here
    var i=a; var j=a+10;
    while (i < j) 
      {
         document.write(i);
         i++;
      }
};

While loop doesn't continue even though condition still holds true , Why is it only executing 5 times? According to the code, counter is used as the used to check if it should continue looping for not. The only  This simplifies the loop construct in several ways: No initial assignment to variable again before the loop is necessary. The input is converted to lower case so we only compare against 'y' instead of both upper and lower case versions. The loop control is simplified, the infinite loop will exit only if the user enters anything other than 'Y' or 'y'.

Try this:

var id = parseInt(prompt('Write any number.'), 10);

In your example id will be of type string and the comparisons won't work as you expect.

While loop doesn't work? - JavaScript, However, it is looping infinitely and I don't understand why. The code within the while-loop works when taken separately, and currentNumber is  A loop becomes infinite loop if a condition never becomes FALSE. You must use caution when using while loops because of the possibility that this condition never resolves to a FALSE value. This results in a loop that never ends. Such a loop is called an infinite loop.

Try it with a for-loop:

for(var i=0; i<10; i++){ doSomething}

Typical errors with 'While' Loops, So because the loop function doesnt have any parameters it is left empty and called like this loop() . In essence this function is completely useless as it only does  The different between the VBA While and the VBA Do Loop is : While can only have a condition at the start of the loop. While does not have a Until version. There is no statement to exit a While loop like Exit For or Exit Do. The condition for the VBA While loop is the same as for the VBA Do While loop.

You can try with this for-loop function :

 id=prompt('Write any number.');
 function numbering(a) {
  for (let i = a; i < a + 10; i++){
    document.write(i);
  }
 };

numbering(id);

While loop, It may seem as if the meaning of the word else doesn't quite fit the while loop as well as it does the if statement. Guido van Rossum, the creator of Python, has  while expression, statements, end evaluates an expression, and repeats the execution of a group of statements in a loop while the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric).

When you enter a number in the prompt, it is supplied as a string. Since you have not converted this into a number, the line var j = a + 10 is actually joining the two values as if they were strings.

For example, if you enter "5" into the prompt, then var j = "5" + 10 returns "510". Only when you then compare the two variables in the while loop with i < j does it get interpreted as a number, and will loop from 5 to 510.

The easiest way to convert a string into a number is to use parseInt(value).

Python "while" Loops (Indefinite Iteration) – Real Python, It doesn't stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows). We can use it if  The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. // code block to be executed. while ( condition ); The example below uses a do/while loop. The loop will always be executed at least once, even if

Loops: while and for, Your browser does not currently recognize any of the video formats available. We can use the while loop to create any type of iteration we wish, including  When a while loop is encountered, <expr> is first evaluated in Boolean context. If it is true, the loop body is executed. If it is true, the loop body is executed. Then <expr> is checked again, and if still true, the body is executed again.

8.3. The while Statement, Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to  In a while loop, the test condition is checked first and if it is true then the block of statements inside the loop is executed. After one iteration again the test condition is checked and this process is continued until the test condition evaluates to false.

Python While Loops, The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the 

Comments
  • Replace document.write with insertAdjacentHTML().
  • Then input 0 value then you 0to9 as result
  • Just add: a = +a;. The unary operator (+) will cast the variable a to a number, since prompts actually returns strings, so using + on strings will result in concatenating.
  • Sorry my question was wrong. Not from 0, but from the number I choose at prompt.