본문 바로가기
PROGRAMMING CODE/SWIFT

[Swift] 기초 예제 1

by daye_ 2023. 5. 31.

 

 //code1
 
 let arr : Array<String> = ["a","b","c","d","e"]

for i in 0 ... 4 {
    print("환영합니다 \(arr[i])")
}

 //code2
 
 let arr : Array<String> = ["a","b","c","d","e"]

 for i in 0 ... 4 {
     if i<3 {print("환영합니다 \(arr[i])님!")}
     else {print("반갑습니다 \(arr[i])님!")}
     
 }


//code3
 
let arr : Array<String> = ["a","b","c","d","e"]

for i in 0 ... 4 {
    switch arr[i] {
    case "a":
        print("반가워요 \(arr[i])님!")
    case "b":
           print("환영해요 \(arr[i])님!")
    case "c":
           print("잘지내봐요 \(arr[i])님!")
    case "d":
           print("좋은하루되세요 \(arr[i])님!") 
    case "e":
           print("오랜만이에요 \(arr[i])님!") 
    default:
       print("대상이 없습니다.") 
    }
}

 //code4
 
 let arr : Array<String> = ["a","b","c","d","e"]

 for i in 0 ... 4 {
     switch arr[i] {
     case "a","b","c":
         print("반가워요 \(arr[i])님!")
     case "d","e":
            print("좋은하루되세요 \(arr[i])님!") 
     default:
        print("대상이 없습니다.") 
     }
 }
 
 //code5
 
 var num : Int = 0
 for i in 1 ... 100 {
     num=num+i
 }
 print("\(num)")
 
 //code6
 
 for i in 1 ... 100 {
     if(i%10 == 5) {print("\(i)")}
 }
 
 //code7
 
 let temperature = 54

 switch (temperature) {
     case 0...49 where temperature % 2 == 0 :
         print("Cold and even")
         fallthrough
     case 50...79 where temperature % 2 == 0 :
         print("Warm and even")
         fallthrough
     case 80...110 where temperature % 2 == 0 :
         print("Hot and even")
         fallthrough
     default :
         break
     
 }
 
 //code8
 
 for i in 2 ... 9 {
     for j in 1 ... 9 {
         if i%2 == 0 {print("\(i) x \(j) == \(i*j)")}
     }
     print("\n")
 }

 //code9
 let max : Int = 5

 for i in 0...1{
     for up in 0...max {
         for star in 0...up {
             print("*",terminator: "")
         }
         print()
     }
     for down in (0...max).reversed(){
         for star in 0...down {
             print("*",terminator: "")
         }
         print()
     }
 }
 
//code10
 
 let arr : Array<Int> = [12,8,10,5,7,4]

 for i in 0 ... 5 {
      print("\(i+1) ",terminator: "")
     for stripe in 0...arr[i] {
         print("=",terminator: "")
     }
     print(" \(arr[i])")
 }