Control Statement for: having bettter control over the repetitions


We will now see the most important and used Java control statemente, which is always seen in our course code: the for loop

His fame is due to the power we have because of the details that the loop is able to receive, and you can have more control over the repetitions.



Differences between while and for loop


Generally, what you can code using the for, you can code using the while looping.
Then of course comes the question: why and what it does, then the loop?

It's like asking 'why use a++' if we can use 'a = a +1'.
Simple: for simplicity and practicality.

We, Java programmers, should always seek maximum efficiency. Doing things the simplest way possible.

The usage is due to the following two facts, you may have noticed if you made over the years while loop and if you have seen the examples while the article:

1. There is usually a starting point, an initial counter for while loop
2. Generally this counter, or other, changes. In order to increase or  decrease until the
condition is no longer satisfied, to the while loop stops.



The for loop: how to use


The for loop syntax is as follows:

for(value or initial condition; for looping condition ; factor, change number, increase or decrease){
    //your code
}



The loop works as follows:
The loop begins with the initial condition. Usually starts the counter. This first stage ALWAYS happens.

The second stage is the test of the loop condition. If true, the code is executed.
At the end of code execution, is the factor of change, which is usually an increment or decrement, happens.

After, the condition is retested. If returns 'true', the code is executed.
At the end of code execution, is always the factor of change ... and so on.

Learn how to use the for statement control loop in Java, using examples and code commented.


We will show 3 code examples that show the good example of the use and flexibility for loop statement.
It is important that you learn well because they are widely used in our Java course.

Example 1: Counting to 10 with for loop


public class for1 {
    public static void main(String[] args) {
        for(int count=1 ; count <= 10 ; count++){
            System.out.println(count);
        }
    }

}


In this example, I made a point to show an important feature of the loop: the declaration happens inside the for
Note that the initial condition of the counter, was declared the entire 'count', which was used in the loop.

How it works:
The first thing that happens is the initial condition. In our case, it creates a variable 'count' and initialized with the value 1.
Then, the condition is tested. As it is true, it prints the value of 'count', which is 1.

After 'count' is incremented and becomes 2.
Then it is tested. As continues to be less than 10, it is printed. After incremented, tested, printed ... it is printed until 10, because after that it will be incremented and 'count' is 11, then the condition is no longer true and the loop will end.


Example 2: countdown using the for loop


public class for2{
    public static void main(String[] args) {
        for(int count=10 ; count >= 1; count--){
            System.out.println(count);
        }
    }

}


This example serves to show that we can use the decrement, not only the increment within the loop.

How it works:
The first thing that happens is the initial condition. In our case, it creates a variable 'count' and initialized with the value 10.

Then, the condition is tested. How to 'count' is greater than or equal to 1 is true and prints the value of 'count', which is 10.
After this implementation, 'count' becomes 9, and continues to be greater than or equal to 1, and when printed ... 'count' is 1, it is printed by one is greater than or equal to 1.
However, to be decremented, becomes 0 and the loop will end.


Example 3: countdown and countup in the same loop


Another feature of the for loop, you do not need to use only one control variable ('count') or test only one condition. The Java code for this is:

public class for3 {
    public static void main(String[] args) {
        for(int up=1, down=10 ; up<=10 && down>=1; up++, down--){
            System.out.printf("%d \t %d \n", up, down);
        }
    }

}


How it works:

Declare two variables, 'up', which will be incremented and printed 1 to 10,
and the variable 'down' which will be printed and decremented from 10 to 1.
Inside the printf use the character '\ t', which is the ASCII code for the tab, or our famous 'tab'.

Compile and run this simple code, you will see how wonderful it is to be Java programmer and your life will make more sense.

No comments: