English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn to use two conditional statements: if and if ... else to specify the flow of program execution.
In programming, you may want to perform different operations based on whether the specified condition is true or false (only known at runtime). In this case, control flow statements will be used.
The syntax of the if statement in Swift is:
if expression {}} // statements }
expression is a boolean expression (returns true or false).
If the expression evaluates to true, the statements in the If code block are executed.
If the expression evaluates to false, the statements in the if code block are skipped from execution.
let number = 10 if number > 0 { print("数字为正。") } print("此语句始终执行。")
When running the program, the output is:
The number is positive. This statement always executes.
In the above program, we have used the value10The constant number was initialized, and the calculation result of the test expression number>0 is true. Therefore, the statement print("Number is positive.") is executed in the if statement body, which outputs:The number is positive. In the console.
Now, change the value of number to a negative integer. For example-5. The output in this case will be:
This statement always executes.
When number uses the value-5When initializing, the calculation result of the test expression number > 0 is false. Therefore, the Swift compiler skips the execution of the if statement body.
If the value of the test expression is true, the specific part of the code in the if statement is executed. The if statement can have an optional else statement. If the test expression is false, the code within the else statement body is executed.
if-The syntax of the else statement is:
if expression {}} // statements } else { // statements }
let number = 10 if number > 0 { print("数字为正。") } else { print("数字不是正数。") } print("此语句始终执行。")
When running the program, the output is:
The number is positive. This statement always executes.
In the above program, the constant number is initialized with the value 10 initialized, and the test expression (number > 0) evaluates to true. Therefore, the print("数字为正。") statement within the if statement's body will be executed.
This output : The number is positive.In the console, the statements within the else block will be skipped in execution.
Now, change the number value to a negative number. Assume it is -5. The output in this case will be:
The number is not positive. This statement always executes.
When number is -5 When the calculation result of the test expression number > 0 is false, in this case, the statements within the else block will be executed, and the statements within the if block will be skipped.
In Swift, an if..else statement may contain other if..else statements. These are called nested if ... else statements.Ternary operator Instead of if..else statements, it is a shorthand form of if ... else statements.
In Swift, you can also execute a code block in many places. To do this, you can use the if..else..if ladder:
if-else-The syntax of the if statement is:
if expression1 { // statements } else if expression2 { // statements } else if expression3 { // statements } . . else { // statements }
The if statement is executed from top to bottom. Once a test expression is true, the code within the if statement's body is executed. Then, the program control jumps to the next if-else-outside the if ladder.
If all test expressions are false, the code within the else block is executed
The following program checks if number is positive, negative, or 0.
let number = 0; if number > 0 { print("数字为正。") } else if (number < 0) { print("数字为负数。") } else { print("数字为0。") }
When running the program, the output is:
The number is 0.
In the above program, the constant number is initialized with the value 0. Since if executes statements from top to bottom, it first checks if the expression number > 0 evaluates to false.
Then, it checks the next expression number < 0, which also evaluates to false.
Therefore, print("Number is 0.") will execute the statements within the else block, which outputs in the consoleThe number is 0.
在Swift中,if..else语句中可能包含其他的if..else语句。称为嵌套if ... else语句。
In Swift, an if..else statement may contain other if..else statements. These are called nested if ... else statements.In Swift, you can also useYou can also use
Nested if-The syntax of the else statement is:
if expression1 { if expression2 { // statements } else { // statements } } else { if expression3 { // statements } else { // statements } }
This is a program that uses nested if statements to find3a program to find the largest number among
let n1 = -1.0, n2 = 4.5, n3 = -5.3 if n1 >= n2 { if n1 >= n3 { print("The largest number is", n1) } else { print("The largest number is", n3) } } else { if n2 >= n3 { print("The largest number is", n2) } else { print("The largest number is", n3) } }
When you run the above program, the output will be:
The largest number is 4.5