Hello! :p

I don't fully understand how the Fall-Through method works. I’m making a Calendar and I’m using the switch statement to find which season it is for the month.

Here is the book code:

switch (month)
	{
	case 12:
	case 1:
	case 2: Console::WriteLine(S" [Winter]"); break;

	case 3:
	case 4:
	case 5: Console::WriteLine(S" [Spring]"); break;
	
	case 6:
	case 7:
	case 8: Console::WriteLine(S" [Summer]"); break;

	case 9:
	case 10:
	case 11: Console::WriteLine(S" [Fall]");  break;
	}

For example, if the month is 12, does it run everything until there is a semicolon or a break? Can someone tell me what this actually is doing?

-Matt ^_^

Hello,

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. The break statment causes an immediate exit from the switch. Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch.

Falling through cases is a mixed blessing. On the positive side, it allows several cases to be attached to a single action. For example:

int main(void) {
	int tmp = 5;

	switch (tmp) {
		case 1:
		case 2:
		case 3:
			/* Do something if 1, 2, or 3 */
		break;	/* Break statment if case expression matched */
		case 4:
		case 5:
			/* Do something if 4 or 5 */
		break;	/* Break statment if case expression matched */
	}

	return 0;
}

Keep in mind, this implies that normally each case must end with a break to prevent falling through to the next. Falling through from one case to another is not robust, being prone to disintegration when the program is modified. With the exception of multiple labels for a single computation, fall-throughs should be used sparingly, and commented.


- Stack Overflow

I am not trying to be mean, but since you already have the code couldn't you just run it and see what happens?

Thanks a bunch, I had a hard time understand all of your big words! LoL, but I understood. And Prog-Bman, I had tested it and I still didn't understand how it would run something on a nother line... so why does it matter so much anyways? Mocking didn't help anything Bezotch! Hehe!

Thanks again! By-eE

commented: Mocking? Don't plan on stick around any programming forum if this is the limit of what you can take. You made yourself into the Bezotch by complaining -- you are requesting help: try not to forget it. +0
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.