The computer programming usually requires that the professional is able to accurately manipulate various structures, commands and features of programming languages. Among the various commands available one need to know those who manipulate and alter the execution flow of programs. Moreover, the looping structures are the main resource for the programmer to reuse several lines of source code.

This article aims to present a review of the main Java looping structures for those just starting to program in this programming language and is still becoming familiar with how to structure a program logically. The article discusses the concept of structures, their uses, implications, risks and also specific details of the syntax. This article contains three exercises to help put in practice the Java loop concepts.

Focusing exercises

To assist the reader who is beginning to program in Java, this article presents three exercises that reinforce the learning of loop structures. It is recommended that during the resolution of these exercises the reader first mentally organize the algorithm that should be implemented before actually creating the Java program. Obviously, there are several ways to solve the same problem, and the aim of the exercise here is to put into practice the ability to manipulate loop structures of as well as other skills needed for a Java programmer.

1) Build a Java program to check if a security number of a identification code named CPF is valid or not. The rule is: the number of a CPF has exactly nine digits and two more digits checkers that are listed last. Therefore, a CPF has 11 digits. The CPF number is written in the form abcdefghi-jk or directly as abcdefghijk. It is also common to place dots and dashes in the CPF as follows: abc.def.ghi-jk, where the number represented by the letter j is called the first check digit and the number represented by the letter k is called the second check digit.

How to calculate the check digit j: To calculate the value of j we multiply the digits a, b, c, d, e, f, g, h and i by the corresponding constants according to Table 1.

Table 1. Multiplier values of each digit according to the verifier j.

The sum S1 = 10a + 9b + 8c + 7d + 6e + 5f + 4g + 3h + 2i is divided by 11. Then you get the remainder of dividing 11 by S1 and check as the following:

• The check digit j shall be zero if REST is equal to 0 or 1;

• The check digit j is equal to the result number 11 - REST, if the REST value is equal to 2, 3, 4, 5, 6, 7, 8, 9 or 10.

How to calculate the check digit k: To calculate the value of k we multiply the digits a, b, c, d, e, f, g, h, i and j by the corresponding constants according to Table 2.

Table 2. Multiplier values of each digit according to the verifier k.

Note that to calculate the check digit k is first necessary to calculate the check digit j. The sum, S2 = 11a + 10b + 9c + 8d + 7e + 6f + 5g + 4h + 3i + 2j is divided by 11. Then you get the remainder of dividing S2 by 11 and check as the following:

• The check digit k shall be zero if REST is equal to 0 or 1;

• The check digit k is equal to the result number 11-REST, if the REST value is equal to 2, 3, 4, 5, 6, 7, 8, 9 or 10.

Example: Let's see if the CPF 491122534-30 is valid. For the calculation of j we have:

S1 = (4*10) + (9*9) + (1*8) + (1*7) + (2*6) + (2*5) + (5*4) + (3*3) + (4*2)

S1 = 40 + 81 + 8 + 7 + 12 + 10 + 20 + 9 + 8 = 195

Dividing 195 by 11 we get the REST = 8 and for this REST the check digit j is the result of [11 - REST], so j = 3. For the calculation of k we have:

S2 = (4*11) + (9*10) + (1*9) + (1*8) + (2*7) + (2*6) + (5*5) + (3*4) + (4*3) + (3*2)

S2 = 44 + 90 + 9 + 8 + 14 + 12 + 25 + 12 + 12 + 6 = 232

Dividing 232 by 11 we get the REST = 1 and for this REST k is the digit 0, so k = 0. As the CPF number was provided: 491122534-30, and the calculations, j = 3 and k = 0 we have that this CPF is valid. Tip: Use the modulus operator (%) for calculating the remainder of the division and test their algorithm with the values of several existing CPFs such as 096.177.547-54 or 772 843 809 34.

2) A triangular number, proposed by Gauss in 1788 when he was only 10 years old, is a natural number can be represented in the form of equilateral triangle. To find the nth triangular number from a previous triangular number you simply add n units according to Figure 6.

Figure 6. Examples of triangular numbers and the formula for calculating the elements of the nth triangular number.

Based on this information create a Java program that takes as parameters the value of n on the console screen and shows a graphical representation of the triangular number. Make a loop that puts the character . (dot) to represent the triangle like the red circles in Figure 6.

3) Make a Java program that captures n values from the keyboard (can be numeric valued or not). Also, get a number that will be named k. Your program should print on the screen all the possible combinations of n values organized into groups with k elements, i.e.: which means the combination of all n elements into groups of k elements. For example, if you enter the values: a, b and c (n = 3) and if you enter the value for k = 3, the program should print all combinations of values a, b and c with three elements in each group without repetition of groups, as follows:

(a,a,a) (b,a,a) (c,a,a) (a,b,a) (b,b,a) (c,b,a) (a,c,a) (b,c,a) (c,c,a) (a,a,b) (b,a,b) (c,a,b) (a,b,b) (b,b,b) (c,b,b) (a,c,b) (b,c,b) (c,c,b) (a,a,c) (b,a,c) (c,a,c) (a,b,c) (b,b,c) (c,b,c) (a,c,c) (b,c,c) (c,c,c)

Conclusion

(If you want to learn more about Java, go to our Java online courses)

This article presented a short review of the concepts related to the structures of repetition of an algorithm and a program. The focus of this article was to present the concepts behind the repetitions in the flow of execution algorithms, followed by an explanation and details of some commands in Java that implement looping. The article also addressed the consequences and implications of the use of repetition structures, and ended with the presentation of three exercised for setting the content addressed in order to help readers who are beginning to program in Java. This article showed three exercises that were provided to help out beginners in Java.