当前位置:网站首页>Conditional judgment (while loop & for & switch loop & if else judgment)
Conditional judgment (while loop & for & switch loop & if else judgment)
2022-04-22 23:53:00 【~Abandoned memories】
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 The following conditions can omit the parentheses 2. The brace after the condition can't be omitted */
if The latter condition can only be Bool type
while
var num = 5
while num > 0
{
print("num is \(num)")
num -= 1
} // Print the 5 Time
var num = -1
repeat
{
print("num is \(num)")
} while num > 0 // Print the 1 Time
1.repeat-while amount to C In language do-while
2. Here there is no num-- , Because from Swift3 Start , Remove the self increasing ( ++ )、 Self reduction ( -- ) Operator
for
Closed interval operators :a…b, a <= Value <= 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 The default is let, If necessary, it can be declared as var
for var i in 1...3
{
i += 5
print(i)
} // 6 7 8
for _ in 1...3
{
print("for")
} // Print the 3 Time
Semi open interval operator :a…<b, a <= Value < b
for i in 1..<5
{
print(i)
} // 1 2 3 4
for – Interval operators are used on arrays
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names [0...3]
{
print(name)
} // Anna Alex Brian Jack
Unilateral interval : Let the interval go as far as possible in one direction
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
Interval type
let range1: ClosedRange<Int> = 1...3
let range2: Range<Int> = 1..<3
let range3: PartialRangeThrough<Int> = ...5
character 、 Strings can also use interval operators , But default cannot be used in for-in 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 To ~ It covers all the things that might be needed ASCII character
let characterRange: ClosedRange<Character> = "\0"..."~"
characterRange.contains("G") // true
Interval values with intervals
let hours = 11
let hourInterval = 2
// tickMark The value of : from 4 Start , Add up 2, No more than 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 You can't put curly braces after it {}
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
// Default can not write break , It doesn't run through the conditions behind it
fallthrough
Use fallthrough Can achieve penetration effect
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 You have to make sure you can handle all the situations
/* switch Be careful : 1.switch It must be ensured that all situations can be handled 2.case ,default At least one following statement ; 3. If you don't want to do anything , Add one break that will do ; 4. If you can guarantee that all the situations have been handled , You don't have to use default */
// If you can guarantee that all the situations have been dealt with , You don't have to use default
enum Answer {
case right, wrong }
let answer = Answer.right
switch answer
{
case Answer.right:
print("right")
case Answer.wrong:
print("wrong")
}
Compound condition
switch Also support Character、 String type
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
Interval matching 、 Tuple matching
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
// You can use underscores _ Ignore a value
// About case Matching problems , Belongs to pattern matching ( Pattern Matching ) The category of
Value binding
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
// When necessary, let It can also be changed to 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
// Add up all the positive numbers
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0
{
// Use where To filter num sum += num
}
print(sum) // 60
Tag statement
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)")
}
}
版权声明
本文为[~Abandoned memories]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222352442995.html
边栏推荐
- [leetcode] binary tree, maximum depth of 104 binary tree, diameter of 543 binary tree, maximum path and sum of 124 binary tree
- [transaction management]
- 【leetcode】二叉树,226翻转二叉树,116填充二叉树节点的右侧指针,114将二叉树展开为链表
- 根据宽度计算文字大小
- The latest MySQL tutorial is easy to understand
- 程序设计语言基础(1)
- 2022-04-22:给你一个大小为 m x n 的矩阵 board 表示甲板,其中,每个单元格可以是一艘战舰 ‘X‘ 或者是一个空位 ‘.‘ ,返回在甲板 board 上放置的 战舰 的数量。 战舰
- PCIe reference clock architecture
- matlab-day01
- 【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
猜你喜欢
![[leetcode] binary tree, 226 flip the binary tree, 116 fill in the right pointer of the binary tree node, and 114 expand the binary tree into a linked list](/img/ea/dcecef82651f15ed1be3821bf8a063.png)
[leetcode] binary tree, 226 flip the binary tree, 116 fill in the right pointer of the binary tree node, and 114 expand the binary tree into a linked list

PCIe reference clock architecture

LabVIEW implements application stop or exit
![FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决](/img/dd/d8068792911be2d04a04eb4c1a158c.png)
FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决

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

【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)

“亿”点点技术情怀

FPGA (V) one of RTL codes (cross clock domain design)
![[dvcon2020] software brothers shout: Hardware brothers, please be personal](/img/52/153bf53416f9f8af6a504b70283752.jpg)
[dvcon2020] software brothers shout: Hardware brothers, please be personal

Traceup | a powerful tool to reduce stress and achieve more success and help you get rid of work anxiety
随机推荐
51 single chip microcomputer learning_ 4-1 nixie tube display
SystemVerilog verification - Test Platform preparation guide learning notes (2): process statements and subroutines
Typora style tuning
Failed to execute goal on project xxxxx
Share two recent classic OJ questions (arrange subsequences + find the longest continuous number string in the string)
根据高度计算文字大小
Failed to execute goal on project xxxxx
【leetcode】二叉树,104二叉树的最大深度,543二叉树的直径,124二叉树的最大路径和
ssh: connect to host 172.31.8.132 port 22: Connection refused
字體自適應
“亿”点点技术情怀
Write a Tetris game in C language
[perseverance challenge] PCIe daily question and answer (table of contents)
LabVIEW implements application stop or exit
[experience sharing] share mangopapa's paper learning experience
【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
[note] PCIe ltssm status transition
FPGA(六)RTL代码之二(复杂电路设计1)
SystemVerilog verification - Test Platform writing guide learning notes (3): connecting design and test platform
3D旋转动画