조건문: 두 판 사이의 차이
내용 삭제됨 내용 추가됨
편집 요약 없음 |
잔글 봇: 틀 이름 및 스타일 정리 |
||
81번째 줄: | 81번째 줄: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
|<syntaxhighlight lang="bash"> |
|<syntaxhighlight lang="bash"> |
||
case $someChar in |
case $someChar in |
||
a) actionOnA; ;; |
a) actionOnA; ;; |
||
x) actionOnX; ;; |
x) actionOnX; ;; |
||
139번째 줄: | 139번째 줄: | ||
| {{yes}} |
| {{yes}} |
||
| {{yes}} |
| {{yes}} |
||
| {{yes|[[switch 문|예]]}} |
| {{yes|[[switch 문|예]]}} |
||
| {{no}} |
| {{no}} |
||
| {{no}} |
| {{no}} |
||
175번째 줄: | 175번째 줄: | ||
| {{yes}} |
| {{yes}} |
||
|- |
|- |
||
|{{rh}}| [[자바 (프로그래밍 언어)|자바]] |
|{{rh}}| [[자바 (프로그래밍 언어)|자바]] |
||
| {{yes}} |
| {{yes}} |
||
| {{yes}} |
| {{yes}} |
||
303번째 줄: | 303번째 줄: | ||
<li> {{note label|choice else if|B|1}} {{note label|choice else if|B|2}} {{note label|choice else if|B|3}} {{note label|choice else if|B|4}} {{note label|choice else if|B|5}}The often-encountered <code>else if</code> in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent ''if then else'' statements combined with a particular source code layout. However, this also means that a distinct else–if construct is not really needed in these languages.</li> |
<li> {{note label|choice else if|B|1}} {{note label|choice else if|B|2}} {{note label|choice else if|B|3}} {{note label|choice else if|B|4}} {{note label|choice else if|B|5}}The often-encountered <code>else if</code> in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent ''if then else'' statements combined with a particular source code layout. However, this also means that a distinct else–if construct is not really needed in these languages.</li> |
||
<li> {{note label|choice const|C|1}} {{note label|choice const|C|2}} In Haskell and F#, a separate constant choice construct is unneeded, because the same task can be done with pattern matching.</li> |
<li> {{note label|choice const|C|1}} {{note label|choice const|C|2}} In Haskell and F#, a separate constant choice construct is unneeded, because the same task can be done with pattern matching.</li> |
||
<li> {{note label|case regexp|D|none}} In a Ruby <code>case</code> construct, [[정규 표현식|regular expression]] matching is among the conditional flow-control alternatives available. For an example, see [http://stackoverflow.com/questions/6803647/how-to-write-a-ruby-switch-statement-case-when-with-regex-and-backreferences this] Stack Overflow question.</li> |
<li> {{note label|case regexp|D|none}} In a Ruby <code>case</code> construct, [[정규 표현식|regular expression]] matching is among the conditional flow-control alternatives available. For an example, see [http://stackoverflow.com/questions/6803647/how-to-write-a-ruby-switch-statement-case-when-with-regex-and-backreferences this] Stack Overflow question.</li> |
||
<li> {{note label|SQL|S|1}}{{note label|SQL|S|2}} SQL has two similar constructs that fulfill both roles, both introduced in [[SQL-92]]. A "searched <code>CASE</code>" expression <code>CASE WHEN cond1 THEN expr1 WHEN cond2 THEN expr2 [...] ELSE exprDflt END</code> works like <code>if ... else if ... else</code>, whereas a "simple <code>CASE</code>" expression: <code>CASE expr WHEN val1 THEN expr1 [...] ELSE exprDflt END</code> works like a switch statement. For details and examples see [[Case (SQL)]].</li> |
<li> {{note label|SQL|S|1}}{{note label|SQL|S|2}} SQL has two similar constructs that fulfill both roles, both introduced in [[SQL-92]]. A "searched <code>CASE</code>" expression <code>CASE WHEN cond1 THEN expr1 WHEN cond2 THEN expr2 [...] ELSE exprDflt END</code> works like <code>if ... else if ... else</code>, whereas a "simple <code>CASE</code>" expression: <code>CASE expr WHEN val1 THEN expr1 [...] ELSE exprDflt END</code> works like a switch statement. For details and examples see [[Case (SQL)]].</li> |
||
</small> |
</small> |
||
311번째 줄: | 311번째 줄: | ||
== 바깥 고리 == |
== 바깥 고리 == |
||
* {{ |
* {{언어링크|en}} [http://everythingfla.com/courses/video/8/69/ IF NOT (ActionScript 3.0)] video |
||
{{토막글|프로그래밍 언어}} |
{{토막글|프로그래밍 언어}} |
2017년 8월 21일 (월) 12:51 판
컴퓨터 과학에서 조건문(條件文)이란 프로그래머가 명시한 불린 자료형 조건이 참인지 거짓인지에 따라 달라지는 계산이나 상황을 수행하는 프로그래밍 언어의 특징이다.
C 언어에서의 조건문
C에서는 if를 사용하여 조건문을 만든다. if의 조건이 참이 아닐 때 수행할 동작은 else 문에서 지정 가능하며, if와 else 사이에 else if를 통해 새로운 조건을 넣을 수 있다. C의 영향을 받은 C++, Java 등도 같은 형식을 따른다.
#include <stdio.h>
int main(void){
int a = 2, c = 5;
if(a == c){
printf("a는 c와 같다.");
}
else {
printf("a는 c와 다르다.");
}
return 0;
}
이 소스는 a라는 변수에 2를 저장하고 c라는 변수에 5를 저장한 뒤, a와 c가 같은지 확인하고 출력한다. 이 소스는 밑의 소스처럼 좀 더 줄일 수 있다.
#include <stdio.h>
int main(void){
int a = 2, c = 5;
a == c ? printf("a는 c와 같다.") : printf("a는 c와 다르다.");
return 0;
}
위의 소스는 삼항 연산자를 사용해 if를 사용한 소스와 같은 동작을 수행하는 프로그램을 작성한 것이다. 그러나 이 소스는 else if처럼 다른 조건에서 동작하는 지의 여부를 알 수 없다.
#include <stdio.h>
int main(void){
int a = 2, c = 5;
if(a > c){
printf("a는 c보다 크다.");
} else if(a < c) {
printf("a는 c보다 작다.");
} else {
printf("a는 c와 같다.");
}
return 0;
}
이 소스는 else if까지 추가시켜서 만든 조건문이다.
case와 switch 문
switch 문 (일부 언어의 경우 case 문이나 다방향 브랜치라고도 함)은 지정된 상수의 값을 비교한 다음, 일치하는 첫 상수에 따라 동작을 취한다.
파스칼: | C: | 셸 스크립트: |
---|---|---|
case someChar of
'a': actionOnA;
'x': actionOnX;
'y','z':actionOnYandZ;
else actionOnNoMatch;
end;
|
switch (someChar) {
case 'a': actionOnA; break;
case 'x': actionOnX; break;
case 'y':
case 'z': actionOnYandZ; break;
default: actionOnNoMatch;
}
|
case $someChar in
a) actionOnA; ;;
x) actionOnX; ;;
[yz]) actionOnYandZ; ;;
*) actionOnNoMatch ;;
esac
|
언어별 비교 참조
프로그래밍 언어 | 구조화된 if | switch 선택 | 산술 if | 패턴 일치[A] | ||
---|---|---|---|---|---|---|
then | else | else–if | ||||
에이다 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
C, C++ | 예 | 예 | 불필요[B] | Fall-through | 아니요 | 아니요 |
C 샤프 | 예 | 예 | 불필요[B] | 예 | 아니요 | 아니요 |
코볼 | 예 | 예 | 불필요[B] | 예 | 아니요 | 아니요 |
에펠 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
F# | 예 | 예 | 예 | 불필요[C] | 아니요 | 예 |
포트란 90 | 예 | 예 | 예 | 예 | 예 | 아니요 |
고 | 예 | 예 | 불필요[B] | 예 | 아니요 | 아니요 |
하스켈 | 예 | 필요 | 불필요[B] | 불필요[C] | 아니요 | 예 |
자바 | 예 | 예 | 불필요[B] | Fall-through[1] | 아니요 | 아니요 |
ECMAScript (자바스크립트) | 예 | 예 | 불필요[B] | Fall-through[2] | 아니요 | 아니요 |
메스메티카 | 예 | 예 | 예 | 예 | 아니요 | 예 |
오베론 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
펄 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
PHP | 예 | 예 | 예 | Fall-through | 아니요 | 아니요 |
파스칼, 오브젝트 파스칼 (델파이) | 예 | 예 | 불필요 | 예 | 아니요 | 아니요 |
파이썬 | 예 | 예 | 예 | 아니요 | 아니요 | 아니요 |
퀵베이직 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
루비 | 예 | 예 | 예 | 예 | 아니요 | case w/ Regexp[D]
|
스칼라 | 예 | 예 | 불필요[B] | Fall-through | 아니요 | 예 |
SQL | 예[S] | 예 | 예 | 예[S] | 아니요 | 아니요 |
비주얼 베이직, 클래식 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
비주얼 베이직 닷넷 | 예 | 예 | 예 | 예 | 아니요 | 아니요 |
윈도우 파워셸 | 예 | 예 | 예 | Fall-through | 아니요 | 아니요 |
각주
- 내용주
- ^ This refers to pattern matching as a distinct conditional construct in the programming language – as opposed to mere string pattern matching support, such as regular expression support.
- 1 2 3 4 5 The often-encountered
else if
in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent if then else statements combined with a particular source code layout. However, this also means that a distinct else–if construct is not really needed in these languages. - 1 2 In Haskell and F#, a separate constant choice construct is unneeded, because the same task can be done with pattern matching.
- ^ In a Ruby
case
construct, regular expression matching is among the conditional flow-control alternatives available. For an example, see this Stack Overflow question. - 1 2 SQL has two similar constructs that fulfill both roles, both introduced in SQL-92. A "searched
CASE
" expressionCASE WHEN cond1 THEN expr1 WHEN cond2 THEN expr2 [...] ELSE exprDflt END
works likeif ... else if ... else
, whereas a "simpleCASE
" expression:CASE expr WHEN val1 THEN expr1 [...] ELSE exprDflt END
works like a switch statement. For details and examples see Case (SQL).
- 참조주
- ↑ Java.sun.com, Java Language Specification, 3rd Edition.
- ↑ Ecma-international.org ECMAScript Language Specification, 5th Edition.
바깥 고리
- (영어) IF NOT (ActionScript 3.0) video
이 글은 프로그래밍 언어에 관한 토막글입니다. 여러분의 지식으로 알차게 문서를 완성해 갑시다. |