[Java] For Loop with Examples
For statement
for(initialization; conditional expression; increment/decrement expression) {
Statement to be executed;
}
This is a loop statement that is executed as many times as the condition is met. It can be used as an infinite loop, such as for( ; ; ).
You must know how the operators+= ++ are applied between conditional statements and be able to use them freely.
+= and ++ continue to affect the next operation. This is because it includes the concept of substitution. When the initial value is 0, i applied to the output value is 0 and thereafter follows the increment/decrement formula.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
for(int i@@T104@ @=0; i@ @T112@@10; i+@ @T119@@=1) { ·
System.out.println((i@@T135 @@+1)+". Hello JAVA~");
}
for(int i=0; i@@T162 @@<10; i++)
System.out.println(+@@T186 @@+i+". Hello Eclipse~");
}
for(int i=0; i<10; i++) {
System.out.println(i++ +". 안녕이클립스~");
}
for(int i=0; i<10; i+=2) {
System.out.println((i+1) +". 안녕Eclipes~");
}
for(int i=0, j=0; i<10; i++,j+=2) {
System.out.println((j+1) +". 안녕오라클~");
}
for(int i=10; i>0; i--) {
if(i%2 != 0)
System.out.println (i+". HelloSpring~");
}
|
cs |
/*
1) Line values are output from 1 to 10.
5) Line values are odd and 1,3,5,7,9 are output.
9) Line values are even numbers and 0,2,4,6,8 are output.
Since the postfix operator is in the output value, the input initial value of 0 is output first, and then 1 is added and placed in the conditional expression.
And by the increment formula, it becomes 2 and outputs from 2 again
13) Line values are odd and 1,3,5,7,9 are output.
At first, the value of i is 0 and 1 is added to the output value, so it is 1. Afterwards, the increment increases by 2, so the output value becomes 2+1 and is output as 3.
17) The line value is repeated 10 times according to i, and the j value starts from 1 and outputs 10 odd values.
21) The line value is output if the remainder after dividing by 2 is not 0, so it is output backwards from 9 as an odd value.
*/
break; and continue;
break; used with an if statement in a for statement; means that repetition stops when the if statement condition is met.
continue; used with an if statement in a for statement. If the condition of the if statement is met, the following statements are not executed and the loop returns to the loop to perform the increment/decrement expression.
|
1
2
3
4
5
6
7
8
9
10
|
for(;;) {
System.out.println(++cnt+"번째반복");
if(cnt==5) break;
}
for(int i=0; i<10; i++) {
if((i+1)%2 == 0)
continue;
System.out.print(i@@T652@ @+1+" "); //1 3 5 7 9
}
|
cs |
Example 1 - Applying membership registration conditions
Consider all cases
First, the ID, password, and name must all be entered, and the password must be a string of 8 to 15 characters mixed with English letters, numbers, and special characters. Therefore, if you enter a value that does not meet each condition, you must write code that outputs a warning message.
1. Method to check password
If it is true, the value is input, and if it is false, a warning message is used instead, so the method is written as a boolean type. First, check whether the number of password digits is satisfied, and if satisfied, check if Character.isAlphabetic, Character.isDigit, and other else values are all true.
At this time, to check the index value of each digit char ch = pwd.charAt(i); Use
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
Boolean checkPwd(String pwd) {
| @= false, sepcialFlag = false;
| //Length of the entered password string
| 80@@= length @@T88 9@@&& length @@T8 98@@<=15) {
for (int i=0; i<length; i++) {
char ch = pwd.charAt(i);
if (Character.isAlphabetic(ch)) { //int타입에 char타입 쓰면 자동형변환 발생
alphabetFlag = true;
} else if (Character.isDigit(ch)) {
numFlag = true;
} else
sepcialFlag = true;
}//end of for--------------------------------------------
| @& numFlag && sepcialFlag) {
return true;
|
}
else { //If the character length is not more than 8 characters but less than 15 characters
return false;
}
|
cs |
2. Considering all other cases
Since the value must be entered and have at least one character, null values and .length() values of 0 are excluded. At this time, the null point value should not be excluded to prevent NullPointerException from occurring. Of course, when entering Eclipse, the value is entered as " " by default.
|
1
2
3
4
5
|
if (mbr.id !@@T 1103@@= null &@ @T1113@@& mbr.id.length()>@@T1121 @@0 &&
mbr.pwd != @ @T1141@@null &&
mbr.name ! = null @@T1165 @@&& mbr.name.length@@ T1172@@()>0) {
System.out.@@ T1185@@println("== Sign up successful!! ==");
} else System.out@ @T1196@@.println("== Membership registration failed!! ==");
|
cs |
Example 2 - Determining the number of upper and lower case letters, numbers, and special characters in a string
index To compare values, substitute the letters of the word into ch in order to check which letter corresponds to the conditional expression. Set the variable corresponding to the number of characters to upperCnt++; Counting is done using postfix increment and decrement operators, such as
|
1
2
3
4
5
6
7
8
9
10
11
|
for (int i@@T 1280@@=0; i@@T 1287@@<word.length(); i ++) {
|
|
upperCnt++;
lowerCnt++;
{ @T1368@@++; //Check whether special character
}
|
cs |
Example 3 - Printing cumulative calculation formula
If you want to output something like
1+2+3+4+5+6+7+8+9+10=55, the values up to
9 should be output with a + sign after them, and the else value of 10 should be output as 10.
|
1
2
3
4
5
6
7
8
9
|
sum = 0;
String str = "";
for (int i@ @T1471@@=1; i@@T1478 @@=10; i@@T 1486@@++) {
if(i<10) str@@T 1505@@+=i+"+"; //Combining strings
else str+=i;
sum+=i;
}
System.out.println(str@@T 1556@@+"="+sum);
|
cs |
Example 4 - Printing cumulative calculation formula with input integers
When receiving 1 as the starting integer and 10 as the ending integer, if you want to output as 1+2+3+4+5+6+7+8+9+10=55
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package my.day06.a.FOR;
import java.util.Scanner;
public class Sum2Main2 {
· public static void main(String[] args) {
Scanner sc = n ew Scanner(System.in);
| //A variable that stores the accumulated sum, initialized to 0
String str =""; //A variable that stores the numbers to be accumulated and + by combining them into a string and is initialized with "".
System.out.@@T 1789@@print("Starting number to accumulate => ");
int startNo @@T1799 @@= Integer.parseInt(sc.nextLine());
System.out.@@T 1809@@print("Last number to accumulate => ");
int endNo @@T1819 @@=Integer.parseInt(sc.nextLine());
for (;;) {
try {
| @-startNo+1;
| 3@@=0, j=startNo; i@@T1 872@@<cnt; @T1882@@,j++) {
if(j <cnt) str @@T1 901@@+= j@@T1907 @@+"+";
else str@@T191 8@@+=j;
sum+=j;
{
break;
…
{catch (NumberFormatException e) {
System.out. println("\n>> Please enter only integers!! <<");
|
sc.close();
|
}
//1+2+3+4+5+6+7+8+9+10=55
|
System.out.print(str@@ T1990@@+"="+sum);
sc.close();
System.out.println("\n>> Program End <<");
{ }//end of main
}
|
cs |
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going