0% found this document useful (0 votes)
5 views22 pages

Java Repetition Cycles Explained

This document describes the different types of repetition cycles in Java, including for, while, and do while. It explains the syntax and usage of each one, with examples of how to print text multiple times and show even numbers within ranges using a for loop.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views22 pages

Java Repetition Cycles Explained

This document describes the different types of repetition cycles in Java, including for, while, and do while. It explains the syntax and usage of each one, with examples of how to print text multiple times and show even numbers within ranges using a for loop.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING

OBJECT-ORIENTED: JAVA

USE OF REPEAT CYCLES IN JAVA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

CONTENT STRUCTURE

INTRODUCTION
1Repetition cycles.........................................................................................................................................................................................................3
1.1for
1.2While
1.3Do while...................................................................................................................................................................................................................................14
GLOSSARY
BIBLIOGRAPHIC REFERENCES..........................................................................................................................................................20
CREDITS...........................................................................................................................................................................................................................21

2 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

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.

It is necessary to take into account that in the solutions


Computers are frequently presented, true.
type of operations that must be resolved in multiples
iterations, depending on the values stored by
certain variables and a comparison, normally logical,
that is carried out around these variables; the execution of
these iterations can be executed using different
lines of code; however, this would make the coding non-functional and poorly structured, therefore, it is
necesario que el programador cuente con la habilidad requerida para manejar los ciclos de repetición.

1CYCLES OF REPETITION

The control structures known as loops of


repetition or also known as loops, consist of
in instructions whose main objective is to avoid the
repetitive writing of lines of code and control by
through analyzed conditions, the number of times
that must be executed a block of statements.

There are three types of loops which are for,


the while and do while, which provide different
functionality to the programmer; below, there
observe each of them with their respective syntax and
exemplification.

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.

3 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

The syntax of the cyclophore is as follows:

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:

4 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

» Examples
aRepetition Cycles. Repetition Cycles. Repetition Cycles. Repetition Cycles. Repetition Cycles.

Code and execution example a - for

The previous example can be done without using the loop in the following way:

Example code without using for

The returned result would be exactly the same, note the improvement in the code structure, avoiding the
repetitive writing of instructions.

5 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPEAT CYCLES


INJAVA

[Link] is required to show the even numbers between 100 and 120.

Code and execution example b - for

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.

6 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

c. A countdown from ten to zero is desired to be displayed on the screen.

Example code c – for

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 - -.

7 National Learning Service


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

[Link] is desired to simulate the functioning of a clock through nested for loops.

Example code d - for

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.

8 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

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.

The syntax of the while loop is as follows:

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.

The process used when a while loop is employed is illustrated below.

» Examples
a. A program is required that asks the user for an integer and prints all existing numbers.
between 0 and that number.

9 National Learning ServiceSENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPEATED CYCLES


INJAVA

Example code a - while

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.

10 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

Example code b – while

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:

Execution example code b - while

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.

11 National Learning Service


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

Example code c - while

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.

Example code execution in C - while

d.A program is required that asks the user for an integer and displays the multiplication table.
multiply by that number.

12 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

Example code d - while

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.

Execution example code d - while

13 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPEATED CYCLES


INJAVA

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 syntax of the while loop is as follows:

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.

14 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPEATED CYCLES


INJAVA

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.

Example code b - do while

16 National Learning Service SENA


VARIABLES Y ESTRUCTURAS DE CONTROL EN LA PROGRAMACIÓN
OBJECT-ORIENTED:JAVA

USE OF REPEAT CYCLES


INJAVA

In the following figure, the execution of the previous example is observed.

Execution example code b - while

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.

Example code C - do while

17 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

Execution example c - do while

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.

Example solution d - do while

18 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

GLOSARIO

Loop:Name by which repetition cycles are also known.

Infinite cycle:Repetition cycle whose control variable did not change state and therefore the cycle
never finished.

Booleanvalue:Type of value that some variables take, defined as false or true.

Control variable:Value that ensures each cycle of repetition can have an ending.

19 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

BIBLIOGRAPHIC REFERENCES

NetBeans. (2019). NetBeans IDE 8.2 Download. Recuperado de [Link]

20 National Learning Service (SENA)


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPEATED CYCLES


INJAVA

CREDITS

Instructional Content Team

» Gloria Matilde Lee Mejía Team leader Centro de Comercio y Servicios –


Regional Tolima

Industrial Design Center and the


» Rafael Nelftalí Lizcano Reyes Pedagogical advisor
Manufacturing - Santander Region

Development Manager of Coffee Training Center


» Lucero Montes Arenas
Programs (Caldas)

Agricultural Sector Attention Center


» Julio Alexander Rodriguez Del Castillo E-instructional pedagogue
Regional Risaralda

Agricultural Sector Attention Center


» Rachman Bustillo Martínez Content evaluator
Regional Risaralda

Design and Metrology Center


» Natalia Andrea Bueno Pizarro Instructional designer
Capital District

Design and Development Team

Industrial Design Center and the


» Francisco José Lizcano Reyes Team Leader
Manufacturing - Regional Santander

Industrial Design Center and The


» Daniel Ricardo Mutis Gómez Web layout
Manufacturing - Regional Santander

Industrial Design Center and The


» Yazmin Rocio Figueroa Pacheco Construction of digital documents
Manufacturing - Regional Santander

Industrial Design Center and The


» Edgar Mauricio Cortes García Front-end development
Manufacturing - Santander Region

Industrial Design Center and the


» Luis Gabriel Urueta Álvarez Development of didactic activities
Manufacturing - Santander Region

Industrial Design Center and the


» Leyson Fabian Castaño Pérez Integration of resources and tests
Manufacturing - Regional Santander

21 National Learning Service SENA


VARIABLES AND CONTROL STRUCTURES IN PROGRAMMING
OBJECT-ORIENTED:JAVA

USE OF REPETITION CYCLES


INJAVA

Repository Management Team

Repository Administrator of Trade and services center -


» Kely Alejandra Quiros Duarte
content and repository managers. Regional Tolima

Graphic resources

Photographs and vectors taken [Link]

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.

22 National Learning Service SENA

You might also like