- Learn Swift by Building Applications
- Emil Atanasov
- 143字
- 2021-06-25 22:13:47
The if statements – how to control the code flow
This is how we can branch our code logic based on some data stored in a variable:
let num = 5
if num % 2 == 0 {
print("The number \(num) is even.")
} else {
print("The number \(num) is odd.")
}
The general pattern of an if statement is organized as follows:
var logicalCheck = 7 > 5
if (logicalCheck) {
//code which will be executed if the logical check is evaluated to true
} else {
//code which will be executed if the logical check is evaluated to false
}
We know that the if clause gives us huge freedom to shape the code that will be executed (evaluated). An application may handle many different cases, but only the code that fulfills the conditions encoded in our solution will be triggered.
推薦閱讀
- Node.js+Webpack開發實戰
- INSTANT OpenCV Starter
- INSTANT FreeMarker Starter
- Vue.js入門與商城開發實戰
- 實戰Java程序設計
- OpenCV for Secret Agents
- Python高級編程
- The DevOps 2.4 Toolkit
- 嵌入式Linux C語言程序設計基礎教程
- SQL Server on Linux
- Mastering Bootstrap 4
- 絕密原型檔案:看看專業產品經理的原型是什么樣
- 大話程序員:從入門到優秀全攻略
- Developing RESTful Web Services with Jersey 2.0
- 軟件自動化測試實戰解析:基于Python3編程語言