C++ break statements | Sololearn: Learn to code for FREE!

+6

C++ break statements

In the tutorial, it is written that "The break statement's role is to terminate the switch statement." However, I find that this is not true, for instance, in the code below: #include <iostream> using namespace std; int main() { int age = 70; switch (age) { case 16: cout << "Too young" << endl; break; case 42: cout << "Adult" << endl; case 70: cout << "Senior" << endl; default: cout <<"This is the default case" << endl; } return 0; } If the switch statement was truly terminated before case 42, shouldn't there be no output? Instead, the computer outputs: Senior This is the default case ==================================================================== Similarly, the break does NOT terminate the switch statement in the following because the output is 3 (instead of no output): #include <iostream> using namespace std; int main() { int x = 3; switch (x) { case 0: cout << x++; break; case 1: cout << x++; case 2: cout << x++; case 3: cout << x; } return 0; } Am I misunderstanding something about break statements?

7/22/2020 7:58:59 AM

Solus

8 Answers

New Answer

+10

Solus There is only one break statement in the first code which will only execute when age is 16. Since age is 70, the case for 16 will be skipped and break will not be evaluated. Since case 70 is the match, "Senior" will be printed to the standard output. Since case 70 did not have a break statement, the default case will also be evaluated. So... the output you listed is expected. The

+9

Solus You should also be aware that once a case statement is satisfied, all subsequent case blocks in the switch statement will be evaluated as well. If this is not the desired behavior of your code logic, then the break statement should be used in respective case blocks. Here is an example where I moved case 70 to the first spot. https://code.sololearn.com/cFHGVF218246/?ref=app In this code, the output is: Senior Too young Notice that case 42 and default are not evaluated because case 16 has a break statement.

+8

You misunderstood the meaning of switch and break! Let me give you an example of switch without break and justify the output! For example, int a = 10; switch (a) { case 9: cout << "9 "; case 10: cout << "10 "; case 11: cout << "11 "; default: cout << "default "; } output: 10 11 default This is because you didn't put the break statement! The switch statement will output all the cases when it arrives to a correct case See here, 10 was the correct case so it gave 10 but also gave out all the values after 10 even though they maybe wrong! The break statement does not allow this! int a = 10; switch (a) { case 9: cout << "9 "; break; case 10: cout << "10 "; break; case 11: cout << "11 "; break; default: cout << "default "; break; } Output: 10 Here when it arrives 10 and finds that 10 is the correct case, it performs the task and comes out of the switch! So the output is only 10! Now try this, int a = 0 switch (a) { case 0: a++; case 1: a++; case 2: a++ case 5: a++; default: a++; } cout << a; Now, guess the output! Yess! You got it correct output is 5 No matter what the condition is but since there are no break statements so we can say that all the cases after a correct case are true Now try to guess the output if all the conditions had break! Yess! Again correct! The output will be 1

+6

The break is only effective when the correct case or default case is matched.

+4

Namit Jain Very detailed 👏 👏 👏 I think the one without break case is called the the "fall through".

+3

In the nut shell The value of x is matched to the all the case value if it is true it display the message other wise skip . If there is no break statement in that case then fall through takes place. Hope it will help

+1

Without break statement unusual execution of one or more cases will take place.To avoid that break is used.

+1

Boolean== like C applies here...