In the following code:
for (Iterator<MyClass> iterOuter = mList1.iterator(); iterOuter.hasNext();) {
mClass1 = iterOuter.next();
for (Iterator<MyClass> iterInner = mList2.iterator(); iterInner.hasNext();) {
mClass2 = iterInner.next();
if (...) {
continue;
}
else if (...) {
//code 1
}
else if (...) {
continue;
}
else if (...) {
//code 2
}
}
}
EDIT: code 1 & 2 are math calculation, in any moment ain't add or remove to the iterators.
When I inspect it with debugger I notice then after the last inner iteration, when I expect the debugger jump to the inner for line check the condition and then realize it's end of loop and jump back to outer for loop.
Instead after the inner for check (when the condition is not met and need to jump back to outer) the debugger jumps to the line with //code 2 , NOT executing the code there and then jumps back to outer for.
What's happening here? Is this some issue with the debugger or it's something with java, the if else if structure with the continue etc?
Related
I am using debug mode in Eclipse Oxygen to, as you might guess, debug my code.
I am writing a backtracking algorithm (a recursive function - it calls itself).
In the Backtrack function there is a for loop, and at the end of the for loop, if certain conditions are met, this code is run: Backtrack(csp, index + 1, CopyCSP(currentSolution));.
I'm debugging my code, and I want to go to the next iteration of the for loop, so when I get to this line, I hit "step over." But it steps into, and walks me through the next Backtrack function.
I know for a fact that it is actually the next function, because as you can see, the index variable goes up by one, which happened.
Why is this happening? How can I avoid this and actually step over? If step over doesn't do what I want here, what should I use?
Here's my code for the full function:
private void Backtrack(CSP csp, int index, CSP currentSolution) {
//BREAKPOINT IS HERE
if(index == csp.numVars) {
currentSolution.PrintSolution();
csp.PrintSolution(currentSolution);
solved = true;
return;
}
for(int test = 0; test < csp.MaxDomainSize(); test++) {
if(solved) {
return;
}
if(test < currentSolution.vars[index].domain.size) {
currentSolution.vars[index].value = currentSolution.vars[index].domain.get(test);
}
else {
continue;
}
boolean satisfied = true;
for(int i = 0; i < csp.constraints.size; i++) {
if(!csp.constraints.get(i).Satisfied(currentSolution.vars, index)) {
satisfied = false;
}
}
if(satisfied) {
System.out.println("Variable " + index + " satisfied by " + currentSolution.vars[index].value + ".");
Backtrack(csp, index + 1, CopyCSP(currentSolution));
}
}
}
I've put a comment where the breakpoint is.
I was having a similar problem and found similar solution, posting here for reference.
Problem:
Debug session 'Stepped Into' each line for every 'Step Over (F6)' operation
Solution:
'Run->Remove All Breakpoints'
'Run->Restart'
Result:
'Step Over (F6)' steps over active debug line now as expected.
Sharing for clarity and simplicity if encountered, hopefully this helps.
Following the train of thought that it's the breakpoint...
A breakpoint stops the flow of control in most situations.
Eclipse has options to disable individual breakpoints and disable all breakpoints. Your situation might be right for conditional breakpoints:
Right click on the breakpoint and select breakpoint properties. This will bring up a dialog with "hit count" and "conditional" which works most of the time and is confusing when it doesn't.
If you check "conditional", this will enable a text box where you can write a condition to use the variables to make a true statement. So you could enter "index==1000" and then it would stop when you are 1000 calls deep.
I have not used "hit count" myself.
I have two "for" loops.
for(n=0;n<6;n++){
for(w=n;w<6;w++){
// if w==4 then go to first loop an continue from n=4!!
}
}
How can i jump to n=4 when w takes value 4; Like old Basic command "goto"..
Thanks
How can i jump to n=4 when w takes value 4; Like old Basic command
"goto"
You can use break to "jump" out of the current loop back to the outer loop:
for(n=0;n<6;n++){
// other code
for(w=n;w<6;w++){
// if w==4 then go to first loop an continue from n=4!!
if (w == 4) {
n = 4;
break;
}
}
// other code
}
I'm trying to make a very basic game where you guess a number between 1-1000 using a do loop. Everything works, except when I finally make the correct guess, I am still prompted to make another guess, and when I enter the same correct guess again, the program terminates like it's suppose to.
Why do I have to make that extra guess to finally get my program to work? Am I looping around an extra time? Also, if I make a correct guess (the compiler will say I am correct then still prompt me), then a wrong guess (the compiler will tell me I'm wrong), then the correct guess again, the program will only terminate after I make the correct guess a second time.
The second do loop at the bottom is what I put in my main method. Everything above is in a method I wrote called play.
public static boolean play()
{
boolean c;
int n = 0;
do {
String input = JOptionPane.showInputDialog("Enter a number between 1-1000");
n = Integer.parseInt(input);
if (n == guess)
{
System.out.println("Correct");
c = true;
}
else if (n < guess)
{
System.out.println("Not Right");
c = false;
}
else
{
System.out.println("Not Right");
c = false;
}
guess++;
} while (c == false);
return c;
}
In main method:
do {
game1.play();
} while (game1.play() != true);
This loop runs the play method twice in each iteration of the loop :
do {
game1.play(); // first call
} while (game1.play()!=true); // second call
You are not testing the value returned by the first call, so even if it returns true, you would still call game1.play() again, which will display "Enter a number between 1-1000" again.
Replace it with:
boolean done = false;
do {
done = game1.play();
} while (!done);
This would only call play() one time in each iteration of the loop.
That said, I'm not sure why you need the outer loop.
You can just replace in with one call to game1.play(), since game1.play() will loop until the correct number is entered.
I have this code which generates random number from an array, at a particular condition (when x and y both are equal to zero). I want the control to jump to the label. But the control never jumps to the label in any condition. I wanted to know that whether I am doing it right or not?
int[] arr = {0, 1, 2};
Random rn = new Random();
label: {
//some code
if (x != 0 && y !=0) {
//some code
} else {
break label;
}
}
Without examining whether you should, yes, you can use labeled statements with if in Java. According to the 1.7 specification
The Identifier is declared to be the label of the immediately contained Statement. [...] identifier statement labels are used with break (§14.15) or continue (§14.16) statements appearing anywhere within the labeled statement.
It goes on (emphasis added)
If the statement is labeled by an Identifier and the contained Statement completes abruptly because of a break with the same Identifier, then the labeled statement completes normally. In all other cases of abrupt completion of the Statement, the labeled statement completes abruptly for the same reason.
So if you break an if block (remember a block is a statement), you can exit the if body. Let's test it:
public static void main(String[] args) {
if (true) label: {
if (args != null)
break label;
System.out.println("doesn't get here");
}
System.out.println("Outside of labeled block");
}
Output:
Outside of labeled block
The break statement breaks loops and does not transfer control to the label.
Using a label with break is for when you have inner and outer loops. An unlabeled break breaks the inner most loop (the one you are in) whereas a labeled break allows you to specify an outer loop.
See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
Specifically the BreakWithLabelDemo
Try to Avoid using labels. What you could do there, is:
while(true) {
if(x == 0 && y == 0) {
break;
}
// some stuff with x and y
}
I suggest that you use a recursive function.
public void work(){
// some code
if (x != 0 && y != 0) {
//some code
} else {
work();
}
}
You cannot use break without loops
As far as I know, labels cannot be used arbitrarily around {}, you need to do them to mark a for, while or do-while loop
I would like to ask you guys a question. You see, I know what a for-loop is for but can someone please maybe explain how one works, just to help me get my head around it, an example is:
for(int i = 0; i < 10; i++) {
System.out.println("hello");
}
Now obviously that will just print Hello 10 times into the console but that's besides the point, I want to know how the for-loop works.
Sorry if i have confused anyone asking this - Shaun
The for loop in your example is more or less equivalent to this:
int i = 0;
while (i < 10) {
System.out.println("hello");
i++;
}
The only difference is that with your for loop, the variable i exists only within the scope of the loop.
Every for loop can be transformed into a while loop using this same pattern.
for (init; test; continuation) {
// loop body
}
becomes:
init;
while (test) {
// loop body
continuation;
}
Again, the only difference will be with the scope of any variables declared in init.
The for Statement
The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:
for (initialization; termination; increment) {
statement(s)
}
When using this version of the for statement, keep in mind that:
The initialization expression initializes the loop; it's executed once, as the loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
Well, this is how it is set up:
for (a; b; c)
"A" is something that is done at the beginning of the loop. It can actually be left out if necessary, like this:
for (; b; c)
"B" must be a true or false statement (like i<10, it either is or it isn't). Once "b" is no longer true, the loop stops.
"C" is something that is done at the end of the loop.