swift-制御文テスト問題の答え

1847 ワード

タイトル

初めての答え


(2014.7.30)
/*1*/

for n in 1...5 {
    println(n)
}

for n in ["gog","name","sex"]{
    println(n)
}

for (index,value) in [1:"sdf",2:"sdd"]{
    println(index)
}

for chars in "nsdfsfd" {
    println(chars)
}


/*2*/
var a = 10
while a>0{
    a--
}
/*3*/
var n = 3
do {
    println("hell")
    n--
}while n>0

/*4*/

// : break。case 

/*5*/

var flag = 5

switch flag {
case 1,2:
    println("this is 1 or 2")
default:
    println("sdfd")

}
/*6*/
switch flag {
case 1..4:
    println("sdfd")
case 4...5:
    println("in 4...5")
default:
    println("ss")
}

var t = (1,2)
switch t {
case (1,3):
    println("1,2")
case (3,4):
    println("3,4,5")
case (_,2):
    println("second is 2")
case (1,_):
    println("first is 1")
case (0...2,0...3):
    println("niubi")
default:
    println("unknow")

}


/*7*/

// : case , case 


switch t {
case (let x,let y):
    println("x = \(x),y=\(y)")
default:
    println("sdfd")
}


/*8*/
t = (1,1)
switch t {
case (let x,let y) where x == y:
    println("x=y")
default:
    
    println("sdfds")

}

/*9*/


//fallthough



switch t {
case (let x,let y) where x == y:
    println("x=y")
    fallthrough
default:
    
    println("sdfds")
    
}

/*10*/
// , 


/*11*/
// c 1 
var c = 2
one:for g in 1...5{

two:switch c {
case 2:
    println(c)
    break one
default:
    
    println("sdfds")
    break two
}
    println("two over")
    
}
println("one over")
let y = 3
three:while y==3 {
    for n in 1...7 {
        if n==4 {
            break three
        }
        println(n)
    }
}
println("three")