answersLogoWhite

0


Best Answer

You can omit the break statement at the end of a case whenever you want execution to flow into the next case, or when the case is the last case.

For instance, if you wanted to test a character regardless of whether it was upper or lower case, you might use the following:

void f(char c)

{

switch (c)

{

case 'a': // execution flows into next case...

case 'A':

// do something

break;

case 'b': // execution flows into next case...

case 'B':

// do something else

}

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

You can omit the break statement at the end of a case whenever you want execution to flow into the next case, or when the case is the last case.

For instance, if you wanted to test a character regardless of whether it was upper or lower case, you might use the following:

void f(char c)

{

switch (c)

{

case 'a': // execution flows into next case...

case 'A':

// do something

break;

case 'b': // execution flows into next case...

case 'B':

// do something else

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When might you omit a break statement from the end of a case in a switchstatement?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a short note on case statement?

'Never use it in itself, always nest it in a switchstatement'


How do you break a loop in a switch case statement?

using break; statement


What is the format of the switch statement?

switch (expression) { case value 1 : [ statement-block 1] [break ;] case value 2 : [ statement-block 2] [break ;] ……. ……. case value N : [ statement-block N] [break ;] [default: [default block] [break;] ] } statement x;


The break statement is required in the default case of a switch selection structure?

Ends the case statement. Without it, any code after where the break; is supposed to be will get executed as well until it does encounter a break; or the end of the switch.Code Example:char cTest = 'a';switch(cTest) {case 'a':/* Code here gets executed. */case 'b': //* Code here gets executed. */case 'c':/* Code here gets executed. */break;case 'd':/* Code here won't be executed. */default:/* Code here won't be executed. */}


What is break in c?

The break statement is used to exit a loop or switch-case.


Why break statement is optional in c plus plus?

You use a break statement to end execution of the enclosing iterator (loop) or switch statement. If omitted at the end of a case label, execution continues to the next case label, including the default case, until a jump statement is encountered (break, goto or return) or the switch statement falls from scope. Sometimes this can be desirable behaviour, thus the use of break is optional. A break statement is always optional at the end of the default case, or the last case label. Its usage in loops is always optional, to allow a loop to end prematurely regardless of the conditional expression that controls the loop.


What is the different between break and continue?

The break statement exits out of the smallest containing loop or switch-case statement. The continue statement transfers control to the next iteration of the smallest containing loop statement.


What optional statement can be used in a case structure to execute statements when all other conditions are false?

default : <statement>; i.e. switch (value) { case 1 : do_this(); break; case 2 : do_that(); break; default : do_whatever(); }


Calling a function from switch in C?

Yes, you can call a function from within a switch statement in C. switch (i) { case 0: function1(i); break; case 1: function2(i); break; default: function3(i); break; } When the function returns, you will still be in the switch statement.


Write an equivalence of switch statement and if statement?

switch(ch) { case '+': ....cout <<"arithmetic operator"; ....break; //<===break is a must /// <====================other cases . . default: // <=======else }


How do you use a switch statement in GML?

Switch Statements are used to generate different outputs of code based on the value of an expression. Switch Statements work as follows:{randomNumber = floor(random(3))+1;switch(randomNumber) {case 1: { } break;case 2: { } break;case 3: { } break;default: { } break;}}This may seem confusing if you are new to GML, so I will give an in-depth explanation. The first line sets the variable randomNumber to a random number between 0 and 2, and adds it by 1 to make it a random number from 1-3. So far the only thing that has gone on in the code is to set a variable to either 1, 2, or 3. This is where the switch statement comes in.switch(randomNumber) {case 1: { } break;case 2: { } break;case 3: { } break;default: { } break;}this is the actual switch statement. You may be wondering what the case statements are for. case statements are always written inside switch statements and do nothing anywhere else. case statements activate when the expression in the switch statement is the same as the value that they are assigned to. Take a look at this switch statement:{rand = floor(random(3));switch(rand) {case 0: {show_message("The Random Value Was 0");} break;case 1: {show_message("The Random Value Was 1");} break;case 2: {show_message("The Random Value Was 2");} break;}} When the values assigned to the case statements are equal to the expression in the switch statement, the case statement will run the code contained in it's brackets. break statements order the switch statement to abort. The reason that you need break statements inside a switch statement is because it keeps the other cases from activating as well. (When one case statement activates, the others do as well.)A final briefing on switch statements is that they are not limited to variables. Take a look at this switch statement.{switch(obj_block.x > x) {case true: {show_message("The Block Is Ahead Of You.");} break;case false: {show_message("You Are Ahead Of The Block.");} break;}} This switch statement returns a true or false value, and the case statements operate accordingly.


How do you turn a if statement to a switch statement?

Old: if (condition) stmt1; else stmt2; New: int cc = (condition)!=0; switch (cc) { case 1: stmt1; break; case 0: stmt2; break; }