If (표현식)

Specifies one or more statements to execute if an expression evaluates to true.

If (Expression)
{
    Statements
}

논평

An If statement that contains an expression is usually differentiated from a traditional If statement such as if FoundColor != Blue by enclosing the expression in parentheses, as in if (FoundColor != "Blue"). However, this is not strictly required, as any If statement which does not match any of the legacy if patterns is assumed to contain an expression. In particular, the following are also common ways of writing an If (expression):

Known limitation: For historical reasons, If (expression) actually accepts a numeric parameter rather than a pure expression. For example, if %MyVar% is equivalent to if MyVar. This can be avoided by always enclosing the expression in parentheses.

If 서술문의 표현식이 참으로 평가되면 (빈 문자열 또는 숫자 0만 아니면 어떤 결과도 참이므로), 그 줄 또는 그 밑의 블록이 실행됩니다. Otherwise, if there is a corresponding Else statement, execution jumps to the line or block underneath it.

If an If owns more than one line, those lines must be enclosed in braces (to create a block). 그렇지만, 한 줄만 IF 또는 If에 속해 있다면, 활괄호는 선택적입니다. 이 페이지 아래의 예제들을 참조하십시오.

The space after if is optional if the expression starts with an open-parenthesis, as in if(expression).

The One True Brace (OTB) style may optionally be used with If statements that are expressions (but not traditional If statements). 예를 들어:

if (x < y) {
    ; ...
}
if WinExist("Untitled - Notepad") {
    WinActivate
}
if IsDone {
    ; ...
} else {
    ; ...
}

Unlike an If statement, an Else statement supports any type of statement immediately to its right.

On a related note, the statement if Var between LowerBound and UpperBound checks whether a variable is between two values, and if Var in MatchList can be used to check whether a variable's contents exist within a list of values.

표현식, 할당 표현식 (:=), if var in/contains, if var between, IfInString, 블록, Else, While-회돌이

예제

If A_Index is greater than 100, return.

if (A_Index > 100)
    return

If the result of A_TickCount - StartTime is greater than the result of 2*MaxTime + 100, show "Too much time has passed." and terminate the script.

if (A_TickCount - StartTime > 2*MaxTime + 100)
{
    MsgBox 너무 많은 시간지 경과하였습니다.
    ExitApp
}

This example is executed as follows:

  1. If Color is the word "Blue" or "White":
    1. Show "The color is one of the allowed values.".
    2. Terminate the script.
  2. Otherwise if Color is the word "Silver":
    1. Show "Silver is not an allowed color.".
    2. Stop further checks.
  3. 그렇지 않고:
    1. Show "This color is not recognized.".
    2. Terminate the script.
if (Color = "Blue" or Color = "White")
{
    MsgBox 컬러는 허용된 값 중 하나입니다.
    ExitApp
}
else if (Color = "Silver")
{
    MsgBox 회색은 허용하지 않는 색입니다.
    return
}
else
{
    MsgBox 이 색은 인지할 수 없습니다.
    ExitApp
}

A single multi-statement line does not need to be enclosed in braces.

MyVar := 3
if (MyVar > 2)
    MyVar++, MyVar := MyVar - 4, MyVar .= " test"
MsgBox % MyVar  ; Reports "0 test".