오늘의 과정 목록
- 데이터 조작을 위한 각종 연산자
(비교 연산자, 논리 연산자, 상항 연산자)- 프로그램의 흐름을 제어하는 제어문
(조건문, 반복문)
비교 연산자 (true or false)
int a = 15;
int b = 9;
boolean compareResult;
// 동등 비교 연산자
compareResult = a == b;
System.out.println(compareResult);
// 출력결과 : false
// 부등 비교 연산자
compareResult = a != b;
System.err.println(compareResult);
// 출력결과 : true
// 크다 비교 연산자, 크거나 같다 비교 연산자
compareResult = a >= b;
System.out.println(compareResult);
// 출력결과 : false
// 작다 비교 연산자, 작거나 같다 비교 연산자
compareResult = a <= b;
System.out.println(compareResult);
// 출력결과 : true
//캐릭터 'a'는 아스키코드 값으로 변환되기 때문에 비교가 가능하다.
char tmpC = 'a'; // a = 97
compareResult = tmpC > a;
System.out.println(compareResult);
// 출력 결과 : true
논리 연산자
final boolean TRUE = true; // 상수
final boolean FALSE = false;
// 논리 AND 연산자
compareResult = TRUE && TRUE;
System.out.println(compareResult);
compareResult = TRUE && FALSE;
System.out.println(compareResult);
compareResult = FALSE && TRUE; // Dead code -> 좌항에 false가 뜨면 아예 우항을 읽지 않는다.
System.out.println(compareResult);
// 출력 결과 : true false false
// 논리 OR 연산자
compareResult = TRUE || TRUE; // Dead code -> true를 좌항에서 만났기 때문에 뒤에 있는 코드를 읽지 않는다.
System.out.println(compareResult);
compareResult = TRUE || FALSE; // Dead code -> true를 좌항에서 만났기 때문에 뒤에 있는 코드를 읽지 않는다.
System.out.println(compareResult);
compareResult = FALSE || FALSE;
System.out.println(compareResult);
// 출력 결과 : true true false
// 논리 NOT 연산자
compareResult = !TRUE;
System.out.println(compareResult);
compareResult = !FALSE;
System.out.println(compareResult);
// 출력 결과 : false true
삼항 연산자
int a = 15;
int b = 9;
String message1 = a > b ? "a는 b보다 큽니다." : "a는 b보다 크지 않습니다.";
System.out.println(message1);
// 출력 결과 : a는 b보다 큽니다.
// 값 재할당
a = 0;
String message2 = a > b ? "a는 b보다 큽니다." : "a는 b보다 크지 않습니다.";
System.out.println(message2);
// 출력 결과 : a는 b보다 크지 않습니다.
// 주의점
//String message3 = a > b ? "a는 b보다 큽니다." : 4;
//System.out.println(message3);
/*
오류) Type mismatch: cannot convert from int to String
해석) 유형 불일치: int에서 string으로 변환할 수 없습니다
유형이 String이기 때문에 int인 숫자가 올 수 없다.
*/
// 심화과정 string에 반환하는 식 a= 0;
String message4 = a > b ? "a는 b보다 큽니다." : (a < b ? "a는 b보다 작습니다" : "a는 b와 같습니다");
System.out.println(message4);
// 출력 결과 : a는 b보다 작습니다.
a=9;
String message5 = a > b ? "a는 b보다 큽니다." : (a < b ? "a는 b보다 작습니다" : "a는 b와 같습니다"); // 반환을 두 번 함.
System.out.println(message5);
// 출력 결과 : a는 b와 같습니다.
제어문
- if문
int age1 = 20;
if (age1 > 19) System.out.println("성인입니다."); // 한 문장으로도 사용 가능
System.out.println("if문 외부");
// 출력 결과 : 성인입니다.
// if문 외부
int age1 = 20;
if (age1 > 19) {
System.out.println("성인입니다.");
}
System.out.println("if문 외부");
// 출력 결과 : 성인입니다.
// if문 외부
int age2 = 10;
if (age2 > 19) {
System.out.println("성인입니다.");
}
System.out.println("if문 외부");
// 출력 결과 : if문 외부
- else
age1 = 20; age2 = 10;
if (age1 > 19) {
System.out.println("성인입니다.");
} else {
System.out.println("미성년자입니다.");
}
// 출력 결과 : 성인입니다.
if (age2 > 19) {
System.out.println("성인입니다.");
} else {
System.out.println("미성년자입니다.");
}
// 출력 결과 : 미성년자입니다.
int number = 0;
if(number > 0)
System.out.println("양수입니다.");
else if(number < 0) {
System.out.println("음수입니다.");
} else {
System.out.println("0입니다.");
}
// 출력 결과 : 0입니다.
- switch
int day = 5;
switch (day) { //
case 1:
System.out.println("일요일");
case 2:
System.out.println("월요일");
case 3:
System.out.println("화요일");
case 4:
System.out.println("수요일");
break; // 코드 블록을 벗어난다. 스위치 문을 벗어나게 되어 밑의 코드를 읽지 않아 디폴트 값에 도달하지 못한다.
case 5:
System.out.println("목요일");
case 6:
System.out.println("금요일");
case 7:
System.out.println("토요일");
default:
System.out.println("한주 끝!!"); // 어떤 상황에서도 실행됨. 기본 값. 무조건 실행되는 값. 조건들과는 관련이 없다.
}
// 출력 결과 : 목요일
// 금요일
// 토요일
// 한주 끝!!
반복문
- for
1) 특정 코드를 몇 번 반복하고자 할 때.
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
.
.
.
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
System.out.println("이 문장은 100번 출력됩니다.");
/*
출력 결과 :
이 문장은 100번 출력됩니다.
이 문장은 100번 출력됩니다.
이 문장은 100번 출력됩니다.
이 문장은 100번 출력됩니다.
.
.
.
이 문장은 100번 출력됩니다.
이 문장은 100번 출력됩니다.
이 문장은 100번 출력됩니다.
이 문장은 100번 출력됩니다.
*/
for(int count = 1; count <= 2; count++) {
System.out.println("이 문장은 2번 출력됩니다.");
}
// 출력 결과 : 이 문장은 2번 출력됩니다.
// : 이 문장은 2번 출력됩니다.
for(int count = 0; count < 2; count++) { // 배열에 접근할 때 많이 사용. 때문에 대부분 초기값을 0으로 사용
System.out.println("이 문장은 2번 출력됩니다.");
}
// 출력 결과 : 이 문장은 2번 출력됩니다.
// 이 문장은 2번 출력됩니다.
2) 특정한 순서를 가지고 반복하고자 할 때
패턴) 3 * n = (3 * n)
System.out.println("3 * 1 = 3");
System.out.println("3 * 2 = 6");
System.out.println("3 * 3 = 9");
System.out.println("3 * 4 = 12");
System.out.println("3 * 5 = 15");
System.out.println("3 * 6 = 18");
System.out.println("3 * 7 = 21");
System.out.println("3 * 8 = 24");
System.out.println("3 * 9 = 27");
// for문 사용
for(int n = 1; n < 10; n++) {
System.out.println("3 * " + n + " = " + (3 * n));
}
/*
출력 결과 :
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
*/
3) 배열을 초기화할 때 / 배열에 접근할 때
int[] numbers = new int[10];
for(int index = 0; index < numbers.length; index++) {
numbers[index] = index;
}
for(int index = 0; index < numbers.length; index++) {
System.out.println(numbers[index]);
}
// 출력 결과 : 0
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
fareach(향상된 for문)
- 컬렉션(리스트, 배열 등)의 요소를 꺼내어 작업으로 각 요소에 대해 코드를 실행하는 방법을 제공한다.
- 예시에 있는 item라는 변수의 값을 1로 바꾼 것. 배열의 값을 바꾼것이 아니다.
- foreach 반복문은 읽기전용으로 읽어서 쓸 때만 사용하면 된다. 값을 바꿀 수 없다.
- 배열 초기화 할 때 쓰면 안 됨!
- for(반복할배열의요소타입 변수명: 반복할배열) {}
for(int item: numbers) {
System.out.println(item);
}
/*
출력결과
0
1
2
3
4
5
6
7
8
9
*/
example)
네자리 수의 수의 모든 자리 수를 다 더한 값은?
예시 - 7654 = 22
1)
number = 7654;
int a = number % 10; // =) int a = (number % 10) / 1;
int b = (number % 100) / 10;
int c = (number % 1000) / 100;
int d = (number % 10000) / 1000;
System.out.println(a + b + c + d);
/*
출력 결과 : 22
*/
2)
number = 7654;
// (number % (10^n)) / 10 ^ (n - 1) => 위 예시의 반복되는 식
int sum = 0;
for(int n = 1; n <= 4 ; n++) {
sum += (number % Math.pow(10, n)) / Math.pow(10, n-1); // 10^n => Math.pow(10,n)
}
System.out.println(sum);
/*
출력결과 : 22
*/
- while
number = 0;
while (number < 10) {
System.out.println(number);
number++;
}
/*
출력 결과
0
1
2
3
4
5
6
7
8
9
*/
number = 0;
while(number < 10) {
if(number == 9) break;
if(number % 2 == 1) {
number++;
continue; // 진행되지 않고 다시 조건문으로 넘어감. 넘버는 계속 1이기 때문에 계속 돌아감.
}
System.out.println(number);
number++;
}
/*
출력 결과
0
2
4
6
8
*/
while(true) {
Scanner scanner = new Scanner(System.in);
int inputNumber = scanner.nextInt();
if(inputNumber == -1) break;
System.out.println("입력한 수 : " + inputNumber);
}
/*
출력 결과
ex)
2
입력한 수 : 2
5
입력한 수 : 5
8
입력한 수 : 8
-1 // break 조건 문인 -1을 입력하면 더 이상 입력할 수 없다.
*/