Java Repetition Cycles Explained
Java Repetition Cycles Explained
OBJECT-ORIENTED: JAVA
CONTENT STRUCTURE
INTRODUCTION
1Repetition cycles.........................................................................................................................................................................................................3
1.1for
1.2While
1.3Do while...................................................................................................................................................................................................................................14
GLOSSARY
BIBLIOGRAPHIC REFERENCES..........................................................................................................................................................20
CREDITS...........................................................................................................................................................................................................................21
INTRODUCTION
In this training material, the use of will be studied.
syntax of the different loops in the language
Java; it is about the second type of instructions of the highest
complexity, which also contribute to the control of
flow of the programs, in this case, related to the
execution of common repetitive orders in writing
this type of codes.
1CYCLES OF REPETITION
1.1 For
The for repetition cycle allows the execution of a statement or block of statements a determined number of times.
sometimes; it is usually used in those cases where, from the beginning, it is known how many times
The execution of the block of instructions is desired to be repeated.
In the first part of the loop, it initializes the variable that will control the loop and with which it indicates which one goes.
to be the value with which said cycle begins.
In the second part, the condition that must be met for the block of statements to continue is analyzed.
executing; at the moment the condition is no longer met, the block of statements is not executed anymore.
Finally, there is the part of modifying the control variable, which indicates whether the value of the
the initialized variable must increase or decrease and by what frequency, by one at a time, by two at a time, between
others; this element is of great importance, since if the value of the control variable does not change, the analysis
the condition will always return the same result, which would lead to the execution of an infinite loop
that in most cases occupies system resources and blocks it.
The process carried out when using a for loop is illustrated below:
» Examples
aRepetition Cycles. Repetition Cycles. Repetition Cycles. Repetition Cycles. Repetition Cycles.
The previous example can be done without using the loop in the following way:
The returned result would be exactly the same, note the improvement in the code structure, avoiding the
repetitive writing of instructions.
[Link] is required to show the even numbers between 100 and 120.
Since the exercise asks for even numbers starting from 100, that is the initialization value of the variable.
delfor, considering that it only requests up to 120, this value is used as the conclusion of the
And since they are even numbers, the increment must go by 2s, for which i+=2 is used, which is the same.
what to say i = i + 2.
Since the counting is backward, the only difference is that the variable in the third part of the for loop,
instead of being increased, it is decreased using the operator - -.
[Link] is desired to simulate the functioning of a clock through nested for loops.
What happens with the foranidado is that a count is made from 0 to 59 for the variables byc that are
simulating the minutes and seconds, and from 0 to 23 for the variable a that handles the hours; in this way
These hours are displayed, from 0:0:0 to 23:59:59.
According to the location of the for, the hours are observed first, then the minutes, and finally the
seconds.
1.2 While
The while loop allows the execution of a block of instructions as long as the analyzed condition
It is true, if in the initial analysis the condition is false, the block of instructions is never executed.
It is important that within that block of instructions there is one that varies the result.
from the condition for it to be true and the instructions to be executed, or it can be
to become false and for the cycle to end.
It can be seen that the condition acquires a boolean value that, when true, enters the lines
of code contained in the braces and are executed, and if it is false, it goes to the first instruction it finds
outside of the while.
» Examples
a. A program is required that asks the user for an integer and prints all existing numbers.
between 0 and that number.
The variable a is declared, which serves as the control variable in the while and the variable number that
it will be the data entered by the user; the program then prints the numbers from 0, which is the value
of initialization that was given to a, up to the number indicated by the user.
[Link] is necessary to find the area of a sphere by asking the user to enter the value of the radius; in case
that the entered value is less than or equal to zero, a message must be returned and ask again
the data.
The variables radius and area are declared, the first is the value entered by the user and the second for
perform the calculation when the value of the radius meets the indication.
If the value 0 is entered as the radius, the program asks for the data again; otherwise, the program
calculate the area; the execution is shown in the following figure:
c. A program is required that asks the user for the measurement in centimeters of the side of a square.
and calculate its area, provided that the entered value is greater than zero; in case that this
if the condition is not met, no more values will be requested and the number of areas must be displayed
have been calculated.
The variables counter, side, and area are declared; the first will hold the number of times the area
calculated sea, the second stores the value entered by the user and the third to perform the calculation
respective, whenever the side meets the specified criteria.
In the while loop, it is indicated that whenever the value entered by the user is greater than zero, it should be executed
calculation, otherwise, the program executes the instruction that is immediately after finishing
the while prints the number of areas that have been calculated. In the following figure, it can be observed
the result of this example.
d.A program is required that asks the user for an integer and displays the multiplication table.
multiply by that number.
The variables counter, result, and number are declared. The first is the control variable of the while.
which indicates that the data from the table will be displayed from 1 to 10, the second to carry out the
respective calculation, and the third one receives the value entered by the user that determines the table that will be
shown.
In the meantime, it is instructed to perform the calculation up to 10, and subsequently, the table is shown.
In the following figure, the execution of this example can be observed.
1.3 Do while
The while loop is also used to repeatedly execute certain instructions, it differs from the while in
the fact that the while block of instructions will always be executed at least once, given
that first the execution takes place and subsequently the condition is evaluated; if it is met, they continue
executing the instructions until it is not so, and if it is not met, no more are executed.
The condition acquires a boolean value that, when true, continues executing the statements that
It is inside the braces, and if it is false, it goes to the first instruction that is found outside the do while.
The process carried out when using a while loop is illustrated below.
b.A program is required that prompts the user to input several numbers and calculates the average.
of these when the entered number is 0, that number should not be taken into account in the quantity
data to average.
c. A program is needed that requests the grades of students until the entered value is 0, and
After that, show on the screen how many obtained a grade equal to or higher than 6, and how many lower.
The variable grade is declared, which receives the grades entered by the user, and two counters are used.
called cant1 and cant2 to store the total number of students who scored below and
greater than 6.
In the loop, it requests the input of each grade, and through an if statement, it adds to each respective counter.
the note that has been entered, and in the while it indicates that the cycle must end when it is entered
how to note the zero value.
Finally, the results show how many students obtained higher grades and
less than 6.
In the following figure, the code of this example is observed along with its corresponding execution.
d.A program is desired that asks the user for the access password and only gives them a welcome.
to the system when this is correct.
GLOSARIO
Infinite cycle:Repetition cycle whose control variable did not change state and therefore the cycle
never finished.
Control variable:Value that ensures each cycle of repetition can have an ending.
BIBLIOGRAPHIC REFERENCES
CREDITS
Graphic resources
This material can be distributed, copied, and displayed by third parties if shown in
the credits. No commercial benefit can be obtained and the derivative works
They must be under the same terms of the license as the original work.