Go (programming language) - Go Decision Making - Go If...else Statement - Go (programming language)

What is an if...else statement in Go language?

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

The syntax of an if...else statement in Go programming language is:

The if block of code is executed, if the Boolean expression evaluates true, otherwise else block of code will be executed.

Flow Diagram

if else statement

Example

The above code is compiled and executed, the result produced appears as -

The if...else if...else Statement

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

When using if , else if , else statements there are few points to keep in mind:

  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax

The syntax of an if...else if...else statement in Go programming language is:

Example

The above code is compiled and executed; the result produced appears as -

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Go (programming language) Topics