Jump to content

Flow-control structures: Difference between revisions

Go explanation
(Go explanation)
Line 358:
: safe-access ( i -- a[i] )
['] myarray CATCH 1 = IF ." Out of bounds!" 0 THEN ;</lang>
=={{header|Go}}==
Not covered here::
* Structures involving for, if, switch, continue, break, fallthrough, or panic. As mentioned in the task description, these structures are covered in other tasks.
* Short-circuit operators. These can be considered flow-control structures, but are also covered in other tasks.
* Flow-control functions in the standard library. Many of these are important, but covering them seems beyond the scope of the task.
===Goto===
Go has goto and labels. The following is an infinite loop:
<lang go>func main() {
inf:
goto inf
}</lang>
===Function call===
Function call works as it does in most languages, transferring execution to to the called function, and returning execution to the following statement upon return from the called function.
 
The return statement returns from a function. Any function can use a return statement. Functions with return values can only return with a return statement. Functions without return values can return by “falling off” the end of the function.
 
The defer statement sets a function or method to be executed upon return from the enclosing function. This is useful when a function has multiple returns. The classic example is closing a file:
<lang go>import "os"
 
func processFile() {
f, err := os.Open("file", os.O_RDONLY, 0)
if err != nil {
// (probably do something with the error)
return // no need to close file, it didn't open
}
defer f.Close() // file is open. no matter what, close it on return
var lucky bool
// some processing
if (lucky) {
// f.Close() will get called here
return
}
// more processing
// f.Close() will get called here too
}</lang>
===Goroutines===
Goroutines are Go’s take on lightweight threads. A goroutine is started with a go statement, which looks like “go” preceding a function call. When the go statement executes, a new goroutine is created, execution in the new goroutine starts with a call to the function named in the go statement, and execution in the calling goroutine continues without interruption. (The main thread of execution is a goroutine itself.)
 
The following program prints a mix of 1’s and 0’s.
<lang go>package main
 
import "fmt"
 
func printOnes() {
for {
fmt.Println("1")
}
}
 
func main() {
go printOnes()
for {
fmt.Println("0")
}
}</lang>
 
A goroutine terminates upon return from the function called in the go statement. Unlike with a regular function call however, it cannot return a value--the calling goroutine has long continued and there is nothing waiting for a return value.
 
Goroutines may not be able to communicate by returning values, but they have other ways. Principal is passing data through channels. Channel operations affect execution when they yield the processor, allowing other goroutines to run, but this does not normally alter flow of execution. The one exception is when channel operations are used in a select statement. A simple use,
<lang go>func answer(phone1, phone2 chan int) {
select {
case <-phone1:
// talk on phone one
case <-phone2:
// talk on phone two
}
}</lang>
Syntax is strongly reminiscent of the switch statement, but rules for flow control are very different. Select will block if no channel operation is possible. If one is possible, it will execute that case. If multiple operations are possible, it will pick one at random.
===Process initialization===
A complete program must have exactly one function named main, which is called on program start up. In addition, a program can have any number of functions named init. These are called before main, but otherwise in unspecified order.
 
=={{header|Haskell}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.