Part 2 - Golang Basics (Loops and if-else)

Part 2 - Golang Basics (Loops and if-else)

ยท

4 min read

This is a continuation of the ongoing Golang series. In this part, I will explain loops, if-else, break and continue statements in Golang.

Although it is a straightforward topic but is a crucial topic and it will help you to create logic of your own, again if you know other programming languages it will be a real cakewalk for you.

Table of content

  1. If-else

  2. Loops

If-Else

We use if-else statements when we have to decide on something just like when we have to go to market or not. Let's take an example,

  • Problem statement 1: In a family, there are 3 members of different ages, Their mother asks them to do some work according to their age if anyone's age is below 18 they have to work 'x' amount of work and others will work 'y'.

      package main
    
      import "fmt"
    
      func main() {
          var member1 int = 20
          if member1 < 18 {
              fmt.Println("YOU WILL DO 'X' AMOUNT OF WORK")
          } else {
              fmt.Println("YOU WILL DO 'Y' AMOUNT OF WORK")
          }
      }
    

    NOTE: Here fmt package will help you to print in the terminal.

  • Problem statement 2: In a family, there are 3 members of different ages, Their mother asks them to do some work according to their age if anyone age is below 18 they have to work 'x' amount of work, those are above or equal 18 but below 20 years old they will work 'Y' and those over age 20 will work 'y'.

      package main
    
      import "fmt"
    
      func main() {
          var member1 int = 20
          if member1 < 18 {
              fmt.Println("YOU WILL DO 'X' AMOUNT OF WORK")
          } else if member1 >= 18 && member1 < 20 {
              fmt.Println("YOU WILL DO 'Y' AMOUNT OF WORK")
          } else {
              fmt.Println("YOU WILL DO 'Z' AMOUNT OF WORK")
          }
      }
    
  • We can set a value while we check using if cond. Here is an example,

      package main
    
      import "fmt"
    
      func main() {
          if member1 := 20; member1 < 18 {
              fmt.Println("YOU WILL DO 'X' AMOUNT OF WORK")
          } else {
              fmt.Println("YOU WILL DO 'Y' AMOUNT OF WORK")
          }
      }
    

Loops

  • Loops help us to traverse a range of numbers. It will be best if I take an example of traversing through 1 to 10

      package main
    
      import "fmt"
    
      func main() {
          for num := 1; num <= 10; num++ {
              fmt.Println(num)
          }
      }
    

    Although we don't have any more types of loop in Golang we can achieve while loop type feature in Golang with for loop. Let's take an example of that,

      package main
    
      import "fmt"
    
      func main() {
          num := 1
          for num <= 10 {
              fmt.Println(num)
              num++
          }
      }
    
  • We can use the loop to traverse slice/array using the range keyword or normal array indexing way.

      package main
    
      import "fmt"
    
      func main() {
          var slice = []int{1, 2, 3, 4, 5}
          // ARRAY INDEXING WAY TO TRAVERSE
          for i := 0; i < len(slice); i++ {
              fmt.Println(slice[i])
          }
          // "Range" WAY TO TRAVERSE
          for _, num := range slice {
              fmt.Println(num)
          }
      }
    

    Range keyword returns (index, value) from an array. we can ignore by using an underscore.

  • Break statement: Let's say we want to loop through a series of numbers from 0 to 10 and we will break the loop if the condition's current number is 5. Here is an example,

      package main
    
      import "fmt"
    
      func main() {
          for num := 1; num <= 10; num++ {
              if num == 5 {
                  break
              }
              fmt.Println(num)
          }
      }
    

Continue statement: Let's say here instead of breaking in condition 5 we want to skip number 5 then we will use the continue statement.

package main

import "fmt"

func main() {
    for num := 1; num <= 10; num++ {
        if num == 5 {
            continue
        }
        fmt.Println(num)
    }
}

And that's it, It's really easy, isn't it? Golang is meant to be an easy and lightweight programming language, Will be bringing more tutorials for the same ๐Ÿ˜ƒ.

ย