Saturday, June 28, 2014

Hello World for non-programmers 新手程式設計師的第一段Swift程式

Update: January 10, 2016 - Add the print commands for Swift 2 and add a link to IBM Swift Sandbox.

Hello World - The first step in learning a new programming language. It usually displays a simple string on the terminal / console.

Execute the code below in IBM Swift Sandbox with the RUN button and try to modify the constants and variables in the code.
IBM Swift Sandbox中按RUN按鈕並嘗試改改程式中的常數及變數

//Swift 1:
//println("Hello World!")
//Swift 2:
print("Hello World!")
//Console output: Hello World!
//No ";" is required

let temperature = 20
let 我的常數 = 50
//Constant 常數
//Support Unicode 支援Unicode編碼 
//Type: Int  //Int整數型態(Integer)
//Initial value is required. 需要初始值
// \n standards for a new line character(return to a new line)
// \n 表示換行字元
print("Add a new line character here.\n")

let helloStr = "Hello"

//Characters included in two quotation marks are known as a string. For example, "Yes" is a string.

var mystr = "Hi ABC"
//Variable 變數
//Type: String //字串型態(Integer)
//Initial value is required. //需要初始值

//Swift 1:
//println("I want to say: \(mystr)")
//Swift 2:
print("I want to say: \(mystr)")
//Console output: I want to say: Hi ABC

mystr += "DE"
//mystr becomes "Hi ABCDE"
//Strings may be "added"

//Swift 1:
//println("I want to say: \(mystr)")
//Swift 2:
print("I want to say: \(mystr)")

//Comments will not be executed by the compiler.

//Single-line comment 

/* Multiple-line
   Comment */

/* Nested
/* Multiline */
   Comments  */
//This is different from C. 巢狀註解
//Good design for quick debugging

//Strings may be joined together with the + sign
let word1 = "The"
let word2 = "End"
print(word1 + " " + word2)

No comments:

Post a Comment