for 문
- 정해진 횟수만큼 반복할 때 사용하는 반복문
- 끝이 정해져 있는 반복문
- 반복 횟수를 알 때 주로 사용
기본 구조
for(초기식; 조건식; 증감식){ // 조건식이 참인 동안 반복 실행 }
while 문
- 어떤 조건을 정해 놓고 반복하는 반복문
- 끝이 정해져 있지 않은 반복문
- 반복 횟수를 알 수 없을 때 (무한 반복) 해야 할 때 주로 사용
기본 구조
while(조건식){ // 조건식에 true를 넣을시 무한 반복 한다. // 조건식이 참인 동안 반복 실행 }
무한 반복을 하는 경우 ‘데몬(Daemon)’이 되는데 이를 멈추기 위해서
‘인터럽트(Interrupt)’를 걸어 줘야 한다. 그래서 while에는 break를 쓰는 것을 추천한다.
ex) break;, Ctrl + C 등
break 문
- 현재의 반복문을 벗어날 때 사용
- 레이블을 붙이면 중첩 반복문도 벗어날 수 있다.
continue 문
- 반복문에서 현재의 반복을 건너뛰어서 다음 반복으로 넘어가도록 한다.
- 핵심 로직을 넘어갈 때 주로 사용
- 아이디 로그인과 비슷하게 생각하면 된다.
for 문 예제
for 문 1번 예제
package ex03; public class ForExample1 { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { System.out.println("i의 값은: " + i); } } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252Ffa62e7a0-1933-4fb1-a624-7cdb008e9ab5%252FUntitled.png%3Ftable%3Dblock%26id%3D47a6e900-3e26-4d58-b610-f96b2f4e997c%26cache%3Dv2&w=3840&q=75)
for 문 2번 예제
package ex03; public class Sum { public static void main(String[] args) { int sum = 0; // 1. sum이라는 저장공간에 i의 값을 중첩시켜서 저장한다. (1~10까지의 합) for (int i = 1; i <= +10; i++) { sum = sum + i; } System.out.println("1부터 10까지의 정수의 합 = " + sum); } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252F5b7978a3-4275-4773-9395-0d46bd62fda9%252FUntitled.png%3Ftable%3Dblock%26id%3D2b2da55c-24f1-47b8-8333-ae8397e5b337%26cache%3Dv2&w=3840&q=75)
for 문 3번 예제
package ex03; import java.util.Scanner; public class Factorial { public static void main(String[] args) { long fact = 1; // 팩토리얼은 길이가 길어질수 있기에 long으로 선언하고 1로 초기화하는 이유는 모든 값을 곱하여야 하기때문에 0을 하면 무조건 값이 0이 되기 때문이다. int n; Scanner sc = new Scanner(System.in); System.out.print("정수를 입력하시오: "); n = sc.nextInt(); for (int i = 1; i <= n; i++) { fact = fact * i; } System.out.print(n + "!은 " + fact + "입니다."); } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252F74511b6c-1461-4018-a0f5-94f6faf6d35e%252FUntitled.png%3Ftable%3Dblock%26id%3Df1a22083-c6d9-4da1-94b4-6a563f361c67%26cache%3Dv2&w=3840&q=75)
for 문 4번 예제
package ex03; import java.util.Scanner; public class Divisor { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("양의 정수를 입력하시오: "); int n = sc.nextInt(); System.out.print(n + "의 약수는 다음과 같습니다."); for (int i = 1; i <= n; i++) { if (n % i == 0) { System.out.print(" " + i); } } } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252F68f238e8-0858-4a53-97dc-5123dd87ed71%252FUntitled.png%3Ftable%3Dblock%26id%3D854c4003-818b-4ca5-b67a-972dd25caa33%26cache%3Dv2&w=3840&q=75)
for 문 5번 예제
package ex03; public class NestedLoop { public static void main(String[] args) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { System.out.print("*"); } System.out.println(); } } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252F6197840c-e3c1-4ca8-b314-d2259ba4e1fa%252FUntitled.png%3Ftable%3Dblock%26id%3D6273b5fc-2c5d-4d79-9cef-d5bc352ca3d8%26cache%3Dv2&w=3840&q=75)
while 문 예제
while 문 1번 예제
package ex03; import java.util.Scanner; public class GetSum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sum = 0, value = 0; while (value != -1) { sum = sum + value; System.out.print("정수를 입력하시오: "); value = sc.nextInt(); } System.out.print("정수의 합은 " + sum + "입니다."); } }
출력 결과
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252Fedac1446-90ab-40d8-9bd3-9f730ced7d9c%252FUntitled.png%3Ftable%3Dblock%26id%3Ddbedacf1-c40d-489f-96c0-49fc488c3804%26cache%3Dv2&w=3840&q=75)
while 문 2번 예제
package ex03; import java.util.Scanner; public class Averager { public static void main(String[] args) { int total = 0, count = 0; Scanner sc = new Scanner(System.in); while (true) { System.out.print("점수를 입력하시오: "); int grade = sc.nextInt(); if (grade < 0) { break; } total = total + grade; count++; } System.out.println("평균: " + total / count); } }
출력 결괴
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fb1939378-3cd6-4f1f-9032-6c4df0711232%252Fa0654691-bf19-4d82-8000-e8834fdeb28f%252FUntitled.png%3Ftable%3Dblock%26id%3D2b128af5-fe13-407b-80a3-ff1786e629c2%26cache%3Dv2&w=3840&q=75)
Share article