Hi, I am going through a book that has some small problems and answers, and one of the answers I am slightly struggling to understand. It has to do with a break statement and I am hoping someone could help with explaining it. The code is given below.
The issue I am having is with the break statement in the first if statement. When I went over break statements, it said a break would break you out of the block of code that you are in, so to me that would mean it would break me out of that if statement - only, but it seems to break me out of the whole while loop. Why is this?
When I went over break statements, it said a break would break you out of the block of code that you are in
This is true, but you are looking at the wrong block. The if and else statements may have a set of {}s to define a block is for the if and else, but this does not mean that the "break" is working on the if block. The statement could have been written as:
1 2
if (tolower(*pointerOne) != tolower(*pointerTwo))
break;
With this there are no {}s to define a block, so what would the "break" have to work on.
Another way to look at this is that the if/else is deciding if you continue with the while loop or if you are finished.
The "break" is used for loops not if/else statements. See
When I went over break statements, it said a break would break you out of the block of code that you are in
That is incorrect. The break statement breaks you out of the closest enclosing for, do, while or switch statement. It doesn't matter if the break is directly inside the structure or 10 blocks inside it.
int
main()
{
bool a=true, b=true, c=true;
int i;
for (i=0; i<10; ++i) {
if (a) {
if (b) {
if (c) {
break; // break out of for loop (go to line 16)
}
}
}
}
while (a) {
switch (i) {
case 0:
a = false;
break; // break out of switch (go to line 22)
}
b = false;
}
c = false;
}
As can be seen the ambiguities and uncertainties arising from the break statement except under the few ‘orthodox’ uses render it the same as its eqivalent as a goto, as something to be avoided.
I very rarely use it outside of switches. When you need it, though, the only alternatives are ugly (goto, which is clean but frowned upon, or put in a number of conditions and extra code: an inefficient mess). A normal for loop, you can modify the loop variable to stop without an extra variable, and that is also a bit unclean. Basically, the alternatives all stink, so we have and use break instead. It touches on the 'one use of goto' .. breaking out of many nested loops all the way out of all levels, that needs goto (or variable approach can do it) because break only hits the innermost one.
The rule is very simple and it's a very useful part of the language.
Well, for a simpleton who doesn't understand what the word ambiguity means, the nuances encountered by @OP would be beyond them. C'mon grandpa, you're supposed to empty your geriatric-bag down the sewer, not drink it.
Try Again againtry. There's that wispy thin ego of yours again, so fragile that you're talking nonsense now. So I'll help you dig yourself into another hole. Please tell us what is ambiguous about the break statement. It can be explained in a single sentence. It's one of the clearest things in the language.