What does the identifier "break" mean and how would it be used? Thanks.

Recommended Answers

All 7 Replies

The Break statement is used to change the flow of control. Is it use inside a while, for, do/while, or switch structure. And it causes the program to exit that structure immediatly, executing the first statement
after that structure.
You can use break statement to escape early from a loop or to skip the rest of a switch structure.

Thank you... I have never used a "Switch" statement so not sure how that would be used either.

I'll give you an example:

Let's assume you have asked the user for an age to be inputed, and you have stored
that in a variable named age. Now, you want to display some remarks according to
that age.
Here's the switch structure to the rescue.

switch( age )    /* the variable age is being evaluated */
    {
        case 1: /* if age is 1 */
            printf("Congratulation for that first year");
            break;
        case 18: /* if age is 18 */
            printf("Now you really have an opinion; you can vote");
            break;
        case 21: /* if age is 21 */
            printf("You have come to age, have a beer");
            break;
        case 50: /* if age is 50 */
            printf("Life starts now");
            break;
        case 65: /* if age is 65 */
            printf("Retire would you?");
            break;
        default: /* any other age will get this */
            printf("You age doesn't matter, keep living");
    }

As you can see, switch is perfect to be use instead of many if/elses in this case.
The key is to remember that the value evaluated has to be an integer expression, and the case is an integer literal.

Also, if you forget to include the break statement after any case and that evaluates true, the following case will be executed too.

Also, if you forget to include the break statement after any case and that evaluates true, the following case will be executed too.

Oh, that would be all the cases after that, not just the following case.

Here is an interesting code snippet:

int main ()
{
    int ch = 66;

    switch (ch)
    {
        case 1:
            cout << "In one" << endl;
            break;
        case 2:
            cout << "In two" << endl;
            break;
        case 3:
            cout << "in three" << endl;
            break;
        default:
            cout << "in default" << endl;
        case 4:
            cout << "In four" << endl;
        case 5:
            cout << "in Five" << endl;
    }
    getchar ();
}

Oh, that would be all the cases after that, not just the following case.

You are correct as always. However, I would like to correct myself a little futher, by saying that it will execute down the line of cases until encountes a break statement.

>it will execute down the line of cases until encountes a break statement.
Or until it reaches the end of the switch() statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.