"If"의 두 판 사이의 차이

 
(사용자 4명의 중간 판 24개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{lowercase title}}
{{lowercase title}}


[[분류: 제어문]]
==Bash==
==Bash==
[[category: Bash]]
[[분류:Bash]]
<source lang='bash'>
{{다른뜻|Bash if}}
<syntaxhighlight lang='bash'>
He="John Smith"
He="John Smith"
if [ "$He" = "John Smith" ]
if [ "$He" = "John Smith" ]; then
then
echo "He is John Smith"
echo "He is John Smith"
else
else
12번째 줄: 13번째 줄:
fi
fi
# He is John Smith
# He is John Smith
</source>
</syntaxhighlight>
*[[Bash 숫자 비교]]
*[[Bash 숫자 비교]]


==Cmd==
==C==
[[category: Cmd]]
[[분류: C]]
<source lang='bash'>
{{참고|C if}}
<syntaxhighlight lang='c' run>
#include <stdio.h>
void main()
{
    if (43 > 42)
    {
        printf("hello\n");
    }
    else
    {
        printf("world\n");
    }
}
</syntaxhighlight>
 
==CMD==
[[분류:Cmd]]
<syntaxhighlight lang='bash'>
set my_name=John Smith
set my_name=John Smith
IF "%my_name%"=="John Smith" ECHO my_name is John Smith
IF "%my_name%"=="John Smith" ECHO my_name is John Smith
</source>
</syntaxhighlight>
<syntaxhighlight lang='bash'>
set my_nam=10
IF "%my_nam%" LSS "11" ECHO 10은 11보다 작습니다.
</syntaxhighlight>
* 지연된 환경 변수 확장을 사용하지 않을 경우 비교하는 문자열이 홀수 갯수의 쌍따음표를 포함하면 IF 문은 무조건 실패하므로 주의해야 한다.
* 쌍따음표로 문자열을 감싸는 것은 선택적인 사항이지만, 이 경우 문자열이 비어있거나 이스케이핑이 필요한 문자를 포함할 경우(&와 같은 문자, 단 지연된 환경 변수 확장을 사용할 경우는 예외이다.) 또는 문자열이 한개 이상의 띄어쓰기를 포함할 경우 무조건 실패한다.
 
==Perl==
[[분류:Perl]]
<syntaxhighlight lang='perl'>
if ( 43 > 42 ) {
printf("hello\n");
printf("world\n");
}
 
printf("hello\n") if (43 > 42);
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[분류:PHP]]
<source lang='php'>
{{참고|PHP if}}
<syntaxhighlight lang='php'>
if(43>42) {
if(43>42) {
echo('hello');
echo('hello');
30번째 줄: 67번째 줄:
}
}
// helloworld
// helloworld
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
if(43>42) echo('hello');
if(43>42) echo('hello');
// hello
// hello
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: python]]
[[분류:Python]]
<source lang='python'>
{{참고|파이썬 if}}
<syntaxhighlight lang='python' run>
if 43 > 42:
if 43 > 42:
print('hello')
print('hello')
print('world')
print('world')
# hello
</syntaxhighlight>
# world
<syntaxhighlight lang='python' run>
</source>
<source lang='python'>
if 43 > 42: print ('hello')
if 43 > 42: print ('hello')
# hello
</syntaxhighlight>
</source>


==Ruby==
==Ruby==
[[category: Ruby]]
[[분류:Ruby]]
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
if 43 > 42
if 43 > 42
print 'hello'
print 'hello'
end
end
# hello
# hello
</source>
</syntaxhighlight>
<source lang='Ruby'>
*one-line
<syntaxhighlight lang='Ruby'>
print 'hello' if true
print 'hello' if true
# hello
# hello
print 'world' if false
print 'world' if false
#
</syntaxhighlight>
</source>


==같이 보기==
==같이 보기==
*[[else]]
* [[if ... else]]
*[[else if]]
* [[else if]]
*[[unless]]
* [[switch]]
*[[true]]
* [[unless]]
*[[while]]
* [[true]]
*[[for]]
* [[while]]
*[[==]]
* [[for]]
*[[조건문]]
* [[==]]
*[[제어문]]
* [[ternary operator]]
*[[자바키워드]]
* [[조건문]]
* [[제어문]]
* [[자바키워드]]

2023년 8월 20일 (일) 02:33 기준 최신판

1 Bash[ | ]

  다른 뜻에 대해서는 Bash if 문서를 참조하십시오.
He="John Smith"
if [ "$He" = "John Smith" ]; then
	echo "He is John Smith"
else
	echo "He is not John Smith"
fi
# He is John Smith

2 C[ | ]

#include <stdio.h>
void main()
{
    if (43 > 42)
    {
        printf("hello\n");
    }
    else
    {
        printf("world\n");
    }
}

3 CMD[ | ]

set my_name=John Smith
IF "%my_name%"=="John Smith" ECHO my_name is John Smith
set my_nam=10
IF "%my_nam%" LSS "11" ECHO 10은 11보다 작습니다.
  • 지연된 환경 변수 확장을 사용하지 않을 경우 비교하는 문자열이 홀수 갯수의 쌍따음표를 포함하면 IF 문은 무조건 실패하므로 주의해야 한다.
  • 쌍따음표로 문자열을 감싸는 것은 선택적인 사항이지만, 이 경우 문자열이 비어있거나 이스케이핑이 필요한 문자를 포함할 경우(&와 같은 문자, 단 지연된 환경 변수 확장을 사용할 경우는 예외이다.) 또는 문자열이 한개 이상의 띄어쓰기를 포함할 경우 무조건 실패한다.

4 Perl[ | ]

if ( 43 > 42 ) {
	printf("hello\n");
	printf("world\n");
}

printf("hello\n") if (43 > 42);

5 PHP[ | ]

if(43>42) {
	echo('hello');
	echo('world');
}
// helloworld
if(43>42) echo('hello');
// hello

6 Python[ | ]

if 43 > 42:
	print('hello')
	print('world')
if 43 > 42: print ('hello')

7 Ruby[ | ]

if 43 > 42
	print 'hello'
end
# hello
  • one-line
print 'hello' if true
# hello
print 'world' if false

8 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}