Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Go lang - Get Started
  • Go lang Overview
  • Install Go
  • Go Syntax
  • Create Go Program
  • Go DataTypes
  • Go Variables
  • Go Operators
  • Go if-else Statement
  • Go Switch Statement
  • Go For Loop
  • Working with Functions
  • Go Arrays
  • Go Slice
  • Go Struct
  • Go Map
  • Go Pointers
  • Go Packages
  • Go Modules
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Go If-else Statements

The if statement in Go lang is a decision-making statement that controls the execution flow of the program based on the boolean condition. Here, you will learn about if, elseif, else, and nested if-else statements in Go lang.

if Statement

The if statement contains a boolean condition followed by a single or multi-line code block to be executed. At runtime, if a boolean condition evaluates to true, then the code block will be executed, otherwise not.

Syntax: If Statement
if(condition) {
    //code block to be executed when if condition evaluates to true
}

The following example demonstrates the if statements.

Example: if Statements
package main
import "fmt"

func main() {    
    x := 8
    y := 10

    if (x < y) {
        fmt.Println( "x is less than y")
    }

    if (x > y) {
        fmt.Println( "x is greater than y")
    }
}
Try it
Output:
x is less than y

Above, the first if statement will be executed because the condition x < y evalutes to true. The code block of the second if statement will not be executed because the condition x > y evalutes to false.

Note that the condition expression must return a boolean value, true or false; otherwise compiler will give an error.

Example: Boolean Condition
package main
import "fmt"

func main() {
    x := 8
    y := 10

    if (x + y) {
        fmt.Println( "x is less than y")
    }
}
Try it
Output:
# example ./prog.go:9:8: non-boolean condition in if statement

The starting code block brace { must be specified after the if clause. Go compiler will throw an error if specified in the new line after the if clause.

Example: Starting Code Block
package main
import "fmt"

func main() {
    x := 8
    y := 10

    if (x < y) { // correct
        fmt.Println( "x is less than y")
    }

    if (x > y) 
    {  //error
        fmt.Println( "x is greater than y")
    }
}
Try it

The boolean condition can be specified without the parentheses ( ) but the braces are required, as shown below.

Example: if Statements
package main
import "fmt"

func main() {
    x := 8
    y := 10

    if x < y { 
        fmt.Println( "x is less than y")
    }

    if x > y { 
        fmt.Println( "x is greater than y")
    }
}
Try it

Like for loop, the if statement can start with a statement to execute before the condition where you can declare a variable whose scope would be limited to the code block until the end of the if.

Example: if with a short statement
package main
import "fmt"

func main() {
    x := 8

    if y := 10; x &lt; y { 
        fmt.Println( "x is less than y")
    }
}
Try it
Output:
x is less than y

else Clause

Use the else clause after the if statement to execute the code block when the if condition evaluates to false. The else clause must come after the if block. Only one else block is allowed after the if statement.

Syntax:
if condition {
    // executed when the if condition is true
} else {
    // executed when the if condition is false
}

The following demonstrates the if else statement.

Example: if else Statement
package main
import "fmt"

func main() {
	x := 8
	y := 10
	
    if ( x &lt; y) {
	    fmt.Println( "x is less than y")
	} else {
	    fmt.Println("x is greater than or equal to y")
	}
}
Try it
Output:
x is less than y

The else keyword must come after the closing brace }; otherwise the compiler will give an error.

Example: if else Statement
package main
import "fmt"

func main() {
	x := 8
	y := 10
	
    if ( x &lt; y) {
	    fmt.Println( "x is less than y")
	} 
    else { //error
	    Fmt.Println("x is greater than or equal to y")
	}
}
Try it

Although, the starting brace of else clause can be specified in the new line.

Example: if else Statement
package main
import "fmt"

func main() {
	x := 8
	y := 10
	
    if ( x &lt; y) {
	    fmt.Println( "x is less than y")
	} else 
    { // valid
	    fmt.Println("x is greater than or equal to y")
	}
}
Try it

else if statement

Use if clause after the else clause to execute a code block based on a condition when the first if condition evalutes to false. You can use multiple else-if statements between the if clause and else clause.

Example: else if Statement
package main
import "fmt"

func main() {
	temp := 20
	
    if (temp  &lt;  15) {
	    fmt.Println("Cold")
	} else if (temp < 30) {
	    fmt.Println("Warm")
	} else {
	    fmt.Println("Hot")
	}
}
Try it
Output:
Warm

In the above example, temp < 15 is false, so the compiler will check next else if condition temp < 30, which is true. So, it will execute the else if code block only.

You can have multiple else-if blocks where only one else-if block will be executed whose condition evalutes to true.

Example: Multiple else if Statement
package main
import "fmt"

func main() {
	temp := 35
	
    if (temp  &lt;  15) {
	    fmt.Println("Cold")
	} else if (temp < 30) {
	    fmt.Println("Moderate")
    } else if (temp < 40) {
	    fmt.Println("Warm")
	} else {
	    fmt.Println("Hot")
	}
}
Try it
Output:
Warm

Nested if Statements

Go supports if else statements inside another if, else, or else if code blocks. The nested if statements make the code more readable.

Syntax:
{
if(condition1){
   if(condition2){
        // code block to be executed when 
        //      condition1 and condition2 evaluates to true
    }
    else if(condition3) {
        if(condition4) {
            // code block to be executed when 
            //      only condition1, condition3, and condition4 evaluates to true
        } else if(condition5)
        {
            // code block to be executed when 
            //      only condition1, condition3, and condition5 evaluates to true
        } else {
            // code block to be executed when 
            //      condition1, and condition3 evaluates to true 
            //      condition4 and condition5 evaluates to false
        }
    }
}

The following example demonstrates the nested if condition.

Example: Nested if else Statements
package main
import "fmt"

func main() {
    x := 8
    y := 10

    if (x != y) {
        if (x < y) {
            fmt.Println("x is less than y");
        } else if (x > y) {
            fmt.Println("x is greater than y");
        }        
    } else {
            fmt.Println("x is equal to y");
        }
}
Try it
Output:
x is less than y
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.