Monday, November 12, 2018

Learn Golang Tutorials - if else statement examples

In this blog post, we are going to learn control flow structures - if-else statements in Go Language with examples.
Learn Golang Tutorials - if else statement examples

golang if statements

Like many programming languages, Go Language provides control structures for decision making. if and else statements are one of type.if statements are used to execute a piece of code based on the conditional expressions, if the condition is satisfied, code block will be executed.
This blog post covers examples and syntaxes for the following things
  • simple if statements
  • if shorthand statements
  • if else statements
  • if else nested statements
Here is Syntax for if statements in Golang
if(Conditional Expression) {
Code Block
}
conditional expression is evaluated and always results true or false. the expression can be made of simple boolean values or complex expression created using comparison operators ==,= or !=. the expression can be single or multiple. Multiple expressions are joined with logical operators like &&,||,! operators. Conditional Expressions are not required to be enclosed with parenthesis, But code block must be enclosed with curly braces In golang, curly braces are required for code block which contains sing statements.

simple if statement 

This example evaluates the expression if the expression is true, the code block is executed and return "Event Number test passed " to console The below is an example for simple if the conditional expression
package main

import "fmt"

func main() {
	var numerVariable = 10
	if numerVariable%2 == 0 {
		fmt.Printf("Event Number test passed ")
	}
}
the output of the above code is
Event Number test passed 
below example code give a compilation error, The reason is if statements code block has no curly braces, It throws syntax error: unexpected newline, expecting { after if clause
package main

import "fmt"

func main() {
	var numerVariable = 10
	if numerVariable %2 == 0 
		fmt.Printf("Event Number test passed ")
}
Multiple expression can be combined with logical operators
var number = 2
if  number >= 1 && age <= 10 {
	fmt.Println("Number is between 1 to 10")
}	

if with shorthand statements 

In this, if statement always contains the shorthand statements, before going control to the conditional expression. shorthand statements can contain variable declaration and the scope of the variable are the scope of this block only
func main() {
	var numerVariable = 10
	if result := numerVariable % 2; result == 0 {
		fmt.Printf("Event Number test passed ")
	}
}
if you include shorthand syntax in if statements, the parenthesis is not required. It gives compilation error - syntax error: unexpected :=, expecting ), if parenthesis is in shorthand syntax
func main() {
	var numerVariable = 10
	if (result := numerVariable % 2; result == 0) {
		fmt.Printf("Event Number test passed ")
	}
}

if and else statement 

This syntax contains else block for if statement. if conditional expression is true if the block is executed, otherwise else block is executed.
if (conditional expression){
if true this block of code is executed
}else{
if false block of code is executed
}
example for if else statement
package main

import "fmt"

func main() {
	var numerVariable = 10
	if numerVariable%2 == 0 {
		fmt.Printf("Event Number if block ")
	} else {
		fmt.Printf("Odd Number else block")

	}
}
the output of the above program is
Event Number if block

multiple if else - nested if statements 

if the statement contains if else statements that contain chains of if else statements
package main

import "fmt"

func main() {
	var numerVariable = 1001
	if numerVariable <= 100 {
		fmt.Printf("Number is less than 100")
	} else if numerVariable > 100 && numerVariable <= 1000 {
		fmt.Printf("Number is greater than 100 and less than 1000")
	} else { // else statements
		fmt.Printf("Number is greater than 1000")
	}
}
The output of the above program is
Number is greater than 1000
Compilation error if any line breaks before else statement as like below example the below program gives syntax error: unexpected else, expecting }
func main() {
	var numerVariable = 1001
	if numerVariable <= 100 {
		fmt.Printf("Number is less than 100")
	} else if numerVariable > 100 && numerVariable <= 1000 {
		fmt.Printf("Number is greater than 100 and less than 1000")
    } 
    else { // else statements
		fmt.Printf("Number is greater than 1000")
	}
}

Related article


EmoticonEmoticon