Home > Back-end >  Why isn't my JavaScript Do-While Loop bringing the results I want?
Why isn't my JavaScript Do-While Loop bringing the results I want?

Time:10-22

This is the piece of code giving me headache. I am new to JavaScript and I am just learning the concepts your knowledge would do much help. The details of the code and output are given below. If you have an idea on how to solve this problem, please answer the question. Thanks!

//These are my variables
var candy= 10;
var cookies= 20;
var x= 20;
var y= 30;
var g= 1;

//While Loop
while (1 > 10){
    console.log ("x   y=50")
}//this won't run at all because 1<10 will never be true, I am 
trying to avoid infinity loops

while (g<=5) {
    console.log("The number is "   g )
    g  ;
}/*if it was '<' it would be an infinity loop or we can just break 
the loop. 
output:
The number is 1
The number is 2
The number is 3.... All the way up to 5
*/

//Do-While Loop
do {
    console.log("This is a do-while loop")
} while(candy < 9 && cookies >= x)
/*This will execute once even though both the conditions are 
false, it's just how do-while loops were made*/


//This where the problem comes in
do{
    console.log("The other number is "   g)
    g  ;
} while(g<=5)
/*output: The other number is 6
  I thought 6 is greater than 5? Why is it bringing such an output 
  yet the condition is g<=5?
  I was expecting:
  The other number is 1
  The other number is 2.... All the way up to 5
  */

CodePudding user response:

let g = 1
while (g<=5) {
  console.log("The number is "   g )
  g  ;
}
//Until g is 5 add one to g, this means that g is 6 here
console.log(g)

do{
  console.log("The other number is "   g)
  g  ;
} while(g<=5)
// a do while loop executes once before the condition is checked

CodePudding user response:

I checked your code and found issue.

while (g<=5) {
    console.log("The number is "   g )
    g  ;
}

after this, the g is already 5 so when this come

do{
    console.log("The other number is "   g)
    g  ;
} while(g<=5)

the loop will run only one time due to g is not below 5. if you change code like this. it will work well

var g=1;
do{
    console.log("g"   g);
    g  ;
}while(g<=5)

just remove prev loop codes which make change g

CodePudding user response:

In the first loop, where g is involved you check this condition: (g<=5). This means that the loop ends when g is greater than 5, right?

while (g<=5) {
    console.log("The number is "   g )
    g  ;
}

Here you have

g=1 -> g -> g=2

g=2 -> g -> g=3

g=3 -> g -> g=4

g=4 -> g -> g=5

g=5 (g is still equals to 5, so your condition is met) -> g -> g=6

g=6 (g now is greater than 5, so your condition is not met. You don't loop anymore. Anyway g=6 because you incremented it with g in the previous iteration of the loop)

When you do the next loop, it is a do-while. This means that the condition is checked after the iteration.

do{
    console.log("The other number is "   g)
    g  ;
} while(g<=5)

Having said this, what happens is:

You print "The other number is 6" because g is 6 since the last iteration of the previous loop

You increment g to 7

You check the condition: g=7 is minus or equals to 5? No. --> LOOP STOPS

You can try to debug and evaluate the values of g at each step.

Also, keep in mind that g is declared outside the loops, so its scope is "global". This means that the value won't "reset" at every new loop. That's why you don't get what you expect:

The other number is 1

The other number is 2.... All the way up to 5

  • Related