Solved

Do while loop syntax in java

Posted on 2008-10-10
7
907 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now