You are on page 1of 18

Advance Computer Programming – GO

Lecture – 02
GO language Control Structures

Instructor: Syed Mujtaba Hassan


Email: mujtaba.hassan@riphah.edu.pk
Session: Fall 2022
Riphah School of Computing and Innovation (RSCI)
Riphah International University, Lahore
Control structures - GO Language
The control structures of Go are related to those of C but differ in important ways.
There is no do or while loop, only a slightly generalized for; switch is more
flexible; if and switch accept an optional initialization statement like that of for;
break and continue statements take an optional label to identify what to break or
continue; and there are new control structures including a type switch and a
multiway communications multiplexer, select. The syntax is also slightly
different: there are no parentheses and the bodies must always be brace-
delimited.
if
In Go a simple if looks like this:

if x > 0 {
return y
}
Continue…

Mandatory braces encourage writing simple if statements on multiple lines. It's


good style to do so anyway, especially when the body contains a control statement
such as a return or break.
Since if and switch accept an initialization statement, it's common to see one used
to set up a local variable.

if err := file.Chmod(0664); err != nil {


log.Print(err)
return err
}
Golang - if...else Statement

The if....else statement allows you to execute one block of code if the specified
condition is evaluates to true and another block of code if it is evaluates to false.
if condition {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Continue…
package main
import (
"fmt"
)
func main() {
x := 100
if x == 100 {
fmt.Println("Lahore")
} else {
fmt.Println("Islamabad")
}
}
Continue…
for
The Go for loop is similar to C but not the same as C. It unifies for and while and
there is no do-while. There are three forms, only one of which has semicolons.
// Like a C for
for init; condition; post { }
// Like a C while
for condition { }
// Like a C for(;;)
for { }
Short declarations make it easy to declare the index variable right in the loop.
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
Nested for loop - GO Language
package main
import "fmt"
func main() {
for outer := 0; outer < 5; outer++ {
if outer == 3 {
fmt.Println("Breaking out of outer loop")
break // break here
}
fmt.Println("The value of outer is", outer)
for inner := 0; inner < 5; inner++ {
if inner == 2 {
fmt.Println("Breaking out of inner loop")
break // break here
}
Continue…

fmt.Println("The value of inner is", inner)


}
}
fmt.Println("Exiting program")
}

In this program, we have two loops. While both loops iterate 5 times, each has a
conditional if statement with a break statement. The outer loop will break if the
value of outer equals 3. The inner loop will break if the value of inner is 2.
Switch Statement - GO Language
A switch statement is a shorter way to write a sequence of if - else statements. It runs the
first case whose value is equal to the condition expression.

Go's switch is like the one in C, C++, Java, JavaScript, and PHP, except that Go only
runs the selected case, not all the cases that follow. In effect, the break statement that is
needed at the end of each case in those languages is provided automatically in Go.
Another important difference is that Go's switch cases need not be constants, and the
values involved need not be integers.

Go language supports two types of switch statements:

 Expression Switch

 Type Switch
Expression Switch
Expression Switch
Expression switch is similar to switch statement in C, C++, Java language. It
provides an easy way to dispatch execution to different parts of code based on the
value of the expression.

Syntax:

switch optstatement; optexpression{


case expression1: Statement..
case expression2: Statement..
...
default: Statement..
}
Expression Switch Example
package main
import "fmt"
func main() {
switch day:=4; day{
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
default:
fmt.Println("Invalid")
}
}
Type Switch
Type switch is used when you want to compare types. In this switch, the case
contains the type which is going to compare with the type present in the switch
expression.

Syntax:

switch optstatement; typeswitchexpression{


case typelist 1: Statement..
case typelist 2: Statement..
...
default: Statement..
}
Type Switch Example
package main
import "fmt"
func main() {
var value interface{}
switch q:= value.(type) {
case bool:
fmt.Println("value is of boolean type")
case float64:
fmt.Println("value is of float64 type")
case int:
fmt.Println("value is of int type")
default:
fmt.Printf("value is of type: %T", q)
}}
Continue Statement
The continue statement in Go programming language works somewhat like
a break statement. Instead of forcing termination, a continue statement forces
the next iteration of the loop to take place, skipping any code in between.

In case of the for loop, continue statement causes the conditional test and
increment portions of the loop to execute.

Syntax
The syntax for a continue statement in Go is as follows −

continue;
Continue…
package main Flow Diagram
import "fmt"
func main() {
/* local variable definition */
var a int = 20
/* do loop execution */
for a < 40 {
if a == 12 {
/* skip the iteration */
a = a + 1;
continue;
}
fmt.Printf("value of a: %d\n", a);
a++;
}
}
goto statement - label

It’s used to move execution to the labeled statement:


i := 0
Start:
fmt.Println(i)
if i > 2 {
goto End
} else {
i += 1
goto Start
}
End:
Continue…
package main
import "fmt"
func main() {
s1 := []int{1, 2, 5, 6, 9, 10}
jumpTo:
for i := 0; i < 3; i++ {
fmt.Printf("\n\ni is %d", i)
for _, j := range s1 {
if j%2 == 0 {
jumpTo
} continue
fmt.Printf("\nj is %d", j)
}
}
}
Continue…
package main
import "fmt"
func main() {
s1 := []int{1, 2, 5, 6, 9, 10}
jumpTo:
for i := 0; i < 3; i++ {
fmt.Printf("\n\ni is %d", i)
for _, j := range s1 {
if j%2 == 0 {
break jumpTo
}
fmt.Printf("\nj is %d", j)
}
}
}

You might also like