break statements

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  char stringOne[80];
	char stringTwo[80];

	char* pointerOne;
	char* pointerTwo;

	pointerOne = stringOne;
	pointerTwo = stringTwo;

	cout << "Enter first string : \n";
	cin >> stringOne;

	cout << "Enter second string : \n";
	cin >> stringTwo;

        while (*pointerOne && *pointerTwo)
	{
		if (tolower(*pointerOne) != tolower(*pointerTwo))
		{
			break;
		}
			
		else
		{
			pointerOne++;
			pointerTwo++;
		}
	}

	if (!*pointerOne && !*pointerTwo)
	{
		cout << "String are the same except for possible case differnces";
	}
	else
	{
		cout << "strings differ \n";
	}


	return 0;
The break refers to the whole if-else structure block and it's use here is unclear and potentially ambiguous reflecting bad design.

You could/should re-design the test and so avoid the break altogether.
Hello DonnaPin,


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

http://www.cplusplus.com/doc/tutorial/control/#break

The example is better to help understand the "break".

Andy
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.
@DonnaPin,
The best expert exposition on break is at https://en.cppreference.com/w/cpp/language/break. The examples there are very clear in showing where the (final) destination of the break is.
As can be seen the ambiguities and uncertainties arising from the break statement
There's nothing ambiguous or uncertain about it at all. The rule is very simple and it's a very useful part of the language.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
       bool andthis = true;
       while (*pointerOne && *pointerTwo && andthis)
	{
		if (tolower(*pointerOne) != tolower(*pointerTwo))
		{
			andthis = false;
		}
			
		else
		{
			pointerOne++;
			pointerTwo++;
		}
	}

or

       while (*pointerOne && *pointerTwo)
	{
		if (tolower(*pointerOne) != tolower(*pointerTwo))
		{
			goto ugly;
		}
			
		else
		{
			pointerOne++;
			pointerTwo++;
		}
	}

ugly:
Last edited on
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.
So there we have it. You don't know. But I don't answer to bulging rules-based colostomy bags.
Topic archived. No new replies allowed.