whileforforeach
I will explain the repetition process in JavaScript .
There are three types of iterative processing: “while”, “for”, and “foreach”.
By using these properly, you can perform efficient programming.
What is repeat processing?
Repeat processing is a method for repeatedly executing the same process multiple times.
Iteration operations include , while
, do-while
, for
and foreach
so on.
By using these repetitive processes, you can perform the same process over and over again, making programming more efficient. For example, if you want to repeatedly display numbers from 1 to 100, for
you can easily implement this using statements.
Repetition processing is also known as repetition structure or loop structure, and is an important concept for programming learners.
Iteration processing using while
while
is a process that repeats as long as certain conditions are met .
while (terms) {
Processing;.
}
In the above example, the variable count starts from 1, and while count is 10 or less, the process of outputting count and incrementing count by 1 is repeated. Outputs 1 to 10.
let count = 1;
while (count <= 10) {
console.log(count);
count++;
}
while
Statements can be used when the conditions are not known in advance during repeated processing, and are especially used when processing is performed until a specific state is reached based on conditions such as input values.
Please note that if no conditions are written, an infinite loop will occur and the program will have to be forcibly terminated.
Iteration processing using do-while
Another syntax similar to while is the do-while syntax. do-while is an iterative process that continues to execute at least once until a specific condition is met.
do {
processing
} while (terms);
In the following example, the variable count is output first, and the process of outputting count is repeated while count is less than 10, increasing count by 1.
let count = 1;
do {
console.log(count);
count++;
} while (count <= 10);
Compared to while, do-while is characterized by the fact that the first execution is always executed and the conditional decision is made at the end of the iteration, thus guaranteeing that the process is executed at least once.
Iteration processing using for
for
is an iterative process until a certain condition is met.
for (initialization; conditions; variable updates) {
Processing;
}
In the example below, the variable i starts from 0, and while i is less than 10, i is output, and i is incremented by 1.
for (let i = 0; i < 10; i++) {
console.log(i);
}
The for statement can be used when the number of repetitions is known in advance during repeated processing, and can be used to perform processing efficiently when the number of repetitions is determined.
Iteration processing using forEach
forEach
is an iterative process that retrieves and processes each element of an array or object one by one.
array.forEach(function(element) {
processing;
});
In the example below, each element of the array array is retrieved and output is repeated.
let array = [1, 2, 3, 4, 5];
array.forEach(function(element) {
console.log(element);
});
forEach
internally omits the use of subscripts and retrieves elements one by one, so the number of repetitions is determined according to the number of elements in the array, so it is relatively easy to retrieve and process the elements of an array one by one. can be implemented in
How to break and continue repeating
“break” to interrupt repetition
break
There is an instruction for forcefully exiting from repeated processing . break
Instructions allow you to break out of an iterative process when certain conditions are met.
break;
Below break
is a sample using.
let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
if (array[i] === 3) {
console.log("3 is found and the process is terminated.");
break;
}
console.log(array[i]);
}
Extract the elements of the array “array” for
using a statement, and if the extracted element is “3”, execute “console.log(“3 was found, processing will end.”);” break
Exit from repetitive processing with a command.
“continue” to skip repetition
continue
There are instructions for skipping the current iteration of an iteration and moving on to the next one .
continue;
Below continue
is a sample using.
let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
if (array[i] % 2 !== 0) {
continue;
}
console.log(array[i]);
}
for
A statement is used to retrieve the elements of the array “array” , and if the retrieved elements are an odd number continue
, the instruction skips the current iteration of the iteration and moves on to the next one.
Summary of repetitive processing (while, for, forEach)
The repetition process is summarized below.
while
A statement performs a process that repeats as long as a specific condition is met.do-while
A statement is an iterative process that continues executing at least once until a specific condition is met.for
A statement iterates until a specific condition is met.forEach
extracts and processes each element of an array or object one by one.break
can stop repeating,continue
and can skip repeating.
Each iteration process has its own characteristics, and when choosing which iteration process to use, it depends on what kind of process you want to perform and under what conditions you want to repeat the process.
I was able to check the characteristics of each repetition process, and was able to easily understand which repetition process to use!
Iteration processing is one of the important concepts for beginners in programming.
Please understand repetition processing while actually writing the code.
Comments