Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Breaking If/Else Statements

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.

    I'm not sure if I'm explaining it correctly.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Breaking If/Else Statements

    Does this thread help you?

    http://www.javaprogrammingforums.com...ple-loops.html

    If you scroll near the bottom of the forums, you will find links to similar threads
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Breaking If/Else Statements

    How about 'continue':
     
    for ( int i = 0; i < 100; i++ ){
        if ( i % 2 == 0 ){
            continue;
        }
        System.out.println(i);
    }

    Will print all odd numbers between 1 and 100

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

    Could you post some code to demonstrate the problem.
    Last edited by Norm; August 12th, 2010 at 10:59 AM.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Breaking If/Else Statements

    Ok, this is really killing me now.

    In the following code, I have placed a comment where I want to skip the rest of the loop steps if a condition is met.

    for(int x=0;x<workBook.getLastRow()+1;x++)
    	    			{
    	    				String currName = workBook.getText(r+x,0);
    	    				if(contains(list,currName))
    	    				{
    		    				if(!currName.equals(""))
    		    				{
    		    					String currType = workBook.getText(r+x,1);
    			    				if(!list.get(x/4).Name.equals(currName))
    			    				{
    			    					//INSERT SOMETHING HERE TO CHECK IF NAME IS IN ARRAYLIST. IF NOT, SKIP REST OF LOOP AND START AT BEGINNING WITH NEXT VALUE. IF YES, CONTINUE WITH REST OF LOOP AND CONDITIONS
    			    					workBook.insertRange(r+x,workBook.getLastCol()+1,r+x+3,workBook.getLastCol()+1,WorkBook.ShiftRows);
    			    					workBook.setText(r+x,0,list.get(x/4).Name);
    			    					workBook.setText(r+x,1,"Flights per Week");
    			    					workBook.setText(r+x+1,0,list.get(x/4).Name);
    			    					workBook.setText(r+x+1,1,"Seats per Week");
    			    					workBook.setText(r+x+2,0,list.get(x/4).Name);
    			    					workBook.setText(r+x+2,1,"ASMs per Week");
    			    					currName = workBook.getText(r+x,0);
    			    					currType = workBook.getText(r+x,1);
    			    				}
    			    				MarketData market = list.get(x/4);
    			    				System.out.println("Loop Check");
    								ArrayList arr = market.getArray(currType);
    								for(int y=0;y<arr.size();y++)
    								{
    									workBook.setNumber(r+x,c+y,Long.parseLong(arr.get(y).toString()));
    								}
    		    				}
    	    				}
    	    			}

    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.

    Do you understand what I'm trying to say?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Breaking If/Else Statements

    IF NOT, SKIP REST OF LOOP AND START AT BEGINNING
    That sounds like what a "continue" would do.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Breaking If/Else Statements

    As I mentioned above, what is wrong with using continue?

    for(int x=0;x<workBook.getLastRow()+1;x++)
    	    			{
    	    				String currName = workBook.getText(r+x,0);
    	    				if(contains(list,currName))
    	    				{
    		    				if(!currName.equals(""))
    		    				{
    		    					String currType = workBook.getText(r+x,1);
    			    				if(!list.get(x/4).Name.equals(currName))
    			    				{
    			    					if (NOTINLIST){
    			    					    continue;
    			    					}
    			    					workBook.insertRange(r+x,workBook.getLastCol()+1,r+x+3,workBook.getLastCol()+1,WorkBook.ShiftRows);
    			    					workBook.setText(r+x,0,list.get(x/4).Name);
    			    					workBook.setText(r+x,1,"Flights per Week");
    			    					workBook.setText(r+x+1,0,list.get(x/4).Name);
    			    					workBook.setText(r+x+1,1,"Seats per Week");
    			    					workBook.setText(r+x+2,0,list.get(x/4).Name);
    			    					workBook.setText(r+x+2,1,"ASMs per Week");
    			    					currName = workBook.getText(r+x,0);
    			    					currType = workBook.getText(r+x,1);
    			    				}
    			    				MarketData market = list.get(x/4);
    			    				System.out.println("Loop Check");
    								ArrayList arr = market.getArray(currType);
    								for(int y=0;y<arr.size();y++)
    								{
    									workBook.setNumber(r+x,c+y,Long.parseLong(arr.get(y).toString()));
    								}
    		    				}
    	    				}
    	    			}

    You could also label the loop

    OUTER: for ...
        if ( not in list){
            continue OUTER;
       }

    Which is useful for nested loops

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Breaking If/Else Statements

    I tried this:

    boolean checkIfIn = false;
    			    				for(int y=0;y<list.size();y++)
    			    				{
    			    					if(list.get(y).Name.equals(currName))
    			    					{
    			    						checkIfIn = true;
    			    						break;
    			    					}
    			    				}
    			    				if(!checkIfIn)
    			    					continue mainArr;

    But it hasnt made a difference. I think my logic might be flawed.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Breaking If/Else Statements

    Why not continue from where you detected the condition? vs setting a flag, break and then continue?

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.

Similar Threads

  1. not sure what im doing wrong with these if else statements
    By yrvd86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2010, 08:32 PM
  2. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  3. Unreachable statements
    By Marcus in forum Loops & Control Statements
    Replies: 7
    Last Post: December 10th, 2009, 02:02 PM
  4. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM
  5. Breaking out of multiple loops
    By helloworld922 in forum Loops & Control Statements
    Replies: 4
    Last Post: July 1st, 2009, 02:52 PM