Control Statements in Golang (if, if-else, Nested-if)

What conditional statements does Golang have? How to use conditional statements in Go programming? Let's find out together!

Decision making in programming is similar to decision making in real life. In this process, a piece of code is executed when a given condition is met. They are also sometimes called Control flow statements. Golang uses control statements to control the flow of execution of a program based on certain conditions. They are used to make the flow of execution advance and branch based on changes in the state of the program.

Basic conditional statements in Golang programming

If statement

This is the simplest decision statement. It is used to decide whether a statement or block of statements will be executed or not, i.e. if a condition is true then a block of statements will be executed, otherwise not.

Syntax:

if condition {

   // Lệnh được thực thi nếu
   // điều kiện đúng
}

Flow Chart:

Control Statements in Golang (if, if-else, Nested-if)

For example:

// Go program to illustrate the 
// use of if statement
package main

import "fmt"

func main() {
	
// lấy một biến cục bộ
var v int = 700

// dùng lệnh if để
// kiểm tra điều kiện
if v < 1000="" {="" in="" điều="" kiện="" if="" sau="" điều="" kiện="" đánh="" giá="" đúng="" fmt.printf("v="" is="" less="" than="" 1000\n")="" }="" fmt.printf("value="" of="" v="" is="" :="" %d\n",="" v)="" }="">

Result:

v is less than 1000
value of v is : 700

if…else statement

The else statement alone  tells us that if the condition is true then it will execute a block of code and if the condition is false then it will not execute. But what if we want to do something else when the condition is false. This is where the else statement comes in. We can use the else statement with the if statement to execute a block of code when the condition is false.

Syntax:

if condition {

    // Triển khai khối này nếu
    // điều kiện đúng
} else {

    // Triển khai khối này nếu
    // điều kiện sai
}

Control Statements in Golang (if, if-else, Nested-if)

For example:

// Go program to illustrate the 
// use of if...else statement
package main

import "fmt"

func main() {
	
// taking a local variable
var v int = 1200

// dùng lệnh if
// kiểm tra điều kiện
if v < 1000="" {="" in="" if="" sau="" điều="" kiện="" đánh="" giá="" là="" dúng="" fmt.printf("v="" is="" less="" than="" 1000\n")="" }="" else="" {="" in="" ra="" nội="" dung="" sau="" nếu="" điều="" kiện="" được="" đánh="" giá="" là="" đúng="" fmt.printf("v="" is="" greater="" than="" 1000\n")="" }="" }="">

Result:

v is greater than 1000

Nested if statements

In Go Language, nested if is a conditional statement that is the target of another if or else. This Golang statement means there are two if statements nested inside each other as the name suggests.

Syntax:

if condition1 {

   // Triển khai khi condition1 đúng
   
   if condition2 {

      // Triển khai khi condition2 đúng
   }
}

Control Statements in Golang (if, if-else, Nested-if)

For example:

// Minh họa chương trình Go dùng lệnh if lồng nhau
package main
import "fmt"

func main() {
	
// lấy 2 biến cục bộ
var v1 int = 400
var v2 int = 700

// dùng lệnh if
if( v1 == 400 ) {
		
	// điều kiện đúng thì kiểm tra nội dung sau
	if( v2 == 700 ) {
		
		// điều kiện đúng thì hiển thị nội dung sau
		fmt.Printf("Value of v1 is 400 and v2 is 700\n" );
	}
}

}

Result:

Value of v1 is 400 and v2 is 700

Leave a Comment

Can Large Format Printing Ever Be Sustainable? The Answer Is Changing Fast

Can Large Format Printing Ever Be Sustainable? The Answer Is Changing Fast

Large format printing has long faced criticism for its environmental impact. Traditional substrates, solvent-based inks, and short-lived promotional campaigns have all contributed to the perception that eye-catching graphics come at a high environmental cost.

What Young Riders Should Know About Moving Their Motorcycles Across Cities

What Young Riders Should Know About Moving Their Motorcycles Across Cities

Long-distance travel can involve heavy traffic, changing weather conditions, and rider fatigue. If you are also dealing with the responsibilities of moving home, such as packing belongings or coordinating accommodation, a long ride may add unnecessary pressure to an already busy schedule.

Solving Microsoft Teams Shortcut Error Not Opening

Solving Microsoft Teams Shortcut Error Not Opening

Tired of Microsoft Teams shortcut error preventing you from opening the app? Follow our expert, step-by-step guide with the latest fixes for instant resolution. Works on Windows, Mac &amp; web – no tech skills needed!

Solving Microsoft Teams Task Management Sync Error

Solving Microsoft Teams Task Management Sync Error

Tired of Microsoft Teams Task Management Sync Error halting your workflow? Follow our proven, step-by-step fixes to resolve sync issues fast and restore seamless task collaboration. No tech expertise needed!

Troubleshooting Microsoft Teams Wiki Error Formatting

Troubleshooting Microsoft Teams Wiki Error Formatting

Struggling with Microsoft Teams Wiki Error Formatting? This step-by-step guide reveals proven fixes for common wiki tab issues, ensuring smooth editing and collaboration in Teams. Get back to productive wikis fast!

How to Fix Microsoft Teams Installation Error for Linux

How to Fix Microsoft Teams Installation Error for Linux

Struggling with Microsoft Teams installation error on Linux? Discover step-by-step fixes for Ubuntu, Fedora & more. Resolve dependency issues, crashes, and errors quickly with our ultimate guide. Get Teams running smoothly today!

Solving Microsoft Teams Error Page Not Loading

Solving Microsoft Teams Error Page Not Loading

Struggling with Microsoft Teams "Error Page" not loading? Get step-by-step fixes for desktop, web, and mobile. Solve Microsoft Teams Error Page issues quickly and resume seamless teamwork today.

Solving Microsoft Teams Error Screenshot Issues

Solving Microsoft Teams Error Screenshot Issues

Tired of Microsoft Teams "Error Screenshot" blocking your workflow? Get proven, step-by-step solutions to resolve screenshot errors in Teams instantly and boost productivity. No tech skills needed!

How to Fix Microsoft Teams Error U User

How to Fix Microsoft Teams Error U User

Tired of Microsoft Teams "Error U" User blocking your chats? Get proven, step-by-step fixes to clear cache, reset, and restore seamless collaboration instantly.

Where are Microsoft Teams Registry Keys Located on Windows 11?

Where are Microsoft Teams Registry Keys Located on Windows 11?

Unlock the precise locations of Microsoft Teams registry keys on Windows 11. Step-by-step guide to find, access, and safely tweak them for optimal performance and troubleshooting. Essential for IT pros and Teams enthusiasts.