?
Solved

Do while loop syntax in java

Posted on 2008-10-10
7
Medium Priority
?
934 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 668 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 668 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
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!

 
LVL 2

Assisted Solution

by:Monky42
Monky42 earned 664 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

800 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