Solved

Do while loop syntax in java

Posted on 2008-10-10
7
912 Views
Last Modified: 2013-12-14
I have some java code for a simple do while loop but it is exhibiting some strange behavior. Sometimes it works but sometimes it exits the loop when it is not supposed to. Can anyone see what I'm doing wrong with this?

I have checked that I am receiving keyboard input correctly and setting the flag to 4 when entered.
I have just put the relevant code.

Thank you
int flag = 0;
do
{
  if (flag == 4)
  {
  System.out.print("Quitting");						
  }
}
while(flag != 4);

Open in new window

0
Comment
Question by:GrimReeper
7 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 167 total points
ID: 22685484
Its infinite loop for any value other than 4. What is that you want to achieve with this loop?
0
 
LVL 4

Assisted Solution

by:petr_hlucin
petr_hlucin earned 167 total points
ID: 22685491
I suppose that value of flag is modified in a different thread. Make sure modyfying of this value takes place within a critical section.
0
 
LVL 5

Author Comment

by:GrimReeper
ID: 22685494
I want the loop to keep running until input is changed to 4 but the loop exits when input is 1.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Assisted Solution

by:Monky42
Monky42 earned 166 total points
ID: 22685573
You need to provide more information. If you are using multiple threads please post all code concerning the two threads and the synchronized flag value. If you are not using multiple threads you might have a design problem. The loop as you have posted it is senseless.
0
 
LVL 5

Author Comment

by:GrimReeper
ID: 22685590
I have changed it to a while loop and now it works. Strange but thanks for the help anyway.
0
 
LVL 5

Author Closing Comment

by:GrimReeper
ID: 31504943
Thank you
0
 
LVL 5

Author Comment

by:GrimReeper
ID: 22685660
For anyone interested it ended up being the validation from another class I had was kicking in. I always forget the most simple of things that have the biggest consequences haha.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
mockito example issue 8 72
Error trying to install RTMT Win7 5 60
Unhandled exception type Exception 18 31
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question