2014年8月4日月曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 4(Putting Some Order in Your Data: Arrays)、CODE MAGNETS(p.139)をSwiftで考えてみる。

CODE MAGNETS(p.139)

コード(Xcode)

main.swift

//
//  main.swift
//  sample139
//
//  Created by kamimura on 8/4/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

var products = ["Choo Choo Chocolate", "Icy Mint", "Cake Batter", "Bubblegum"]
var hasBubbleGum = [false, false, false, true];
var i = 0;
var m = hasBubbleGum.count

println("while loop")
while i < m {
    if hasBubbleGum[i] {
        println("\(products[i]) contains bubble gum")
    }
    i += 1
}

println("for loop(for/in)")
for i in 0..<m {
    if hasBubbleGum[i] {
        println("\(products[i]) contains bubble gum")
    }
}

入出力結果(Console Output)

while loop
Bubblegum contains bubble gum
for loop(for/in)
Bubblegum contains bubble gum
Program ended with exit code: 0

0 コメント:

コメントを投稿