EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Go Programming Language

Control Statement in Go

Decision-making in programming is analogous to decision-making in real life. In programming, a piece of code executes when a specified condition is met. These constructs are often referred to as Control Flow Statements. In Go programming, control statements are employed to guide the flow of execution in the program based on specified conditions. They allow the program’s flow to advance and branch depending on the program’s state.

The decision-making statements in Go programming include:

if Statement

The if statement is the simplest form of decision-making. It determines whether a specific block of code will execute, based on whether a given condition evaluates to true. If the condition is true, the corresponding block is executed; otherwise, it is skipped.

Syntax:

if condition {

   // Code to execute if
   // the condition is true
}

Example:

// Go program to demonstrate
// the usage of the if statement
package main

import "fmt"

func main() {

   // Declare a local variable
   var num int = 450

   // Check the condition using if
   if num > 100 {

      // This block executes if
      // the condition is true
      fmt.Printf("num is greater than 100\n")
   }

   fmt.Printf("The value of num is: %d\n", num)
}

Output:

num is greater than 100
The value of num is: 450

Time Complexity: O(1)
Auxiliary Space: O(1)

if…else Statement

The if statement handles execution when a condition is true, but what if the condition is false? In such cases, the else statement provides an alternative block of code to execute.

Syntax:

if condition {

    // Executes this block if
    // the condition is true
} else {

    // Executes this block if
    // the condition is false
}

Example:

// Go program to demonstrate
// the use of the if…else statement
package main

import "fmt"

func main() {

   // Declare a local variable
   var marks int = 900

   // Check the condition using if
   if marks < 500 {

      // This block executes if the
      // condition evaluates to true
      fmt.Printf("marks are less than 500\n")

   } else {

      // This block executes if the
      // condition evaluates to false
      fmt.Printf("marks are 500 or more\n")
   }
}

Output:

marks are 500 or more

Time Complexity: O(1)
Auxiliary Space: O(1)

Nested if Statement

In Go, nested if statements allow placing one if statement inside another. This enables checking multiple conditions sequentially.

Syntax:

if condition1 {

   // Executes if condition1 is true

   if condition2 {

      // Executes if condition2 is true
   }
}

Example:

// Go program to demonstrate
// the use of nested if statements
package main

import "fmt"

func main() {

   // Declare two variables
   var a int = 250
   var b int = 500

   // Check the first condition
   if a < 300 {

      // If condition1 is true,
      // check the nested condition
      if b < 600 {

         // Executes if both conditions are true
         fmt.Printf("a is less than 300 and b is less than 600\n")
      }
   }
}

Output:

a is less than 300 and b is less than 600

Time Complexity: O(1)
Auxiliary Space: O(1)

if..else..if Ladder

When multiple conditions need to be evaluated, the if..else..if ladder provides a way to do so. The conditions are evaluated sequentially, and as soon as one condition is satisfied, its corresponding block is executed. If none of the conditions are true, the final else block executes.

Important Points:

  • An if statement can have zero or one else, which must come after any else if blocks.
  • An if statement can have zero to multiple else if blocks, which must appear before the else.
  • Once an else if condition is true, no further else if or else blocks are evaluated.

Syntax:

if condition_1 {

     // Executes if condition_1 is true

} else if condition_2 {

    // Executes if condition_2 is true

} else {

    // Executes if none of the conditions are true
}

Example:

// Go program to demonstrate the
// usage of the if..else..if ladder
package main

import "fmt"

func main() {

   // Declare a variable
   var value int = 150

   // Evaluate conditions
   if value == 50 {

      // Executes if condition1 is true
      fmt.Printf("value is 50\n")

   } else if value == 100 {

      // Executes if condition2 is true
      fmt.Printf("value is 100\n")

   } else if value == 150 {

      // Executes if condition3 is true
      fmt.Printf("value is 150\n")

   } else {

      // Executes if none of the conditions are true
      fmt.Printf("value does not match any condition\n")
   }
}

Output:

value is 150

Time Complexity: O(1)
Auxiliary Space: O(1)

End of lesson.