954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

switch + if ... is it possible?

Hi,

I tried searching but I don't know how exactly to describe my question in 2-3 words so I didn't find anything.
Is it possible to do something like this?Just to get the idea... I know it doesn't work:

switch(c){
	case '1':{ (...) ; break;}
	case '2':{ (...) ; break;}
	case '3':{ (...) ; break;}
	if(f){
		case '4':{ (...) ; break;}
		case '5':{ (...) ; break;}
		case '6':{ (...) ; break;}
	}
}


I can always do it this way:

if(f){
	switch(c){
		case '1':{ (...) ; break;}
		case '2':{ (...) ; break;}
		case '3':{ (...) ; break;}
		case '4':{ (...) ; break;}
		case '5':{ (...) ; break;}
		case '6':{ (...) ; break;}
	}
}
else{
	switch(c){
		case '1':{ (...) ; break;}
		case '2':{ (...) ; break;}
		case '3':{ (...) ; break;}
	}
}


Or put the if check in cases 4,5,6...
Isn't there anything simpler and better to do?

d_panayotov
Newbie Poster
5 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
switch(c){
		case '1':{ (...) ; break;}
		case '2':{ (...) ; break;}
		case '3':{ (...) ; break;}
	}
	if(f){
		switch(c){
			case '4':{ (...) ; break;}
			case '5':{ (...) ; break;}
			case '6':{ (...) ; break;}
		}
	}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

You could always take advantage of the short-circuit operators.

switch (argc) {
        case 1: { printf("1\n"); break; }
        case 2: { printf("2\n"); break; }
        case 3: { printf("3\n"); break; }
        case 4: { (f) && printf("4\n"); break; }
        case 5: { (f) && printf("5\n"); break; }
        case 6: { (f) && printf("6\n"); break; }
    }
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: