YouTip LogoYouTip

Go If Else Statement

# Go Language if...else Statement [![Image 4: Go Language Conditional Statements](#) Go Language Conditional Statements](#) An optional else statement can follow an if statement. The statements in the else block are executed when the boolean expression is false. ### Syntax The syntax for the if...else statement in the Go programming language is as follows: if boolean_expression { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ } If the boolean expression evaluates to true, the block of code immediately following the if statement is executed. If it evaluates to false, the else block is executed. The flowchart is as follows: ![Image 5: Go Language if Statement](#) ### Example Use if else to determine the size of a number: ## Example package main import"fmt" func main(){ /* Local variable definition */ var a int=100; /* Check the boolean expression */ if a <20{ /* If condition is true then execute the following */ fmt.Printf("a is less than 20n"); }else{ /* If condition is false then execute the following */ fmt.Printf("a is not less than 20n"); } fmt.Printf("The value of a is : %dn", a); } The result of the above code execution is: a is not less than 20 The value of a is : 100 [![Image 6: Go Language Conditional Statements](#) Go Language Conditional Statements](#)
← Go Basic SyntaxGo Environment β†’