Contents

About loops, arrays, and strings (with.Java)

   Mar 11, 2024     3 min read

This is an article about loops, arrays, and strings.

In this article, we will learn about loops, arrays, and strings in the Java programming language in preparation for an information processing article.

These three elements play a very important role in programming.

Let’s take a look at it together.

loop

Loop statements are used to perform the same task multiple times in a program.

Generally, there are for statements, while statements, and do-while statements, and each has different characteristics and usage methods.

Using loops can help you avoid code duplication and perform tasks more efficiently.

For example, it is useful for printing all elements of an array or finding elements that satisfy a certain condition.

example

for (int i = 0; i < 5; i++) {
     System.out.println("Output using loop: " + i);
}

This example is a for statement that repeatedly prints numbers from 0 to 4.

Set the initial value, repeat as long as the condition is met, and execute the increment/decrement expression at the end of each iteration.

The example above prints numbers from 0 to 4.

Arrangement

An array is a data structure that stores multiple values in one variable.

Data of the same type is stored sequentially, and each element can be accessed through an index.

Arrays can efficiently manage sets of data, and can be used with loops to perform various tasks.

Arrays make it easy to structure and manage data.

For example, it is useful for storing student grades or managing multiple coordinates.

example

int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Length of array: " + numbers.length);

for (int i = 0; i < numbers.length; i++) {
     System.out.println("Output array elements: " + numbers[i]);
}

This example creates an integer array, prints the length of the array, and then repeats the elements of the array.

Arrays are declared using the [] symbol, and each element can be accessed through an index.

The above example prints the length of the array and iterates over all elements of the array.

string

A string is a set of characters and is often used when dealing with text data in programming.

Strings are surrounded by double quotation marks (β€œβ€) or single quotation marks (β€˜β€™), and operations such as storing the string in a variable or printing it out can be performed.

Strings can be accessed through indices in a similar way to arrays, and various string processing functions are provided to facilitate string manipulation.

For example, it is useful for tasks such as receiving input from the user, searching or converting strings, etc.

example

String text = "Hello, World!";
System.out.println("String length: " + text.length());
System.out.println("Convert to uppercase: " + text.toUpperCase());
System.out.println("Convert Hello to Hi: " + text.replace("Hello", "Hi"));

This example creates a string, prints the length of the string, converts it to uppercase, and then converts one string to another.

In Java, you can manipulate strings using the String class, which provides a variety of string handling methods.

The above example prints the string length, the result of converting it to uppercase, and the result of converting β€œHello” to β€œHi”.

Conclusion

Loops, arrays, and strings are very important elements in programming languages.

By utilizing these three elements well, you can develop an efficient and powerful program.

Since these elements are supported in various programming languages, it is a good idea to learn and practice the grammar and functions of the language.

So far, we have learned about loops, arrays, and strings in programming languages.

See you next time with another topic! thank you