Solved

break?

Posted on 2004-10-18
30
379 Views
Last Modified: 2010-03-31
hi experts,

would it be correct to use break with an if statement if I wanted to check something inside it then break out if its not true?

like for example
if (something)
{  
   do something
 
   if (something true) then go on doing something
  else exit main if

}//main if

go here after exit

does that work? if not what should I do?

0
Comment
Question by:Samooramad
  • 11
  • 5
  • 5
  • +4
30 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 12341659
break is used in loops. You don't need to do anything special to "else exit main if"

if (x) {
      foo();
      if (y) {
            // do it
      } // nothing between this and main if. If 'y' false, code will next execute at 'bar()"
}
bar(); // do this in any case
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341666
>>break is used in loops.

and in case statements
0
 

Author Comment

by:Samooramad
ID: 12341774
no I mean after the inner if statment there is code I want to be ignored if false..so how do I jump from there to outside the main if statement ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341819
We've got 'x' and 'y' in my code. Are you talking about a further 'z'?
0
 

Author Comment

by:Samooramad
ID: 12341935
hold on I'll check my code again and get back to you.. gh
0
 
LVL 13

Expert Comment

by:kawas
ID: 12342084
MAIN_IF:
    if (something)
    {  
       do something
   
       if (something true) then go on doing something
       
       else


    }//main if

go here after exit
0
 
LVL 13

Assisted Solution

by:kawas
kawas earned 50 total points
ID: 12342097
Oops I pressed tab and enter by accident 8-)

 MAIN_IF:
    if (something)
    {  
       do something
   
       if (something true) then go on doing something
       
       else
            break MAIN_IF;
    }

go here after exit
0
 
LVL 92

Expert Comment

by:objects
ID: 12342857
write it instead like this:

if (something)
{  
   do something
 
   if (something true) then
   {
      go on doing something
   }
}//main if
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12344904
the above code(objects) is fine , use it
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 50 total points
ID: 12346121
>>I wanted to check something inside it then break out if its not true

Although a break is not meant to break out of an if statement,
you *can* use it for that if you place that if statement inside a while(true) loop:

        boolean someCondition1 = true;
        boolean someCondition2 = false;
        while (true) {
            if (someCondition1) {
                System.out.println("Statement 1");
                if (!someCondition2)
                    break;     // <<<<<<<<<<<<<<< To break out of the while loop (and thus the if statement)
                System.out.println("Statement 2");
            } //main if
            break;  // <<<<<<<<<<< Don't forget this one!!! (=to break out of the while(true) in "normal" cases)
        }
        System.out.println("End reached");

prints:
Statement 1
End reached
0
 

Author Comment

by:Samooramad
ID: 12349310
thanks everyone..will try the code and if I cant get it working will post my code
0
 

Assisted Solution

by:sudhindra123
sudhindra123 earned 50 total points
ID: 12355185
break will only be applicable in case of Loops or switch statement.

Such a functionality has not been provided since it would never be required.By modifying code a bit
you can achieve the same functionality..
The Statement1,Statement2 will be executed only when the inner if is true,
so move Statement1,Statement2 in to the inner if and remove the else part
altogether.
if (something)
{  
   do something
 
   if (something true) then go on doing something
  else exit main if
  Statement 1
  Statement2
}//main if

go here after exit
0
 

Author Comment

by:Samooramad
ID: 12355861
objects the thing is the rest of my code after the inner if statement has more if statement that I want to ignore if are false..so the code would be more like
 
if (something)
{  
   do something
 
   if (something true) then
   {
      go on doing something
   }if this statement is false I want to exit main if

if (something true)
 {
  go on doing something
}
}//main if

so the only way I can see solving it is using something like break(the way kawas or zzynx suggested) but some say I shouldn't use break
0
 

Author Comment

by:Samooramad
ID: 12355870
sorry sudhindra123 , i just read your code afer I posted mine..that looks like it would do it.. I'll try that

thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 12355885
> (the way kawas or zzynx suggested)

i'd strongly suggest against either of those methods

> but some say I shouldn't use break

you shouldn't and don't need to

use:

if (something)
{  
   do something
 
   if (something true) then
   {
      go on doing something
      if (something true)
      {
          go on doing something
      }
   }if this statement is false I want to exit main if
}//main if
0
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

 
LVL 86

Expert Comment

by:CEHJ
ID: 12355904
>>
if (something true) then
   {
      go on doing something
   }if this statement is false I want to exit main if

if (something true)
 {
  go on doing something
}
>>

Samooramad, as i mentioned earlier, those statements are really one statement. The block will exit anyway
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12355958
>> i'd strongly suggest against either of those methods
Indeed. If you use them your computer will explode! :°D

I agree that breaking out of the while(true) is no nice code and even not needed.
But no one can't deny it does work, so it IS a possible alternative.
Given as is.
0
 

Author Comment

by:Samooramad
ID: 12355960
Here is my code incase anyone sees a better solution

public void internalFrameClosing(javax.swing.event.InternalFrameEvent e) {
if (true)
       {
         if (! (Saved(variable)))
             {if (sure()) {e.getInternalFrame().dispose();}} //IF THIS STATEMENT IS FALSE GO TO :CONTINUE POINT
         
             else {e.getInternalFrame().dispose();} //close without check
         set variable false;
         set another variable false;
         if (true)source.setForeground(Color.blue);
       }//second if
       else
       { more code  }

         more code to execute in both cases;
}//end of internal frame closing

CONTINUE POINT..or if thats not possible at least something to make it so the rest of the code in internal frame closing will not be executed

sorry this is taking too long but I got confused and figured I need help doing this

thanks for the time
0
 

Author Comment

by:Samooramad
ID: 12355986
meaning that if (sure()) is false I dont want the frame to be disposed and I dont want the rest of the code executed
0
 
LVL 92

Expert Comment

by:objects
ID: 12355988
> I agree that breaking out of the while(true) is no nice code and even not needed.

then why suggest it :)
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 12356013
simplest would just be:

     if (sure()) {
         e.getInternalFrame().dispose();
     } else {
         return;
     }

but you can also do it w/out a return
0
 
LVL 92

Expert Comment

by:objects
ID: 12356038
> if (true)

is this for testing :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12356054
>> meaning that if (sure()) is false I dont want the frame to be disposed and I dont want the rest of the code executed

Well, translated in java that gives:

       if (!sure()) return;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12356070
Does your CONTINUE POINT lies outside your internalFrameClosing() function????
0
 

Author Comment

by:Samooramad
ID: 12357101
>>else {
         return;
why didn't I think of that objects??? :)
I swear I should think abbout posting questions more carefully before actually doing it..this is getting to be embarrasing

thank you everyone
0
 

Author Comment

by:Samooramad
ID: 12357120
>>is this for testing :)
must be ;)
0
 

Author Comment

by:Samooramad
ID: 12357128
>>Well, translated in java that gives:
if (!sure()) return;

I know zzynx, it should have been that simple :)
0
 

Author Comment

by:Samooramad
ID: 12357134
>>Does your CONTINUE POINT lies outside your internalFrameClosing() function????

no comment :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12357142
Thanks for accepting

>>I know zzynx, it should have been that simple
No worries ;°)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12357380
8-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

747 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

15 Experts available now in Live!

Get 1:1 Help Now