Swift control flow statements

In the Swift programming language, control flow statements are used to control the execution sequence of a program. These statements allow us to execute different blocks of code based on conditions, repeat blocks of code, or jump to specified code locations. This article will introduce in detail the commonly used control flow statements in Swift and provide corresponding source code examples.

  1. if statement

The if statement is used to selectively execute a block of code based on a condition. Its basic syntax is as follows:

if condition {
   
    
    
    // 如果条件为真,执行这里的代码
} else {
   
    
    
    // 如果条件为假,执行这里的代码
}

Among them, conditionis a Boolean expression. If its value is true, ifthe code in the code block will be executed; otherwise, elsethe code in the code block will be executed. Here is an example:

let num = 10

Guess you like

Origin blog.csdn.net/qq_33885122/article/details/132987779