当前位置:网站首页>条件判断(whilec循环&for&switch循环&if-else判断)
条件判断(whilec循环&for&switch循环&if-else判断)
2022-04-22 23:52:00 【~废弃回忆 �༄】
if-else
let age = 4
if age >= 22
{
print("Get married")
}else if age >= 18
{
print("Being a adult")
}else if age >= 7
{
print("Go to school")
} else
{
print("Just a child")
}
/* 1.if后面的条件可以省略小括号 2.条件后面的大括号不可以省略 */
if后面的条件只能是Bool类型
while
var num = 5
while num > 0
{
print("num is \(num)")
num -= 1
} // 打印了5次
var num = -1
repeat
{
print("num is \(num)")
} while num > 0 // 打印了1次
1.repeat-while相当于C语言中的do-while
2.这里不用num-- ,是因为从Swift3开始,去除了自增( ++ )、自减( -- )运算符
for
闭区间运算符:a…b, a <= 取值 <= b
let names = ["Anna","Alex","Brian","Jack"]
for i in 0...3
{
print(names [i])
} // Anna Alex Brian "Alex", "Brian", "Jack"]
let range = 1...3
for i in range
{
print(names [i])
} // Alex Brian Jack
let a = 1
var b = 2
for i in a...b
{
print(names [i])
} // Alex Brian
for i in a...3
{
print(names [i])
} // Alex Brian Jack
// i默认是let,有需要时可以声明为var
for var i in 1...3
{
i += 5
print(i)
} // 6 7 8
for _ in 1...3
{
print("for")
} // 打印了3次
半开区间运算符:a…<b, a <= 取值 < b
for i in 1..<5
{
print(i)
} // 1 2 3 4
for – 区间运算符用在数组上
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names [0...3]
{
print(name)
} // Anna Alex Brian Jack
单侧区间 :让区间朝一个方向尽可能的远
for name in names [2...]
{
print(name)
} // Brian Jack
for name in names [...2]
{
print(name)
} // Anna Alex Brian
for name in names [..<2]
{
print(name)
} // Anna Alex
let range = ...5
range.contains(7) // false
range.contains(4) // true
range.contains(-3) // true
区间类型
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...5
字符、字符串也能使用区间运算符,但默认不能用在for-in中
let stringRange1 = "cc"..."ff" // ClosedRange<String>
stringRange1.contains("cb") // false
stringRange1.contains("dz") // true
stringRange1.contains("fg") // false
let stringRange2 = "a"..."f"
stringRange2.contains("d") // true
stringRange2.contains("h") // false
// \0到~囊括了所有可能要用到的ASCII字符
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") // true
带间隔的区间值
let hours = 11
let hourInterval = 2
// tickMark的取值:从4开始,累加2,不超过11
for tickMark in stride(from: 4, through: hours, by: hourInterval)
{
print(tickMark)
} // 4 6 8 10
switch
var number = 1
switch number {
case 1:
print("number is 1")
break
case 2:
print("number is 2")
break
default:
print("number is other")
break
} // number is 1
//case、 default后面不能写大括号{}
var number = 1
switch number
{
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
} // number is 1
//默认可以不写break ,并不会贯穿到后面的条件
fallthrough
使用fallthrough可以实现贯穿效果
var number = 1
switch number
{
case 1:
print("number is 1")
fallthrough
case 2:
print("number is 2")
default:
print("number is other")
}
// number is 1
// number is 2
switch必须要保证能处理所有情况
/* switch注意点: 1.switch必须保证能处理所有情况 2.case ,default后面至少一条语句; 3.如果不想做任何事,加个break即可; 4.如果能保证已处理所有的情况,也可以不必使用default */
// 如果能保证已处理所有情况,也可以不必使用default
enum Answer {
case right, wrong }
let answer = Answer.right
switch answer
{
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
}
复合条件
switch也支持Character、 String类型
let string = "Jack"
switch string
{
case "Jack":
fallthrough
case "Rose":
print("Right person")
default:
break
} // Right person
let character: Character = "a"
switch character
{
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
} // The letter A
switch string
{
case "Jack", "Rose":
print("Right person")
default:
break
} // Right person
区间匹配、元组匹配
let count = 62
switch count
{
case 0:
print("none")
case 1..<5:
print("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
} // dozens of
let point = (1, 1)
switch point
{
case (0, 0):
print("the origin")
case (_, 0):
print("on the x-axis")
case (0, _):
print("on the y-axis")
case (-2...2, -2...2):
print("inside the box")
default:
print("outside of the box")
} // inside the box
//可以使用下划线 _ 忽略某个值
//关于case匹配问题,属于模式匹配( Pattern Matching )的范畴
值绑定
let point = (2, 0)
switch point
{
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with a y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
} // on the x-axis with an x value of 2
//必要时let也可以改为var
where
let point = (1, -1)
switch point
{
case let (x, y) where x == y:
print("on the line x == y")
case let (x, y) where x == -y:
print("on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
} // on the line x == -y
// 将所有正数加起来
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0
{
// 使用where来过滤num sum += num
}
print(sum) // 60
标签语句
outer: for i in 1...4
{
for k in 1...4
{
if k == 3
{
continue outer
}
if i == 3
{
break outer
}
print("i == \(i), k == \(k)")
}
}
版权声明
本文为[~废弃回忆 �༄]所创,转载请带上原文链接,感谢
https://blog.csdn.net/eastWind1101/article/details/122440500
边栏推荐
- The implementation principle and function of closure and memory leakage
- [leetcode] binary tree, in-depth understanding of the first, middle and last order
- [dvcon2020] acceleration of low power design level verification based on signoff abstract model
- Online fox U3D client game configuration loading failed couldn't connect to server
- [turtle confession collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." More joy and peace for the rest of your life ~ (with 3 sourc
- LabVIEW implements application stop or exit
- IOT identity awareness
- 51 单片机学习_4-2 数码管动态显示
- 合并两个有序的数组
- 【newcoder】20220422周赛
猜你喜欢

FPGA (VI) RTL code II (complex circuit design 1)

Django指定数据库的时候报No module named ‘django_test.settings‘

LabVIEW实现应用程序停止或退出

北京航空航天大学开通CnOpenData试用

Mung bean sprouts on the 22nd day of home

unbuntu18.04 安装 gamit10.71 problem solution

51 单片机学习_4-2 数码管动态显示

Visual studio always conflicts with Sogou input method
![[turtle confession collection]](/img/81/b4bacc23691e58e403f1330d0ca7cf.jpg)
[turtle confession collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." More joy and peace for the rest of your life ~ (with 3 sourc

80386 compilation_ Introduction to global description table GDT
随机推荐
【PCIe 实战】SNPS PCIe 开启 SRIS mode
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
matlab-day01
STM32F103 independent watchdog
Solve the error reporting problem of require is not defined
LabVIEW controls computer shutdown, hibernation, logout and restart
Django指定数据库的时候报No module named ‘django_test.settings‘
Magic weapon for easy working time: log in to the server by means of key verification
合并两个有序的数组
Redis的基本使用
[leetcode] binary tree, maximum depth of 104 binary tree, diameter of 543 binary tree, maximum path and sum of 124 binary tree
Reg 正则表达式学习笔记
彻底解决Failed to execute goal on project xxxxx
xpath定位
[dvcon2020] adopt advanced save & restore technology to accelerate simulation turnaround time
【Pygame小游戏】Chrome上的小恐龙竟可以用代码玩儿了?它看起来很好玩儿的样子~
ssh: connect to host 172.31.8.132 port 22: Connection refused
Failed to execute goal on project xxxxx
浅谈LD_PRELOAD劫持
MPP架构概念