2012-10-18 8 views
3

switch 문에 여러 조건을 추가 할 수 없습니다. switch 문을 사용하여이를 수행하는 방법? 여러 if 조건을 사용해야합니까?스위치 C에서 다중 조건

string oldValue = ".....long string1...."; 
string newValue = ".....long string2...."; 
switch (oldValue.Length && newValue.Length) 
{ 
    case <500 && <500: 
     //insert as is 
     break; 
    case >500 && <500: 
     //split oldValue into array of strings and insert each 
     //insert newValue as is 
     break; 
    case <500 && >500: 
     //insert oldValue As is 
     //split newValue into array of strings and insert each 
     break; 
    case >500 && >500: 
     //split oldValue into array of strings and insert each 
     //split newValue into array of strings and insert each 
     break; 
    default: 
     break; 
} 
+4

좋은 옛 'if' –

답변

3

귀하의 의견을 살펴보면, oldValue의 길이는 oldValue과 관련이 있으며, newValue의 길이는 newValue과 관련이 있습니다.

왜 이것을 두 개의 별도 명령문으로 나누지 않습니까? 아니면 일반적인 방법으로?

5

아니요,이 작업을 수행 할 수 없습니다. switch 문은 값이 아닌 값에 대해 테스트합니다.

대신 if 문을 사용해야합니다.

1

이 작업을 수행 할 수 없으면 ifelse if을 사용해야합니다.

switch은 일반적으로 다른 기술을 사용하여 오른쪽 분기 (jumptable)로 이동합니다. 기본적으로 타겟 위치가 valueswitch(value)으로 계산되는 단일 점프가 아니라 연속적인 비교가됩니다. 세부 사항은 아키텍처에 따라 다릅니다.