1.

Explain The Syntax For 'for' Loop?

Answer»

The syntax of a for loop in Go programming language is −

for [CONDITION | ( init; condition; increment ) | RANGE]
{
statement(s);
}

Here is the flow of control in a for loop −

  1. if condition is available, then for loop executes as long as condition is true.
  2. if for clause that is ( init; condition; increment ) is present then

    The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to PUT a statement here, as long as a semicolon appears.

    Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control JUMPS to the next statement just after the for loop.

    After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be LEFT blank, as long as a semicolon appears after the condition.

    The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

  3. if range is available, then for loop executes for each item in the range.

The syntax of a for loop in Go programming language is −

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

Here is the flow of control in a for loop −



Discussion

No Comment Found