Go flow controls
Just like any programming language Go also provides flow controls to control the program execution. In this post, I will be discussing if-else, for loop and switch statements.
If-else
If-else statement is used to execute only some specific code when a certain condition is true. You can also provide an else block that gets executed if the condition results in false.
If-else
statements can also be chained together by putting an if statement in the else
block of another if
statement. Similarly, you can put one if
statement in another if
statement and this is called a nested if
statement.
In Go syntax for writing an if
statement is as follows:
if <condition> {
//Your code will come here
}
You can also add an initialization statement just before the condition.
if <initialization>; <condition> {
//Your code will come here
}
Observe that the condition is not enclosed in the parentheses. Also, the else
keyword should come at the same line as the closing braces of if
block.
Note: Variables declared in the if statement are only available inside the if statement.
Code example
package main
import "fmt"
func main() {
// if-else statement
var a int = 6
if a<20{
fmt.Println("A is less than 20")
}else{
fmt.Println("A is greater than 19")
}
// if statement with initialization statement
if x:=10;x > 30 {
fmt.Println("x is greater than 5")
} else if x > 5 {
fmt.Println("x is greater than 5 but not greater than 30")
} else {
fmt.Println("x is 5 or less")
}
// nested if-else
y := 10
if y > 5 {
fmt.Println("x is greater than 5")
if y > 2 {
fmt.Println("x is greater than 2 but not greater than 5")
} else {
fmt.Println("x is 2 or less")
}
}
}
Loop
If you want to repeat something again and again, a loop is all that you need. All programming languages provide loops. Let's start with the for loop.
For loops have three components namely, initialization, condition and lastly update statement.
The syntax for writing for loop in Go is as follows
for <initialization>;<condition>;<updation> {
//Your code comes here
}
Initialization and updation components of the loop are optional.
There is also another kind of loop called the while loop. While loop goes on running as long as the loop condition results in true.
Syntax of while loop in Go
for <condition>{
// Your code comes here
}
Note here also the condition is not enclosed in parentheses
Code example
package main
import "fmt"
func main() {
// Basic for loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// for loop with only condition
count := 5
for ;count>0;{
fmt.Println(count)
count--
}
// while loop
i := 0
for i < 5 {
fmt.Println(i)
i++
}
}
Switch-case
The switch-case statement is a convenient way to write chained if-else statements. To write a switch statement you have to write switch
keyword and provide a variable to it after that you write each case. Depending on which case matches with the variable, the code block gets executed. If none of the cases matches then control moves to the next statement. You can optionally provide a default case that gets executed when none of the cases matches.
For the case value, you can provide a constant, a variable or even an expression. Here is the syntax for writing the switch-case statement:
switch <variable>{
case <value>:
//case code comes here
case <value>:
// case code comes here
...........// more cases can be written here
case default:
// default case code
}
Keep in mind that like other languages(C/C++, java, etc) you do not put a break
at the end of each case. And if one case gets executed, rest of the cases are ignored, if you want to execute the rest of the cases use fallthrough
keyword.
Code example
package main
import "fmt"
func main() {
day := "Tuesday"
switch day {
case "Monday":
fmt.Println("It's Monday.")
case "Tuesday":
fmt.Println("It's Tuesday.")
case "Wednesday":
fmt.Println("It's Wednesday.")
case "Thursday":
fmt.Println("It's Thursday.")
case "Friday":
fmt.Println("It's Friday.")
case "Saturday":
fmt.Println("It's Saturday.")
case "Sunday":
fmt.Println("It's Sunday.")
default:
fmt.Println("Invalid day.")
}
}
Conclusion
So in this blog, I wrote about the control flow statements like if-else, loops and switch-case statements. You can combine these statements with different operators to write complex logic. So this was it for the control flow statement.
For related posts here is the link
For more updates subscribe to my newsletter.
Like, share and comment on this post, it encourages me to keep writing blog
Give your feedback, it helps me to improve.