1.if / else
주어진 boolean 값에 따라 명령문 실행 여부를 결정
int score = 85;
// else if : 첫 if문이 false일 때 다른 조건을 연속 사용
// else만 사용하는 것은 맨 마지막에
if (score > 90) {
System.out.println('A');
} else if (score > 80) {
System.out.println('B');
} else if (score > 70) {
System.out.println('C');
} else if (score > 60) {
System.out.println('D');
} else {
System.out.println('F');
}
- 위 코드는 가독성이 떨어져 지양된다
지향하는 코드 (보다 가독성 좋은 방식)
int score = 85;
// return문: 해당 메소드를 종료하고 빠져나옴
if (score > 90) {
System.out.println('A');
return;
}
if (score > 80) {
System.out.println('B');
return;
}
if (score > 70) {
System.out.println('C');
return;
}
if (score > 60) {
System.out.println('D');
return;
}
System.out.println('F');
- 위처럼 독립적으로 동작하는 코드가 좀 더 가독성이 좋다
2.switch
byte fingersOut = 2;
// switch : 괄호 안에 기준이 될 변수를 받음
// 가능한 자료형: byte, short, int, char, String, enum(이후 배움)
switch (fingersOut) {
// case : 기준에 일치하는 case로 바로 이동
// break : switch문 실행을 종료
// default: 해당하는 case가 없을 때 - 마지막에 작성
case 2:
System.out.println("가위");
break;
case 0:
System.out.println("바위");
break;
case 5:
System.out.println("보");
break;
default:
System.out.println("무효");
}
- case : 기준에 일치하는 case로 바로 이동
- break : switch문 실행을 종료
- default: 해당하는 case가 없을 때 - 마지막에 작성
3.for & for-each
주어진 조건이 충족되는 동안 특정 작업을 반복
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
- 실행 과정
- 루프 안에서 사용할 변수 초기화
- 루프를 실행하기 위한 조건 확인
- 조건을 충족시 실행할 내용
- 각 루프가 끝날때마다 이행할 내용
- 1번 이후 2를 충족할 동안 2~4 반복
- 변수명은 원하는대로 사용 가능
- 일반적으로 기본형에는
i
를 많이 사용 - *문맥에 따라 index를 뜻함
- 일반적으로 기본형에는
배열의 원소를 반복
int[] multiOf4 = new int[] {1,2,3,4,5};
int sumOfArray = 0;
for (int num : multiOf4) {
sumOfArray += num;
}
IntelliJ IDEA 단축어 : foreach
continue와 break
for (int i = 0; i < 100; i++) { // continue : 한 루프만 건너뜀 if (i % 3 == 0) continue; // break : 블록 전체를 종료 if (i == 10) break; System.out.println(i); }
continue : 한 루프만 건너뜀
break : 블록 전체를 종료
4.while & do while
while : 조건이 true일 동안 반복 수행
int i = 0;
// while 문의 괄호에는 종료조건만
while (i < 10) {
// 종료조건 충족을 위한 값 변화는 외적으로
System.out.println(i++);
}
do ... while : 일단 수행하고 조건을 봄
int enemies = 0;
System.out.println("일단 사격");
do {
System.out.println("탕");
if (enemies > 0) enemies--;
} while (enemies > 0);
System.out.println("사격중지 아군이다");
5.메소드
- 타 언어의 함수 function 과 같은 개념
- 자바는 모든 것이 클래스의 요소이므로 메소드 method 라 부름
의미 : 반복을 최소화
public class Main {
public static void main(String[] args) {
double xx = 3, yy = 4;
addSubtMultDiv(xx, yy);
xx = 10; yy = 2;
addSubtMultDiv(xx, yy);
xx = 7; yy = 5;
addSubtMultDiv(xx, yy);
}
}
// ⭐️ 메인 메소드 외부에 선언할 것
static void addSubtMultDiv (double a, double b) {
System.out.printf("%f + %f = %f%n", a, b, a + b);
System.out.printf("%f - %f = %f%n", a, b, a - b);
System.out.printf("%f * %f = %f%n", a, b, a * b);
System.out.printf("%f / %f = %f%n", a, b, a / b);
}
메소드 오버로딩
static int add(int a, int b) { return a + b; }
// 매개변수의 개수가 다름
static int add(int a, int b, int c) { return a + b + c; }
// 매개변수의 자료형이 다름
static double add(double a, double b) { return a + b; }
// 매개변수의 자료형 순서가 다름
static String add(String a, char b) { return a + b; }
static String add(char a, String b) { return a + b; }
// 반환 자료형이 다른 것은 오버로딩 안 됨 - 다른 함수명 사용
// static double add(int a, int b) { return (double) (a + b); }
- 같은 메소드 이름, 다른 매개변수
- 다른 자료형의 값들로 같은 성질의 작업을 정의할 때
재귀 메소드
static void upTo5 (int start) {
System.out.println(start);
if (start < 5) {
upTo5(++start);
} else {
System.out.println("-- 종료 --");
}
}
- 스스로를 호출하는 메소드
- 호출시마다 메모리에 스택이 축적 - 초과시 스택오버플로우 stack overflow 에러
- 다른 메소드를 호출한 메소드는 호출된 메소드가 종료될 때까지 메모리에 남아 있음
- 호출이 반복될수록 위와 같이 메소드들이 쌓이게 됨
- 예시 (팩토리얼)*
꼬리 재귀 최적화
- 재귀 코드를 내부적으로 루프 형태로 바꿔서 스택이 쌓이지 않도록 함
- 자바에서는 현재 기본적으로 제공하지 않음
- 반복 횟수가 너무 많아지는 작업에는 사용하지 말 것!
6.키보드 입력
문자열 입력받기
// IDE가 최상단에 import java.util.Scanner 자동 작성
Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
String str3 = sc.nextLine();
System.out.println("str1: " + str1);
System.out.println("str2: " + str2);
System.out.println("str3: " + str3);
next : 스페이스를 비롯한 공백 단위로 끊어서 (토큰으로) 문자열을 받음
nextLine : 줄바꿈 단위로 끊어서 문자열을 받음