Using continue in a switch statement
-
23-09-2019 - |
문제
I want to jump from the middle of a switch
statement, to the loop statement in the following code:
while (something = get_something())
{
switch (something)
{
case A:
case B:
break;
default:
// get another something and try again
continue;
}
// do something for a handled something
do_something();
}
Is this a valid way to use continue
? Are continue
statements ignored by switch
statements? Do C and C++ differ on their behaviour here?
해결책
It's fine, the continue
statement relates to the enclosing loop, and your code should be equivalent to (avoiding such jump statements):
while (something = get_something()) {
if (something == A || something == B)
do_something();
}
But if you expect break
to exit the loop, as your comment suggest (it always tries again with another something, until it evaluates to false), you'll need a different structure.
For example:
do {
something = get_something();
} while (!(something == A || something == B));
do_something();
다른 팁
Yes, it's OK - it's just like using it in an if
statement. Of course, you can't use a break
to break out of a loop from inside a switch.
Yes, continue will be ignored by the switch statement and will go to the condition of the loop to be tested. I'd like to share this extract from The C Programming Language reference by Ritchie:
The
continue
statement is related tobreak
, but less often used; it causes the next iteration of the enclosingfor
,while
, ordo
loop to begin. In thewhile
anddo
, this means that the test part is executed immediately; in thefor
, control passes to the increment step.The continue statement applies only to loops, not to a
switch
statement. Acontinue
inside aswitch
inside a loop causes the next loop iteration.
I'm not sure about that for C++.
It's syntactically correct and stylistically okay.
Good style requires every case:
statement should end with one of the following:
break;
continue;
return (x);
exit (x);
throw (x);
//fallthrough
Additionally, following case (x):
immediately with
case (y):
default:
is permissible - bundling several cases that have exactly the same effect.
Anything else is suspected to be a mistake, just like if(a=4){...}
Of course you need enclosing loop (while
, for
, do...while
) for continue
to work. It won't loop back to case()
alone. But a construct like:
while(record = getNewRecord())
{
switch(record.type)
{
case RECORD_TYPE_...;
...
break;
default: //unknown type
continue; //skip processing this record altogether.
}
//...more processing...
}
...is okay.
SharePoint Enterprise 2010 검색 PDF 파일을 검색하려면 여기에서 설명한대로 PDF IFilter를 다운로드해야합니다. http://www.foxitsoftware.com/german/products/ifilter/installation.php.
Foxit PDF Ifilter는 지불 한 것이지만 성능은 다른 IFilters보다 낫습니다.
WAQAS에서 이미 설명한 것처럼 SharePoint 2013은 현재 PDF IFilter를 기본적으로 지원합니다. PDF 아이콘은 SharePoint 2013 팜의 추가 구성 또는 설치없이 SharePoint 2013 결과 및 문서 라이브러리에서 기본적으로 지원됩니다.SharePoint 2010 또는 MOSS 2007과 같은 Adobe의 무료 PDF IFilter 또는 Foxit PDF IFilter를 설치할 필요가 없습니다. 이는 단순히 상자 구성에서 간단히 사용할 수 있습니다.
Switch is not considered as loop so you cannot use Continue inside a case statement in switch...