????? :confused:
Printable View
????? :confused:
There is no breaking out of an If statement. You simply put the remainder of the code within that If into another, nested If statement. You would have to test some condition to know whether to break out anyway, and how would you do that if not with an If statement.
BLEH
thats bad :(
cant you use break in c++????
I just dislike having many many if statements inside each other
if I could use "break" then it would just be a short if-statement breaking out (rather than wrapping a whole chunk of code in an if block:( )
Break is for loops or Switch only. Generally speaking you would test a condition to determine whether to break out of a loop, which means that the Break statement would be inside an If statement. It wouldn't be much help if break just exited the If.
Actually, there is a way that I just thought of. You'll like this. It's goto, which is supported by C# as well VB.NET.
Edit:
And C++, and presumably C (edit: which I just confirmed by checking the index of my C text book from university, after blowing the dust off).
hehe yeah I thought of goto
but don't you think it'd be a bad idea? Everyone considers it "evil":D I've always heard I shouldn't use goto statements eh
that's the only solution apparently
Only its misuse is evil. Tends to cause speghetti code if you have 25 of them in a 200 line program.
The goto statement is considered "evil" because people abused it. It was included in programming languages in the first place for a reason, but undisciplined programmers used it wherever they felt like it instead of looking for the best solution. While I get an uneasy feeling myself saying this, if goto is used in only isolated situations then there is no potential for spaghetti code, which is the reason that goto got a bad name. As long as you use other solutions in situations where they are more appropriate, the occasional goto is not a mortal sin.
Break out of an If statment is just as evil or not evil as using a Goto. It is unlogical, and breakes the normal flow of the app. Nothing more or less then that....
I haven't tested this but it just occurred to me that the goto statement might be the answer to one of your earlier threads regarding falling through cases in a switch statement. That is more likely to be considered an abuse of goto than this current application though.
aha good explanation:)Quote:
Originally Posted by jmcilhinney
I think I'll use goto for now
I was trying to break out of it instead of creating a new if block because the if block indents the code and I alreayd have too much indentations (I have too many loops and if statements inside another). I guess the real answer is that I should break my function down into smaller functions to make it simpler:D
thanks for all the replies
Quote:
Originally Posted by MrPolite
I was actualy just going to suggest that when I saw you did it your self. If you have so much logical branching that you have no room for more indentation, then it is definitly time to break up the function.