Understanding for Loop

Understanding for Loop


Understanding for Loop

Loops are programming language constructs that are used to execute a segment of code repeatedly a number of times.

Syntax:
                for (Initialize Counter; Boolean Expression; Expression)
      {
            // code to execute repeatedly inside the loop
      }

Explain:
For loop has three arguments:
  1. Counter initialization
  2. Boolean expression
  3. Increment/decrement expression (for counter commonly)

Note: All three arguments are optional (you may leave them blank). You may leave any or all the three arguments blank.

For example:
                for (; ; )
      {

      }

  1. Counter Initialization
Counter is a variable that may use to count or limit the number of reputation of the loop.
In this argument the initial value is assigned to the counter. You may leave the argument blank in case the counter is already initialized. The counter initialization executes only the first time the loop executes.

  1. Boolean Expression
Boolean Expression specifies the condition for executing the loop. The loop continues till the Boolean expression is set/returns the true value. When the Boolean expression returns or results to false the loop terminates.

  1. Increment/decrement expression
Increment/decrement expression used to increment or decrement the value of the counter (but not necessarily). You may leave this argument blank or this expression may used to manipulate the value of the variable other than the counter. This expression executes at the end of the loops body.


Some examples explaining the usage of for loop.

int counter;
for (counter = 0; counter < 10; counter++)
{
      System.out.println(counter);
}

Understand the above program.

In the first argument we initialized the value of counter to 0. In the second argument a boolean expression “counter < 10” is supplied to that specifies the condition for executing the loops body. The code inside the loops body execute till the condition satisfies. And the third argument is counter increment expression. This expression will increment the value of counter to 1 at every laps of the loop execution.

When the first time loop executes the value of counter is initialized 0. Then it will check the boolean expression that is “counter < 10” or “0 < 10” that will return true. That means the loop can be executed. Than the loop starts the execution of the code inside its body ( printing the value of counter). At last the third argument expression (counter increment) executes (increasing the value of counter to 1, that is the value of counter is now 1). Now loop restart the process but now the initialization does not execute. It will check the Boolean expression that is now “counter < 10” = “1 < 10” that will return true. The process continues until the Boolean expression results to false (when the counter’s value reaches greater than or equals to 10). Because “10 < 10” will result to false.

Step 1:                  Execute the initialization code.
Step 2:                  Check the Boolean expression. If it is true then go to Step 3. Otherwise go to Step 6:
Step 3:                  Execute the code inside the loops body.
Step 4:                  Execute the Increment/Decrement expression.
Step 5:                  Go to Step 2.
Step 6:                  Loop Ends.

      

Kategori

Kategori