In any programming language, control structures help create the important building blocks of programming logic. Therefore, a clear understanding of his syntax is a basic requirement. Anyone familiar with C / C ++ will find many similarities in the control structures provided by Go in general. But note that Go brought some new ideas to the table. This article presents a glimpse into the use of the control structure in Go programming.
Control buildings in Goleng
The control structures provide the means to control or maneuver the flow of the program. Imagine a stream of water flowing down; We can meander through a curved path or perhaps create a loop through which water can pass. However, we can not change its downward flow, but between the beginning and end of the creek, we can play its course or control the flow. Similarly, control structures in Go, such as If Conditions, To Loops, Replace Statements, and so on, help control the flow of the program. It is not possible to change the beginning and the end – or the end of the plan, since a plan that begins in execution must eventually stop somewhere. The control structures simply control the flow of the program between start and stop.
Plans are but expressions of an algorithm, and an algorithm is the logical explanation you provide in the pursuit of solving a specific problem. Syntactically, in programming languages like Go, this control structure actually helps spread the logic to problem solving. There are usually three types of control structures that we find in any programming language. they:
- Continuous flow
- Conditional flow
- Iterative flow
to read: Understanding Math Operators in Go
Continuous flow control structure in Go
Continuous flow is the simplest control structure in Go and follows in a straight path. Imagine a stream of water flowing straight down. In programming, instructions are executed one after the other in a clear sequence. Also, although this may seem like a very elementary pattern, many complex algorithms are expressed in this way and code modules are actually executed in a numbered manner. Consider the following:
Example: Statement 1 Statement 2 Statement 3 Statement 4 Statement 5
Conditional flow control structure in Go
Conditional flows are best perceived as a choice to choose from each other. At some point in the flow of code, we need to decide on a specific path. The logic is expressed in such a way that the result or outcome of the code changes as one path over another is selected. There can be one or more conditions based on the flow of code changing direction. Imagine a junction of a road where we have to decide which way to go. Consider the following pseudo-code:
Example: If Then: Statement 1 Else If then: Statement 2 ... Else Statement 5 End If
Iterative flow control structures in Go
Iterative flow uses the logic of loops. Here, there is one entry point based on certain conditions, which executes the statements within the iterative block. At each iterative stage, he checks the condition to see if he is still satisfied or not and breaks out of the loop only at the stage when the condition is no longer met – or returns Boolean to read: How to use strings in Go A simple illustration of continuous flow is to write a program that counts the natural numbers 1 to 10 and prints each in its own line. The code in Go will be something like this: Or we can write the following Go code instead: This latter syntax is very typical of Go and can not be found in C / C ++, although in terms of appearance, they may look like a distant brother to each other. In any case, the flow of code is continuous. Also note that this type of code is tedious; Imagine if the numbers are huge – so writing a single line every time we need to print is not a particularly effective way to code. In fact, this can be better expressed through iterative flow of code. The iterative flow in Go is realized through a To Disclaimer. This allows for repeated execution of a block of code. So if we rewrite the code above, it will look like this: Notice how To A loop begins with a value operation i: = 1 To start the iteration, the control flow checks the situation, and if it is provided, enters the loop and performs the fmt.Println (i) Disclaimer. After printing once, the control moves to the magnification section of the loop – e i ++ Statement – and again checking if the condition i <= 10 Satisfied or not. If he is satisfied, the code block Inside God To A loop is executed, then switches to i ++ Clause and clause of To loop. This is repeated until the condition no longer holds and eventually comes out of the loop. Phrase it categorically: Level 1: Variable boot (i: = 1 In our case); If you know the C / C ++ loop statements, Are of three types: To, While and do while. Although there are subtleties of convenience in using each of the three, it’s just a choice between equal capability. This confusion of choice is completely erased in Go and provides only one type, viz To loop. Go uses two types of syntax for statements: Normal To Statement (as we have seen) is another For… range Disclaimer. Here’s a quick example of how to use For… range In Go: to read: Arrays and slices in Go As we can see that To The loop has a conditional expression, which acts as an entry guard but there are statements that decide on the flow of the code based on a given condition. God If– Statements do so. Let’s change the plan of printing 1-10 numbers and print odd or even based on conditions: If a number is mozar or Even Determined by the expression i% 2. If the number is divisible by 2 And he has a 0 The rest, the number is Even Otherwise it is yes mozar. Now, based on the outcome of the conditional statement i% 2 == 0 he Right or a lie, The execution path of the code changes. Of course, we can be multiple / nesting Other Statements as follows: The point is that the conditional statements can change the flow of control depending on the outcome of a condition. Here is another example of a Replace Go statement: So there are three basic control structures in Go: simple continuous flow, To Loops for iterative flow and If Terms and Replace Statements. In fact, these three satisfy the need of the programmer. Unlike C / C ++, Go cut the syntax to the point of need instead of getting too many options. However, Go is still a toddler and has miles to go even to compare him to C / C ++. However, it is worth mentioning that programmers love simplicity and Go delivers it properly. Perhaps this is one of the biggest reasons for its popularity. to read: How to fix Go errorsExample:
For I = 1 To N
Statement 1
...
Statement 5
I = I + 1
End For
Example of a continuous flow code in Go
package main
import “fmt”
func main() {
fmt.Println(1)
fmt.Println(2)
fmt.Println(3)
fmt.Println(4)
fmt.Println(5)
fmt.Println(6)
fmt.Println(7)
fmt.Println(8)
fmt.Println(9)
fmt.Println(10)
}
fmt.Println(`1
2
3
4
5
6
7
8
9
10`)
Example of an iterative flow code in Go
package main
import “fmt”
func main() {
for i := 1; i <= 10; i++ {
fmt.Println(i)
}
}
level2: Conditional expression (i <= 10) Which is or Right or a lie;
Step 3: Phrase (i ++, Addition in our case)
Step 4: Code block to execute if the result of the statement of condition (step 2) is correct. Otherwise break what To loop.
Step 5: Repeat order: Step 3 then Step 2 and Step 4.package main
import (
"fmt"
"unicode"
)
func main() {
greetings := "Hello! friends"
for index, ch := range greetings {
fmt.Printf("%c ", unicode.ToUpper(ch))
index++
}
}
Example of a conditional flow code in Go
func main() {
for i := 1; i <= 10; i++ {
if i%2 == 0 {
fmt.Println(i, "is EVEN")
} else {
fmt.Println(i, "is ODD")
}
}
}
if i%2 == 0 {
// do something
} else if i%3 == 0{
//do something
} else if i%5 == 0{
//do something
} else if i%9 == 0{
//do something
}else {
//do something
}
Sometimes multiple Other Conditional statements are an inconvenient way to write code. Instead we can write Go Replace Statements to harness the flow of code, as shown here:num := 10
switch {
case num < 100: fmt.Println("Less than 100") case num > 100:
fmt.Println("greater than 100")
default:
fmt.Println("equal to 100")
}
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
deck := []string{"spade", "heart", "club", "diamond"}
cards := []string{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}
randIndex := rand.New(rand.NewSource(time.Now().UnixNano()))
switch d := deck[randIndex.Intn(4)]; d {
case "spade":
fmt.Println(cards[randIndex.Intn(13)], "SPADE")
case "club":
fmt.Println(cards[randIndex.Intn(13)], "CLUB")
case "heart":
fmt.Println(cards[randIndex.Intn(13)], "HEART")
case "diamond":
fmt.Println(cards[randIndex.Intn(13)], "DIAMOND")
default:
fmt.Println("OOPS!!")
}
}
Recent thoughts on control structures in Go