Deep parsing of the For loop in go language _golang

Source: Internet
Author: User

A For loop is a circular control structure that effectively writes a specific number of loops that need to be performed.

Grammar
The syntax for the FOR loop in the Go programming language is:

Copy Code code as follows:

for [Condition | (init; condition; increment) | Range]
{
statement (s);
}


The following is a for loop control in a process:

If the condition is available, it is executed for loops as long as the condition is true.

If the FOR clause is (init; condition; increment)

The initialization (INIT) step is executed first and only once. This step can declare and initialize any loop control variable. There is no need to put a declaration here, as long as a semicolon appears.

Then, the condition (condition) is evaluated. If true, the loop body is executed. If it is false, the loop body does not execute, just after the for loop flow control jumps to the next statement.

After the For loop executes the body, the control flow jumps back to the increment (increment) statement. This statement allows you to update any loop control variable. This statement can be left blank, as long as a semicolon appears after the condition.

Condition now reassess the calculations. If true, the loop executes in the process of repeating (the loop body, then adding the step and then the condition again). Then the loop terminates if the condition is false.

If range is available, then loop through each item within the scope.

Flow chart:

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {

var b int = 15
var a int

Numbers: = [6]int{1, 2, 3, 5}

/* For loop execution * *
For a: = 0; A < 10; a++ {
Fmt. Printf ("Value of a:%d\n", a)
}

For a < b {
a++
Fmt. Printf ("Value of a:%d\n", a)
}

For i,x:= range Numbers {
Fmt. Printf ("value of x =%d at%d\n", X,i)
}
}


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

Value of a:0
value of a:1
value of a:2
value of A:3
value of a:4 value of A:5 C7/>value of A:7
value of a:8 value of A:9 value of a:1 value of a:2 value of
a:3
of A:4
value of A:5
value of A:6
value of A:7
value of a:8 value of A:9
value of a:11
value of a:12
value of a:13
value of a:14
value of a:15
value of x = 1 A T 0
value of x = 2 at 1
value of x = 3 at 2
value of x = 5 at 3
value x = 0
at 4 value of x = 0 at 5

Go language nesting for loop
The Go programming language allows you to use one loop to embed another loop. The following section shows a few examples to illustrate this concept.

Grammar
The syntax for nested for loop statements in the go language is as follows:

Copy Code code as follows:

for [Condition | (init; condition; increment) | Range]
{
for [Condition | (init; condition; increment) | Range]
{
statement (s);
}
statement (s);
}


Example:
The following program uses a nested for loop to find the primes from 2 to 100:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var i, J int

For i=2; I < 100; i++ {
For j=2; J <= (I/J); J + + {
if (i%j==0) {
Break If factor found, not prime
}
}
if (J > (i/j)) {
Fmt. Printf ("%d is prime\n", i);
}
}
}


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

2 is prime
3 was prime
5 is prime
7-prime
-Prime is prime-is prime  />23 is prime ' is prime ' is prime ' is prime the is prime the prime ' is prime
53  Is prime the prime is prime-is prime-is-prime-is
Prime
-is prime Prime
Prime is
prime

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.