Solved

Do while loop syntax in java

Posted on 2008-10-10
7
915 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
windows explorer path to command prompt 5 54
ejb wildfly example 2 18
Adding multiple JVM environments to RedHat 6 7 46
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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

808 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