Go if...else Statement

Learn via video courses
Topics Covered

Overview

Control statements in Golang are used to control the flow of the program's execution based on specified conditions. These are used to make the execution flow advance and make the flow of code in a boolean expression. In this article, we will look into the different control statements in golang which are widely used.

Introduction

The if-statement can be found in almost all programming languages. It is a fundamental control-flow statement in programming whose main aim is to make decisions, just like how we decide in our real-time scenarios, programs also decide according to the given condition if the statement we have added fulfills our requirement, it moves or executes else not with the boolean expressions, i.e true or false. Let's see the different types of control or decision-making statements which we can be used in golang.

If the Statement

The syntax of the if-statement is straightforward. The if statement is used to convert an expression to a boolean value. The code block executes if the condition is true; else, it doesn't.

Flow Chart:

if-the-statement

Syntax:

For example:

Output:

Try by Yourself!

If..else Statement

The if....else statement enables you to execute a block of code if the given condition is true and another block of code if it's false.

Flow Chart:

if-else-statement-flow-chart

Syntax:

For Example:

Output:

Try By Yourself!

Nested..If

If statements can be freely nested to any depth. However, overly deeply nested constructs might result in poor software design. An if statement can be contained by another if statement. In this section, we will look at an example of a nested if statement.

Flow Chart:

nested-if-flow-chart

Syntax:

For Example:

Output:

In the above code, we have implemented nested if and trying to check that if num = 63 and name = "scaler" is true then it will give us Welcome to Scaler

Try by Yourself!

If..else..if Statement

In general, whatever condition in an if or else if returns true, the corresponding code block will be executed. Otherwise, if none of the conditions is met, the else block will execute.

Flow Chart:

if-else-if-flow-chart

Syntax:

For Example:

Output:

In the above code, we implemented if else..if and declared num = 12 and checked for the statement num <= 81 which is of course less than 81 so it will print the first if message.

Try by Yourself!

Important Catch here:

The else statement should begin on the same line as the if statement, following the closing curly brace. Otherwise, the compiler will complain.

Let's look at it from the perspective of a program.

For Example:

Output:

Run the Code

  • The else statement in the above program does not begin on the same line as the end of the if statement in line. Instead, it begins with the next line. This is not permitted in Go.
  • If you run this program, the compiler will display the error message,

The reason for this is the way Go automatically adds semicolons:

  • According to the standards, if the closing brace } is the last token on the line, a semicolon will be placed after it.
  • As a result, a semicolon is automatically put after the closing braces of the if statement.

It should be like this:

Output:

Try by Yourself!

Conclusion

  • This article demonstrated how to create and use conditional statements in Go.
  • As you can see, nesting can take several forms sometimes it will go too much depth also so we should avoid it in a large codebase.
  • In the Go programming language, the if-else statement is a specific control flow statement. Conditional statements instruct the program to determine whether a specific condition is met. If the condition is met, the program will execute a particular code; if it is not met, the program will resume executing other code.