The while loop: what it is, how to use and calculating AP and GP series with while


In recent articles we learned several operators and shortcuts used by Java programmers, such as ++, --, +=, -=, *=, /= and other.

It's time to put them in action and learn one of the most important commands in any programming language: the control statements

In this article, we'll explain what it is and how to use the while loop.


How to use while loop

While...remember the English 'while', the word. While, in really, is a loop.
Each cycle of repetition is called iteration.
Its syntax is as follows:

while (condition) {
    //codes
}


That is, while the condition is true, the codes in brackets are repeated.
But what is the advantage of having a repeated code? That does not seem absurd?

Well, when we think of repeated, think of something static, but that is not what happens.
That's where the operators increment, decrement, assignment and others begin to act in our Java codes.

On the lines of code, usually things go 'changing' the condition and each iteration the condition is tested.
If the condition returns a logical value true, the code between the curly braces is executed.
If the returned value is false, the while loop is then completed - finishing the looping- and the Java program usually follows.

Let's see a simple example that shows the numbers from 1 to 10.

Java code example: counting to 10 in Java, with the while loop

The program is simple.
We define an integer variable, 'count'.
While this variable is less than 10, its value will be printed and will be incremented.
Ie will be printed 1, 2, 3, ..., 9, 10 ... and hey! wait!
When you reach 11, the condition inside the loop is no longer true, and the loop will end!

Test:



public
 class counting {

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

}


Making an AP (arithmetic progression) with the while loop in Java

The following program shows the terms 'an' of an A.P, the initial term 'initial' and ratio 'ratio', to a maximum 'maxvalue'.
The difference this program for the former is that the terms of a PA grow through the addition of reason to the previous term.
In programming, we could represent it well: an = an + reason
However, as we have seen some operators in Java, do this: an += ratio;
See the result:



public class apSeries {
    public static void main(String[] args) {
        int initial=1,
            ratio=3,
            an=initial,
            max_value=20;
        
        System.out.printf("A.P elements, of initial value %d and ratio %d, less than %d\n", initial, ratio, max_value );
        while(an<=max_value){
            System.out.println(an);
            an += ratio;
        }
    }

}




Making a G.P (geometric progression) with the while loop in Java


The following program shows the terms 'gn' a GP, the initial term 'initial' and quotient 'quotient', up to a maximum 'maxvalue'.
The products of a GP geometric progression grow through the product term to the previous quotient.
In programming, we could represent it well: gn = gn * reason
However, as we have seen some operators in Java, do this: gn *= reason
See the result:



public class gpSerie {
    public static void main(String[] args) {
        int initial=1,
            quotient=2,
            gn=initial,
            maxValue=32;
        
        System.out.printf("G.P progression elements, of initial value %d and quotient %d, less than %d:\n", initial, quotient, maxValue );
        while(gn<=maxValue){
            System.out.println(gn);
            gn *= quotient;
        }
    }

}

No comments: