for문
1. 구조
for(초기식; 조건식; 증감식){
반복할 문장;
}
*조건식이 거짓이면 반복문 탈출
*초기식은 처음 한 번만 수행 후, 조건식 -> 반복할 문장 -> 증감식이 반복적으로 수행
2. 구조 2
for( 변수 : 컬렉션 형태의 데이터 ) {
반복할 문장;
}
*컬렉션에서 변수에 데이터를 하나씩 순서대로 대입하면서 반복할 문장 실행
3. 예시1(알파벳 대문자의 아스키 코드 출력)
public class ForLoopTest {
public static void main(String[] args) {
//알파벳 대문자와 아스키 코드 값을 출력하기
for(char c='A';c<='Z';c++){
System.out.println(c+":"+(int)c);
}
}
}
A:65
B:66
C:67
D:68
E:69
F:70
G:71
H:72
I:73
J:74
K:75
L:76
M:77
N:78
O:79
P:80
Q:81
R:82
S:83
T:84
U:85
V:86
W:87
X:88
Y:89
Z:90
종료 코드 0(으)로 완료된 프로세스
4. 예시2(배열 안에 숫자 순서대로 출력하기)
public class ForLoopTest {
public static void main(String[] args) {
//int[] numbers={1,2,3,4,5,6,7,8,9,10}; 배열 원소를 순서대로 출력하기
int[] numbers={1,2,3,4,5,6,7,8,9,10};
for(int i=0;i<numbers.length;i++){
System.out.print(numbers[i]+"\t");
}
System.out.println("");
for(int i:numbers){
System.out.print(i+"\t");
}
}
}
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
종료 코드 0(으)로 완료된 프로세스
5. 예시3(구구단 출력하기)
public class ForLoopTest {
public static void main(String[] args) {
//구구단 출력하기
int []a={1,2,3,4,5,6,7,8,9};
int []b={1,2,3,4,5,6,7,8,9};
for(int i:a){
for(int j:b){
System.out.print(i+"x"+j+"="+i*j+" ");
}
System.out.println("");
}
}
}
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9
2x1=2 2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18
3x1=3 3x2=6 3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27
4x1=4 4x2=8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 6x7=42 6x8=48 6x9=54
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 8x9=72
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
종료 코드 0(으)로 완료된 프로세스
while문
1. 구조
초기식;
while(조건식){
반복할 문장;
증감식;
}
*초기식 확인 후 조건식 확인해서 참이면 반복할 문장 실행하고 증감식 실행
*조건식이 거짓이면 while문 탈출
2. 예시(1~10까지 있는 배열 출력하기)
public class WhileTest {
public static void main(String[] args) {
int[]numbers={1,2,3,4,5,6,7,8,9,10};
int a=0;
while(a<numbers.length){
System.out.print(numbers[a]+"\t");
a++;
}
}
}
1 2 3 4 5 6 7 8 9 10
종료 코드 0(으)로 완료된 프로세스
3. do~while 문
1. 구조
초기식;
do{
반복할 문장;
증감식;
}while(조건식);
*초기식 확인 후 반복할 문장 실행후 조건식을 확인. -> 반복할 문장을 최소한 한 번은 실행
*while조건식이 { }밖에 있어서 마지막에 ; 찍어줘야 함
*조건식이 거짓이면 반복문 탈출
2. 예시 (1~10까지 있는 배열 출력하기)
public class WhileTest {
public static void main(String[] args) {
int[]numbers={1,2,3,4,5,6,7,8,9,10};
int a=0;
do{
System.out.print(numbers[a]+"\t");
a++;
}while(a<numbers.length);
}
}
1 2 3 4 5 6 7 8 9 10
종료 코드 0(으)로 완료된 프로세스
Break, Continue
1. break 예시(문자열 출력 중 문자 'o'를 만나면 멈추기)
public class BreakContinueTest {
public static void main(String[] args) {
//문자 'o'를 만나면 중단하기
char[] c={'s','h','u','t','d','o','w','n'};
for(char a:c){
if (a=='o')break;
System.out.print(a + "\t");
}
}
}
s h u t d
종료 코드 0(으)로 완료된 프로세스
2. continue 예시(1부터 임의의 수까지 3의 배수의 개수 구하기)
import java.util.Scanner;
public class BreakContinueTest {
public static void main(String[] args) {
//1부터 임의의 수까지 중 3의 배수의 개수를 구하기
Scanner scan=new Scanner(System.in);
System.out.println("임의의 정수를 입력하세요: ");
int num=scan.nextInt();
int count=0;
int i;
for(i=1;i<=num;i++) {
if(i%3==0){
count++;
}
continue;
}
System.out.println("1부터"+(i-1)+"까지의 3의 배수의 갯수는: "+count);
}
}
임의의 정수를 입력하세요:
3
1부터3까지의 3의 배수의 갯수는: 1
종료 코드 0(으)로 완료된 프로세스
'Study > Java' 카테고리의 다른 글
| [개념]Static, NoneStatic (1) | 2025.06.15 |
|---|---|
| [개념]매서드(Method) (0) | 2025.06.02 |
| [개념]If문과 Switch-case문 (0) | 2025.05.30 |
| [개념]키보드로부터 데이터 입력 받기 (1) | 2025.05.29 |
| [개념]배열(Array) (0) | 2025.05.29 |