Namespaces
Variants
Actions

Difference between revisions of "intro/control/switch"

From cppreference.com
(mt)
 
Line 1: Line 1:
=== The {{tt|switch}} statement ===
+
{{title|The {{tt|switch}} statement}}
  
 
The {{tt|switch}} statement takes an integer and executes the corresponding set of statements.
 
The {{tt|switch}} statement takes an integer and executes the corresponding set of statements.
Line 36: Line 36:
 
Will print 1 to the standart output: (2*6) / 4 - 2 equals 1, therefore the {{c|case 1:}} block is executed.
 
Will print 1 to the standart output: (2*6) / 4 - 2 equals 1, therefore the {{c|case 1:}} block is executed.
  
=== The {{tt|break}} statement ===
+
==The {{tt|break}} statement==
  
 
Note the {{c|break;}} command after each block has executed in the examples above. This keyword is necessary because {{c|switch}} only jumps to the correct {{c|case}} line, but does not return. Therefore, if you removed the {{tt|break}} commands in the given example, {{c|switch}} would jump to {{c|case 1:}} and go on to {{c|case 2:}} and every other following {{tt|case}} block unless it reaches either the closing bracket for the entire {{tt|switch}} block or it encounters a {{c|break;}}, forcing it to leave the {{tt|switch}} block.
 
Note the {{c|break;}} command after each block has executed in the examples above. This keyword is necessary because {{c|switch}} only jumps to the correct {{c|case}} line, but does not return. Therefore, if you removed the {{tt|break}} commands in the given example, {{c|switch}} would jump to {{c|case 1:}} and go on to {{c|case 2:}} and every other following {{tt|case}} block unless it reaches either the closing bracket for the entire {{tt|switch}} block or it encounters a {{c|break;}}, forcing it to leave the {{tt|switch}} block.
Line 42: Line 42:
 
This usually is not what you want, so make sure you don't forget your {{tt|break}}s.
 
This usually is not what you want, so make sure you don't forget your {{tt|break}}s.
  
=== The {{tt|default}} statement ===
+
==The {{tt|default}} statement==
 
A typical {{tt|unsigned int}} variable can take values up to over 4 billion, and you certainly won't be able to cover 4 billion different possibilities with specific {{tt|case}} statements. That's why there is the {{tt|default}} statement, which is somehow similiar to the {{tt|else}} statement: If there is no {{tt|case}} statement covering the specified number available within the current {{tt|switch}} block, the {{tt|default}} block will be called:
 
A typical {{tt|unsigned int}} variable can take values up to over 4 billion, and you certainly won't be able to cover 4 billion different possibilities with specific {{tt|case}} statements. That's why there is the {{tt|default}} statement, which is somehow similiar to the {{tt|else}} statement: If there is no {{tt|case}} statement covering the specified number available within the current {{tt|switch}} block, the {{tt|default}} block will be called:
  

Latest revision as of 17:02, 22 September 2013


The switch statement takes an integer and executes the corresponding set of statements.

switch(/*Integer value*/)
{
    case 0: //commands
            break;
    case 1: //commands
            break;
    ...
}

The switch statement takes an integer value or a command returning an integer value as a parameter. It then jumps to the case block marked with this number and executes from there on. For instance, if you pass one to the switch statement specified above, it will jump to this line:

case 1: //commands

A little example:

switch((2*6) / 4 - 2)
{
    case 0: std::cout << 0;
            break;
    case 1: std::cout << 1;
            break;
    case 2: std::cout << 2;
            break;
}

Will print 1 to the standart output: (2*6) / 4 - 2 equals 1, therefore the case 1: block is executed.

The break statement

Note the break; command after each block has executed in the examples above. This keyword is necessary because switch only jumps to the correct case line, but does not return. Therefore, if you removed the break commands in the given example, switch would jump to case 1: and go on to case 2: and every other following case block unless it reaches either the closing bracket for the entire switch block or it encounters a break;, forcing it to leave the switch block.

This usually is not what you want, so make sure you don't forget your breaks.

The default statement

A typical unsigned int variable can take values up to over 4 billion, and you certainly won't be able to cover 4 billion different possibilities with specific case statements. That's why there is the default statement, which is somehow similiar to the else statement: If there is no case statement covering the specified number available within the current switch block, the default block will be called:

switch(...)
{
    case 0: ...
    default: ...
}

If a number not equal to zero is passed, the default block will be called.