Decision Making

STATEMENT

DESCRIPTION

if Statements

Block of statement executed only when specified test expression is true.

if else Statements

When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.

nested If Statements

When there is an if statement inside another if statement then it is known as nested if else.

switch Statement

A switch statement evaluates an expression against multiple cases in order to identify the block of code to be executed.

Above statements are standard as in many other languages, such as C and Java.

If with short statement

In Go, the conditional expression can be preceded by a simple statement, which is executes before the conditional expression is evaluated, and if it is a declaration statement then the variable declared in the statement will only be available inside the if block and it’s else or else-if sections.

package main

import "fmt"

func main() {

	if x := 3; x%2 == 0 {
		fmt.Printf("%d is even\n", x)
	} else {
		fmt.Printf("%d is odd\n", x)
	}

}

Switch Statement (single cases)

Switch Statement (multiple cases)

Switch Statement - fallthrough

All the following cases are executed after a match is found in the switch statement cases.

Switch Statement - Short Statement

Similar to short statement for if statement.

Switch Statement - No Expression

Last updated

Was this helpful?