Why is the do-while loop in my Java code printing '9'?

In summary: Handle memory allocation errors mov eax,[ebp+var_10] mov ecx,[ebp+var_8] mov edx,[ebp+var_4] mov esi,[ebp+var_2] mov edi,[ebp+var_0] mov ebx,[ebp] ; Allocate memory for the row Mov ecx,dword ptr [ebp+var_10] Mov edx
  • #1
Ghost Quartz
8
4
TL;DR Summary
Hi. I'm a beginner in java and was writing code that I expected to generate 15,16,17,18,5,6,7,8 as output, but I received 15,16,17 and 9. I don't understand why.
Java:
int bd= 15;
int ad= 5;

while(ad!=9){
        System.out.println(bd);
        ad++;
        bd++;
   }//output 15,16,17,18
do {
        System.out.println(ad);
        ad++;
   }while(ad<=9);//output 5,6,7,8

thanks for the help
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
you need to reset the ad value before the do loop as it is actually 9 at that point not 5.

So here's what happens the do loop prints the ad value which is 9 and then you add one to it and then the while condition says "hey the ad value is 10 hence ad>9 and so I'll stop".
 
  • Like
Likes Ghost Quartz
  • #3
I wrote ad = 5; before do loop and it worked. Thank you very much!
 
  • Like
Likes jedishrfu
  • #4
In general, to debug things like this it helps greatly to insert temporary print statements or to follow the execution in a debugger and look at important variable values at critical points.
 
  • Like
Likes Ghost Quartz, jim mcnamara, Vanadium 50 and 1 other person
  • #5
FactChecker said:
In general, to debug things like this it helps greatly to insert temporary print statements or to follow the execution in a debugger and look at important variable values at critical points.
I will remind to do this for the next time. thanks:smile:
 
  • #6
And while you're still a beginner, it would greatly help you to get in the habit of creating meaningful variable names. Your program is small enough that (ad and bd) are easy to read here, but it doesn't take much to reach a level of complexity where inscrutable var names are a real problem. Start that habit soon.

Also, maybe it's just me but I'm pretty sure you have a superfluous while in there.
 
  • Like
Likes Ghost Quartz
  • #7
I know that. I learned while and do-while yesterday, so this program was just to synthesize the content. Thanks
 
  • Like
Likes jedishrfu
  • #8
As a beginner, it is good to see that you are putting comments in that help to understand the code. Many do not. It is a skill that takes time to develop and it is good to start early. Don't be afraid to put more in. There are never(?) too many.
 
  • Like
  • Haha
Likes jedishrfu and DaveC426913
  • #9
DaveC426913 said:
Also, maybe it's just me but I'm pretty sure you have a superfluous while in there.
Sadly, it's you. :-) The OP has a while loop for zero or more iterations and a do...while for one or more iterations. Of course, there are many ways to skin a cat so it's possible it could have been done in a more streamlined fashion with fewer loops.

Choose one or the other based on whether you need to execute the code at least once or not.

As an example, your program might use a do ... while when asking for input from the end-user and if the input is invalid or out of some program-defined bounds then loop around and ask the question(s) again, a one or more action.
 
  • #10
jedishrfu said:
Sadly, it's you. :-) The OP has a while loop for zero or more iterations and a do...while for one or more iterations. Of course, there are many ways to skin a cat so it's possible it could have been done in a more streamlined fashion with fewer loops.

Choose one or the other based on whether you need to execute the code at least once or not.

Yeah, no. I didn't the mean the While versus Do, I meant the two Whiles.

There's one at the start and one at the end.

Been decades since I programmed in Java and it's possible I'm mixing it up with a similar JavaScript structure.
 
  • #11
There are two loop constructs:
One while(...){...} to print 15,16,17,18
And another do{...}while(...) to print 5,6,7,8
 
  • Like
Likes jedishrfu and DaveC426913
  • #12
DaveC426913 said:
Yeah, no. I didn't the mean the While versus Do, I meant the two Whiles.

There's one at the start and one at the end.
I think you are confused about the difference between while loop versus a do... while loop. As @jedishrfu mentioned, the main difference is whether you want the condition tested before the iteration starts (while loop) or after an iteration (do...while loop).
DaveC426913 said:
Been decades since I programmed in Java and it's possible I'm mixing it up with a similar JavaScript structure.
The syntax of both types of loops is the same in Java and Javascript, as well as in C, C++, and C#.
 
  • Haha
Likes jedishrfu
  • #13
While and while conditions away
then do while you’re waiting
To do something today.

A little poetic nonsense for a beautiful sunny Austin Saturday.
 
  • #14
Mark44 said:
I think you are confused...
Yeah.

In my defense, it has been decades since I've been allowed to write in raw code, under pain of shunning. If you're not using a library that sits on a framework that sits on an engine, you are a leper.
Like forgetting your times tables because calculators are required now. :rolleyes:
 
  • #15
DaveC426913 said:
In my defense, it has been decades since I've been allowed to write in raw code
This is my idea of raw code (Intel x64 AVX2 - Advanced Vector Extensions with 256-bit regs):
C++:
ProcessRow:
     ; Read a row (8) of floats
     vmovdqu ymm1, ymmword ptr[r8]        ; Copy from source array to YMM1
     vrsqrtps ymm0, ymm1                  ; Put reciprocal square root values in YMM0
     vmovdqu ymmword ptr[rdx], ymm0       ; Copy results from YMM0 to destination array
     add r8, rsi                          ; Increment addresses in array by 8 bytes
     add rdx, rdi    
     loop ProcessRow

I posted the same snippet in another thread on fast reciprocal square roots - https://www.physicsforums.com/threads/fast-reciprocal-square-root-algorithm.1001061/post-6471417.
 

1. Why is the do-while loop printing "9"?

The do-while loop is printing "9" because it was specifically programmed to do so in the code. The loop continues to run as long as the condition is true, and in this case, the condition is set to print "9".

2. Is there a specific reason why "9" is being printed in the do-while loop?

Yes, the specific reason is that the loop condition is set to print "9". This could be for a specific purpose in the code, or it could be a placeholder value for testing purposes.

3. Can the do-while loop be modified to print a different value?

Yes, the do-while loop can be modified to print a different value by changing the loop condition in the code. This could be done by changing the value within the condition or by altering the code inside the loop.

4. Why is the do-while loop printing "9" repeatedly instead of stopping after one iteration?

The do-while loop is designed to continue running as long as the condition is true. In this case, the condition is set to print "9", so the loop will continue to run until the condition is changed or the loop is manually stopped.

5. What is the purpose of using a do-while loop to print "9"?

The purpose of using a do-while loop to print "9" could vary depending on the context of the code. It could be used as a placeholder value for testing or a specific value needed for a calculation or output. The do-while loop allows for a specific task to be repeated until a certain condition is met, making it useful in various situations.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
634
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top