while(조건문;)
- 증감문 없으면 무한루프
- 프로그램이 종료되지 않고(InputMismatchException) 사용자가 정상 입력할때까지 무한루프 돌릴 수 있다. (입력 예외 관리)
- 조건식에 함수 올 수 있지만 반환값이 boolean 이여야 한다.
- 수행문, 증감문의 순서
package me.day05.whiles;
import java.util.Scanner;
public class WhileExample {
public static void main(String[] args) {
int i = 0;
while(i < 5) {
System.out.println("hello");
i++;
}
System.out.println("i = " + i);
System.out.println();
i = 1;
while(i <= 5) {
System.out.println("hello");
i++;
}
System.out.println("i = " + i);
System.out.println();
i = 5;
while(i > 0) {
System.out.println("hello");
i--;
}
System.out.println("i = " + i);
System.out.println();
Scanner scanner = new Scanner(System.in);
scanner.nextInt();
int sum = 0;
i = 1;
while (i <= 10) {
sum += i;
i ++;
}
System.out.println("sum = " + sum);
sum = 0;
i = 1;
while (i <= 10) {
i ++;
sum += i;
}
System.out.println("sum = " + sum);
i = 1;
while (i <= 10) {
System.out.println("hello");
i++;
}
i = 1;
while (i <= 10) {
i++;
System.out.println("hello");
}
}
}
for(초기값; 조건문; 증감문;)
package me.day05.fors;
import java.util.Scanner;
public class ForExample {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
System.out.printf("[%d] hello\n", i);
}
System.out.println();
for(int i = 1; i <= 5; i++) {
System.out.printf("[%d] hello\n", i);
}
System.out.println();
for(int i = 5; i > 0; i--) {
System.out.printf("[%d] hello\n", i);
}
System.out.println();
for(;;) {
}
}
}
looping example
package me.day05.examples;
import java.util.Scanner;
public class LoopingExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double average = 0.0;
final int NUM = 5;
int sum = 0;
for (int i = 0; i < NUM; i++) {
System.out.print("input num: ");
sum += scanner.nextInt();
}
average = (double) sum / NUM;
System.out.println("average = " + average);
System.out.println();
int num = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < NUM; i++) {
System.out.print("input num: ");
num = scanner.nextInt();
if (min > num) min = num;
if (max < num) max = num;
}
System.out.println("min = " + min);
System.out.println("max = " + max);
scanner.close();
}
}
do while
- 여러번 출력하기
- while 과의 차이점 : 첫 조건이 틀려도 무조건 한번은 실행됨
- do while 보다 무한루프 + breake 를 더 많이 사용한다.
- 조건이 참일때 반복문 빠져 나옴 (조건 반대로 작성)
package me.day05.dowhile;
public class DoWhileExample {
public static void main(String[] args) {
int i = 0;
do {
System.out.println("hello");
i++;
} while (i < 3);
System.out.println("i = " + i);
System.out.println();;
i = 0;
do {
System.out.println("hello");
i++;
} while (i < 0);
System.out.println("i = " + i);
System.out.println();;
i = 0;
while (true) {
System.out.println("hello");
i++;
if (!(i < 0)) break;
}
System.out.println("i = " + i);
System.out.println();
}
}
for break / for continue / while(true) break
- 특정 조건의 값 입력 받으면 종료
break
: 조건에서 반복 블록 종료
continue
: 조건에서 반복 블록 종료X -> 아래 문장 (블록안) 무시
package me.day05.breaks;
import java.util.Scanner;
public class BreakContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) break;
System.out.println("i = " + i);
}
System.out.println("out of for");
for (int i = 1; i <= 10; i++) {
System.out.println("i = " + i);
if (i % 3 == 0) break;
}
System.out.println("out of for");
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) continue;
System.out.println("i = " + i);
}
System.out.println("out of for");
for (int i = 1; i <= 10; i++) {
System.out.println("i = " + i);
if (i % 3 == 0) continue;
}
System.out.println("out of for");
Scanner scanner = new Scanner(System.in);
while (true) {
char ch = scanner.next().charAt(0);
if (ch == 'q') break;
String str = "";
String str2 = "";
str += ch;
str2 = ch + str;
System.out.println("str = " + str);
System.out.println("str2 = " + str2);
}
scanner.close();
}
}
이중 for문
- 구구단, 일차함수, palindrome, checker
- 실행속도에 안좋으므로 지양하는 것이 좋다
- 내부 반복문 종료 :
break
- 외부 반복문 종료 :
break
2번 / return
1번
package me.day05.nested;
public class NestedLoopExample {
public static void main(String[] args) {
for (int i = 2; i < 10; i++) {
for (int j = 1; j < 10; j++) {
System.out.printf("%d x %d = %d\n", i, j, i * j);
}
System.out.println();
}
System.out.println("==========================\n");
int i = 2;
while (i < 10) {
int j = 1;
while (j < 10) {
System.out.printf("%d x %d = %d\n", i, j, i * j);
j++;
}
System.out.println();
i++;
}
System.out.println("==========================\n");
for (int k = 1; k < 10; k++) {
for (int l = 1; l < 10; l++) {
if (k == l) {
System.out.printf("(%d, %d)\n", k, l);
}
}
}
System.out.println("\n==========================\n");
int k = 1;
for (int l = 1; l < 10; l++) {
System.out.printf("(%d, %d)\n", k, l);
k++;
}
System.out.println("\n==========================\n");
for (int x = 2; x < 10; x++) {
for (int y = 1; y < 10; y++) {
if (y % 3 == 0) break;
System.out.printf("%d x %d = %d\n", x, y, x * y);
}
System.out.println();
}
System.out.println("==========================\n");
boolean checker = false;
for (int x = 2; x < 10; x++) {
for (int y = 1; y < 10; y++) {
if (y % 3 == 0) {
checker = true;
break;
}
System.out.printf("%d x %d = %d\n", x, y, x * y);
}
if (checker) break;
System.out.println();
}
System.out.println("\n==========================\n");
for (int x = 2; x < 10; x++) {
for (int y = 1; y < 10; y++) {
if (y % 3 == 0) return;
System.out.printf("%d x %d = %d\n", x, y, x * y);
}
System.out.println();
}
System.out.println("\n==========================\n");
}
}