- Kotlin Programming By Example
- Iyanu Adelekan
- 225字
- 2021-08-27 20:00:10
The if expression
The if expression is used to make a logical decision based on the fulfillment of a condition. We make use of the if keyword to write if expressions:
val a = 1
if (a == 1) {
print("a is one")
}
The preceding if expression tests whether the a == 1 (read: a is equal to 1) condition holds true. If the condition is true, the a is one string is printed on the screen, otherwise nothing is printed.
An if expressions often has one or more accompanying else or else if keywords. These accompanying keywords can be used to further control the flow of a program. Take the following if expression for example:
val a = 4
if (a == 1) {
print("a is equal to one.")
} else if (a == 2) {
print("a is equal to two.")
} else {
print("a is neither one nor two.")
}
The preceding expression first tests whether a is equal to 1. This test evaluates to false so, the following condition has been tested. Surely a is not equal to 2. Hence the second condition evaluates to false. As a result of all previous conditions evaluating to false, the final statement is executed. Hence a is neither one nor two. is printed on the screen.
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- Mastering OpenLayers 3
- JavaScript高效圖形編程
- Visual Basic 6.0程序設計計算機組裝與維修
- Python爬蟲開發(fā):從入門到實戰(zhàn)(微課版)
- Clojure for Domain:specific Languages
- Nginx實戰(zhàn):基于Lua語言的配置、開發(fā)與架構詳解
- 大數(shù)據(jù)分析與應用實戰(zhàn):統(tǒng)計機器學習之數(shù)據(jù)導向編程
- Windows內(nèi)核編程
- 細說Python編程:從入門到科學計算
- Unity 2018 Augmented Reality Projects
- Go語言入門經(jīng)典
- WildFly Cookbook
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- Python應用開發(fā)技術