Go if else Statement

In this tutorial you will learn about the Go if else Statement and its application with practical example.

Go if Statement

If statement allows a block of code to be executed only when a specified condition is true. An if statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be true or false.

Syntax:-

Here, you can omit the parentheses () but the curly braces {} are mandatory.

Example:-

Output:-

go_if_statement

Multiple test expressions can also be combined using && (AND) and || (OR) operators as following –

Go if…else Statement

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.

Syntax:-

Example:-

Output:-

go_if_else_statement

Go Ladder if..else..if Statement

The if..else..if Ladder statement is used to select one of among several blocks of code to be executed.

Example:-

Output:-

go_ladder_if_else

Go Nested if Expression

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

Example:-

Output:-

go_nested_if_statement

Go 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.

Example:-

Output:-

go_if_with_short_statement

Note:- Use of parentheses in case of short statement results in a syntax error.

In this tutorial we have learn about the Go if else Statement and its application with practical example. I hope you will like this tutorial.