I'm going to write the understanding of Go from the basics of programming! Click here for Go language basics ❶ Click here for Go language basics ❷
Divide the process according to whether a certain condition is met For example, change whether to bring an umbrella (processing) depending on tomorrow's weather (conditions)
package main
func main(){
score := 100
if score > 60 {
println("Pass") //Processing when the condition is satisfied
}
}
//console
Pass
If you use an if statement, you can make a conditional branch that says "If XX, do ☓☓".
package main
func main(){
score := 100
if score >= 60 {
println("Pass") //Holds when score is greater than or equal to 60
}
}
//console
Pass
The if statement is described as if conditional expression {processing}
package main
func main(){
score := 70
println(score>60)
println(score>80)
}
//console
true
false
There are two values in "true / false type", "true" and "false"
A conditional expression using a comparison operator is "true" when it holds. If it does not hold, the value is "false"
package main
func main(){
score := 100
if score == 100 { //true when score is 100
}
}
x ==y true when the left and right values are equal
x !=y true when the left and right values are not equal
Comparison operator → Check if they are equal
//Find out if they are equal
x ==y true when the left and right values are equal
x !=y true when the left and right values are not equal
//Compare big and small
x <y true when the right side is larger
x >y true when the right side is smaller
x =<y The right side is larger, when equal, true
x >=y The right side is smaller, when equal, true
package main
func main(){
score := 50
if score > 80 {
println("Pass")
} else {
println("Disqualification") //Executed when the result of the conditional expression is false
}
}
//console
Disqualification
By combining "else" with the if statement You can make a conditional branch such as "If ○○, do ●●, if not, do ▲▲".
package main
func main(){
score := 50
if score == 100 {
println("graduate")
} else if score >= 60 {
println("Pass")
} else {
println("Retest")
}
}
//console
Retest
With else if "If ○○, do ●●, if △△, do ▲▲, If neither is the case, □□ will be performed. "
else if(2)
package main
func main(){
score := 100
if score == 100 {
println("graduate") //Subsequent processing is not executed
} else if score >= 60 {
println("Pass")
} else {
println("Retest")
}
}
//console
graduate
You can write any number of "else if"
It is judged whether the conditions are met in order from the top Only the part that meets the conditions first is processed
else if(2)
package main
func main(){
score := 100
if score == 100 {
println("graduate") //Subsequent processing is not executed
} else if score >= 60 {
println("Pass")
} else {
println("Retest")
}
}
//console
graduate
package main
func main(){
time := 12
if time > 10 && time < 19 {
println("Start work")
}
}
Combining multiple conditional expressions using && True as a whole only if all conditional expressions are true
//Logical operator
true && true true
true && false false
false && true false
false && false false
package main
func main(){
time := 15
if time == 10 || time == 15 {
println("Snack time")
}
}
||When multiple conditional expressions are combined using If even one of the multiple conditional expressions is true, the whole becomes true
//Logical operator
true || true true
true || false true
false || true true
false || false false
package main
func main(){
time := 15
if !(time == 16) {
println("Not a snack time")
}
}
! (Conditional expression) "False" if the conditional expression is "true", "true" if it is "false"
swich condition value{
case value 1:
//processing
case value 2:
//processing
case value 2:
//processing
}
In addition to the if statement, the conditional branch also has a syntax called a switch statement. Processing is executed when the value of the condition matches the value of case
Note that it is easy to forget the colon (:) after the case.
//if statement
x := 8
if x % 2 == 0 {
println("Is an even number")
} else if x % 2 == 1 {
println("Is odd")
}
//switch statement
x := 8
switch x % 2 {
case 0: //match
println("Even") //Run
case 1:
println("Odd")
}
When you want to divide the processing by "what is a certain value (x% 2 this time)" in the switch statement (Especially when there are many branches), it may be easier to write than an if statement
//if statement
year := 1994
if year == 1994 || year == 2005 { //1994 is true
println("The year I was born")
}else if year == 2000 {
println("The year the younger brother was born")
}
//switch statement
year := 1994
switch year {
case 1994,2005: //Matches 1994
println("The year I was born") //Run
case 2000:
println("The year the younger brother was born")
}
Multiple values after case can be specified using commas (,)
If you specify multiple values with commas If the value of switch matches one of them, the processing of that case is executed.
default
//if statement
if rank == 1 {
println("Winner")
} else if rank == 2 {
println("second place")
} else if rank == 3 {
println("3rd place")
} else {
println("Bottom")
}
//switch statement
switch rank {
case 1:
println("Winner")
case 2:
println("second place")
case 3:
println("3rd place")
default:
println("Bottom")
The process to be executed when it does not match any case can be specified in default Similar to else in an if statement