Go if-else statements
Go if statement
The if statement is used to test the condition. If it evaluates to true, the body of the statement is executed. If it evaluates to false, if block is skipped .
Syntax
if bool_expression {
/*
Statements to execute
*/
}
Example :-
package main
import "fmt"
func main() {
var a = 100
if a%2 == 0 {
fmt.Println("a is even")
}
}
Output :-
Go if-else Statement
The if-else statement is used to test the condition. if the condition is true, if block is executed otherwise else block is executed.
Syntax :-
if bool_expression {
/*
Statement that will be executed if and only if the above expression is true.
*/
} else {
/*
statement that should be executed if the if block expression is false.
*/
}
Example :-
package main
import "fmt"
func main() {
var a int = 97
if a%2 == 0 {
fmt.Println("a is even")
} else {
fmt.Println("a is odd")
}
}
Output :-
Go if-else-if
The if-else-if statement is used to execute one statement from multiple conditions. We can have N number of if-else statement.
Example :-
package main
import "fmt"
func main() {
var a int = 82
if a < 0 || a > 100 {
fmt.Println("Either a is less then 0 or greater than 100")
} else if a >= 0 && a < 50 {
fmt.Println("a is either greater than or equal to 0 and less than 50")
} else if a >= 50 && a < 100 {
fmt.Println("a is either greater than or equal to 50 and less then 100")
} else {
fmt.Println("a is equal to 100")
}
}
Output :-
Nested if-else statement
As the name suggest the if-else statement is nested means if statement inside another statement.
The statement inside nested if statement only execute if both the expression are true.
Example :-
package main
import "fmt"
func main() {
var x int = 50
var y int = 60
if x >= 50 {
if y >= 60 {
fmt.Println("Inside nested if statement.")
}
}
}
Output :-
Take input from user and use it inside if-else statement
package main
import "fmt"
func main() {
fmt.Println("Enter Your Marks")
var input int
fmt.Scanln(&input)
if input < 0 || input > 100 {
fmt.Println("Enter valid marks")
} else if input >= 90 && input <= 100 {
fmt.Println("A+ Grade")
} else if input >= 80 && input <= 90 {
fmt.Println("A Grade")
} else if input >= 70 && input <= 80 {
fmt.Println("B Grade")
} else if input >= 60 && input <= 70 {
fmt.Println("C Grade")
} else if input >= 50 && input <= 60 {
fmt.Println("D Grade")
} else {
fmt.Println("Failed!!")
}
}
Output :-
Go Switch statement
The switch statement executes one statement from multiple conditions. It is similar to-else-if statement.
In the switch statement more than one values can be tested in a case, the values are presented in a comma separated list. like: case val1, val2, val3:
Example :-
package main
import "fmt"
func main() {
fmt.Println("Enter Number")
var a int
fmt.Scanln(&a)
switch a {
case 10:
fmt.Println("The value is 10")
case 20:
fmt.Println("The value is 20")
case 30:
fmt.Println("The value is 30")
case 40:
fmt.Println("The value is 40")
default:
fmt.Println("Not 10,20,30 or 40")
}
}
Output :-
In Go break keyword is implicit. So automatic fall-through is not default behavior in Go switch statement.
For fall-through in Go switch statement, use the keyword "fallthrough" at the end of statement.
fallthrough example
package main
import "fmt"
func main() {
fmt.Println("Enter Number")
var a int
fmt.Scanln(&a)
switch a {
case 10:
fmt.Println("The value is 10")
fallthrough
case 20:
fmt.Println("The value is 20")
fallthrough
case 30:
fmt.Println("The value is 30")
fallthrough
case 40:
fmt.Println("The value is 40")
fallthrough
default:
fmt.Println("Default!!")
}
}
Output :-
For Loop in Go
In Go the for loop statement is used for repeating a set of statements number of times.
Do you know for loop is only loop in Go language.
There are two types of for loop in Go.
- Counter-controlled iteration
- Condition-controlled iteration
Counter-controlled iteration example :-
package main
import "fmt"
func main() {
for a := 0; a <= 10; a++ {
fmt.Println(a)
}
}
Output :-
When execution is over, the object created inside loop is destroyed.
Example :-
package main
import "fmt"
func main() {
for a := 0; a <= 10; a++ {
fmt.Println(a)
}
fmt.Println(a)
}
Output :-
See. we are getting error because the "a" object defined in loop is destroyed.
Nested for loop in Go
As the name suggest the loop is inside loop.
package main
import "fmt"
func main() {
for a := 0; a < 3; a++ {
for b := 3; b > 0; b-- {
fmt.Print(a, " ", b, "\n")
}
}
}
Output :-
Infinite for loop in Go.
Infinite for loop means the loop never end it will always be running.
Example :-
package main
import "fmt"
func main() {
for true {
fmt.Println("It runs forever.")
}
}
Output :-
This will run forever. if we want to exit from this loop in terminal window just use Ctrl+c
Condition controlled iteration in for loop
- The for loop statement which has no header is used for condition-controlled iteration.
- It is similar to while-loop in other languages.
Let's see an example :-
package main
import "fmt"
func main() {
a := 1
for a < 100 {
a += a
fmt.Println(a)
}
}
Output :-
goto Statement in Go
The goto statement in Go is a jump statement which is used to transfer the control to other parts of the program.
In go to statement we use label to transfer the control of the program.
goto statement Example :-
package main
import "fmt"
func main() {
var x int
Begin:
fmt.Println("Enter Your age")
fmt.Scanln(&x)
if x <= 17 {
fmt.Println("You are not eligible to vote")
goto Begin
} else {
fmt.Println("You can vote.")
}
}
Output :-
Break statements in Go
Break statement is used to break out the inner most structure in which it occurs. It can be used in for-loop, and also in switch statement. Execution of statement is continued after ending of the structure.
Example of break statement :-
package main
import "fmt"
func main() {
var a int = 1
for a < 10 {
fmt.Println("Value of a is ", a)
a++
if a > 5 {
/*Terminating loop using break*/
break
}
}
}
Output :-
Mostly we use break to break the control flow of nested loop.
Example :-
package main
import "fmt"
func main() {
var a int
var b int
for a = 1; a <= 3; a++ {
for b = 1; b <= 3; b++ {
if a == 2 && b == 2 {
break
}
fmt.Print(a, " ", b, "\n")
}
}
}
Output :-
Continue in Go
Continue in Go is used to skip the remaining part of the loop, and then continues with the next iteration of the loop after checking the condition.
Example :-
package main
import "fmt"
func main() {
var a int = 1
for a < 10 {
if a == 5 {
a = a + 1
continue
}
fmt.Print("a = ", a, "\n")
a++
}
}
Output :-
Go continue example with inner loop
package main
import "fmt"
func main() {
var a int
var b int
for a = 1; a < 3; a++ {
for b = 1; b < 3; b++ {
if a == 2 && b == 2 {
continue
}
fmt.Print("value of a is ", a, " value of b is ", b, "\n")
}
fmt.Print("value of a = ", a, " value of b =", b, "\n")
}
}
Output :-
Constants in Go
A constant in Go contains the data which cannot be changed.
The data of constant can be of type number(integer, float or complex), string or boolean.
Syntax :-
const identifier [type] = value
Example :-
const PI = 3.14
Go Constant Example :-
package main
import "fmt"
func main() {
const PI float64 = 3.14159
var r float64 = 5.0
var area float64
area = PI * r * r
fmt.Println("Area of circle is ", area)
}
Output :-
Type Casting in Go
Type Casting means the data type of variable is converted from one to another.
The value may be lost when the large type is converted to smaller type.
Type Conversion in Go Example :-
package main
import (
"fmt"
"strconv"
)
func main() {
var i int = 100
var f float64 = 15.98
var str1 string = "208"
var str2 string = "13.62"
fmt.Println(float64(i))
fmt.Println(int(f))
newInt, _ := strconv.ParseInt(str1, 0, 64)
fmt.Println(newInt)
newfloat, _ := strconv.ParseFloat(str2, 64)
fmt.Println(newfloat)
}
Output :-
Thank You for Reading
Next blog on Golang is going to be about functions, recursion and Arrays in Go.