Solved

Do while loop syntax in java

Posted on 2008-10-10
7
926 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

691 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