hello guys,

take a look on this piece of code please

http://db.tt/BwcGOr8g

My question is why when the number == 5 it still prints all the other cout's of the other numbers (4,3,2,1).

if the number == 5 then when line 16 saying "case 4: cout.." but the case is not 4 it is 5, so why does it still prints out "nice pick" ?

2. There is no break after the statement for case 5, hence there is fall through to the other cases.

3. Hi,

But although there is no break line, the number 4 or 3 does not have any meaning? Although it is not the case of 4 (in this e.g. It's 5) it will still print what has to be printed in case of the number 4?

4. Originally Posted by Jonathan Yaniv
But although there is no break line, the number 4 or 3 does not have any meaning?
It is not that they have no meaning; it is just that control did not jump to them, but rather control reached them from the previous statement.

Originally Posted by Jonathan Yaniv
Although it is not the case of 4 (in this e.g. It's 5) it will still print what has to be printed in case of the number 4?
Yes, because control falls through until it encounters a break or the end of the switch is reached.

5. Originally Posted by Jonathan Yaniv

Code:
```//Switch case block starts
switch (color) {
case 1:
printf("Red\n");
break;
case 2:
printf("Blue\n");
//break;
case 3:
printf("Yellow\n");
break;
case 4:
printf("Indigo\n");
break;

default:
}
//Switch case block ends```
A switch case statement can consists of a number of case as case 1: case 2: .... case n: (n is the variable which receives value from switch statement '(color)', Remember n should be an integral value). Based on value what switch received in 'color' it jumps on that case and process the lines afterwards sequentially unless it find a break.

Notice a commented break after case 2 in above code.
So When you will enter 1, it jumps to case 1: and print Red, process further and there is a break so quit from the Switch case block.
When you enter 2, it jumps to case 2: and print Blue and "as the break is commented" so it process further and it will print Yellow, process further and finds a break hence quit.
When you enter 3, it jumps to case 3: and print Yellow, process further and there is break, so quit.

Switch case statement is simplest condition to understand in "C"

6. Originally Posted by maven
Switch case statement is simplest condition to understand in "C"
But this isn't C; it's C++!