Home » Go If-else

Go If

The if statement in Go is used to test the condition. If it evaluates to true, the body of the statement is executed. If it evaluates to false, if block is skipped.

Syntax :

Go if example

Output:

a is even number 

Go if-else

The if-else is used to test the condition. If condition is true, if block is executed otherwise else block is executed.

Syntax :

Go if-else example

Output:

a is even value of a is : 10 

Go If-else example: with input from user

Output:

Enter number: 10 10 is even 

Go If else-if chain

The Go if else-if chain is used to execute one statement from multiple conditions.

We can have N numbers of if-else statement. It has no limit.

The curly braces{ } are mandatory in if-else statement even if you have one statement in it. The else-if and else keyword must be on the same line after the closing curly brace }.

Go If else-if chain Example

Output:

Enter text: 84  A Grade 

Go Nested if-else

We can also nest the if-else statement to execute one statement from multiple conditions.

Syntax

nested if-else example

Output:

Inside nested If Statement  Value of x is : 10 Value of y is : 20 

Next TopicGo Switch

You may also like