The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Breaking If/Else Statements

I want to know if it is possible to break out of an If/Else statement that is inside a loop. For example, you enter the Else Statement, do some more stuff, and under a condition that was determined inside the Else Statement, stop running the If/Else statement, and continue with the rest of the loop. Currently, the only way I can figure out how to do it is by using a boolean, setting the boolean under conditions, and checking the boolean's value before continuing with the Else Statement.

Re: Breaking If/Else Statements

continue with the rest of the loop

Can you explain why that is not happening? If there is a chain of if{}else if{} when one of them is true and executes its code block the rest of the else if{} blocks are skipped and you "continue with the rest of the loop".

Or are you asking: if there are some statements in the else{} block and after doing the first one or two you want to skip the rest of the statements in the block? Use a nested if inside the else{} block to control which statements are executed.

Basically, I need to check to see if the name that I read in is even in the ArrayList. If it isnt in the ArrayList, that is an indicator that it will never pass that name and continue with the rest of the data, and end up messing up the output. So, I want to ignore that name and move on. If it is in the ArrayList, it means I havent reached the data for that name yet, and I need to insert a new set of data in its alphabetical order.

Re: Breaking If/Else Statements

I cant directly see the condition at any one point. I need to search the entire ArrayList to see if the String currName value is the Name variable of any of the objects in the list ArrayList. So as far as I know, that means I cant use the contains method in the ArrayList class, which means I have to go through the entire ArrayList and see if any of the object's Name variable is the same as currName. I dont know how to detect a NOT Condition for an entire arraylist without search each object.