The IF condition statement in Go language uses the detailed _golang

Source: Internet
Author: User

If statement
The If statement consists of a Boolean expression followed by one or more statements.

Grammar
The syntax for the IF statement in the Go programming language is:

Copy Code code as follows:

if (boolean_expression)
{
/* Statement (s) would execute if the Boolean expression is true * *
}

If the Boolean expression evaluates to True, then the code block inside the IF statement is executed. If the end of the IF statement (after the closing brace) evaluates to False, the first line of code after the statement is executed.

Flow chart:

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var a int = 10

/* Check the Boolean condition using if statement * *
if (a < 20) {
/* If condition is true then print the following * *
Fmt. Printf ("A is less than 20\n")
}
Fmt. Printf ("Value of A is:%d\n", a)
}


Let's compile and run the above program, which will produce the following results:

A is less than;
Value of a is:10


If...else Statement
an If statement can be followed by an optional else statement, which is executed when the Boolean expression is false.

Grammar
The syntax for the IF ... else statement in the Go programming language is:

Copy Code code as follows:

if (boolean_expression)
{
/* Statement (s) would execute if the Boolean expression is true * *
}
Else
{
/* Statement (s) would execute if the Boolean expression is false/*
}

If the Boolean expression evaluates to True, then the if code block will be executed, otherwise else code blocks will be executed.

Flow chart:

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var a int = 100;

* Check the Boolean condition * *
if (a < 20) {
/* If condition is true then print the following * *
Fmt. Printf ("A is less than 20\n");
} else {
/* If condition is false then print the following * *
Fmt. Printf ("A is not less than 20\n");
}
Fmt. Printf ("Value of A is:%d\n", a);

}


When the above code is compiled and executed, it produces the following results:

A is not less than;
Value of a is:100

If...else If...else Statement
It is useful to use a single if...else if statement to declare tests of various conditions by following an optional else if statement.

When using if, else if, the else statement has a few points to remember to use:

If can have 0 or one else, it must follow else if.

An if can have 0 to many else if and they must precede else.

Once an else if test succeeds, any remaining else if tests will not be tested.

Grammar
If...else If...else The syntax of the statement in the Go programming language is:

Copy Code code as follows:

if (Boolean_expression 1)
{
/* Executes when the Boolean expression 1 is true/*
}
else if (Boolean_expression 2)
{
/* Executes when the Boolean expression 2 is true/*
}
else if (boolean_expression 3)
{
/* Executes when the Boolean expression 3 is true/*
}
Else
{
/* Executes when the none of the above condition is true */
}

Example:
Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var a int = 100

* Check the Boolean condition * *
if (a = = 10) {
/* If condition is true then print the following * *
Fmt. Printf ("Value of A is 10\n")
else if (a = = 20) {
/If Else if condition is true/* *
Fmt. Printf ("Value of A is 20\n")
else if (a = = 30) {
/If Else if condition is true/* *
Fmt. Printf ("Value of A is 30\n")
} else {
/* If none of the conditions is true/*
Fmt. Printf ("None of the values is matching\n")
}
Fmt. Printf ("Exact value of A is:%d\n", a)
}


Let's compile and run the above program, which will produce the following results:

None of the values is matching
Exact value of a is:100

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.