1.2 연산자와 제어문
연산자(Operators) : 변수나 값에 대해 특정 연산을 수행하는 기호.
① 산술 연산자 : +, -, *, /, %(나머지), ~/(몫)
② 관계 연산자 : ==, !=, >, >=, <, <=
③ 논리 연산자 : &&, ||, !
④ 대입 연산자 : =, +=, -=, *=, /=
제어문(Control Flow Statements) : 프로그램의 실행 흐름을 제어하는 문법.
① 조건문 : if, else if, else
int score = 85;
if (score >= 90) {
print("A");
} else if (score >= 80) {
print("B");
} else {
print("C");
}
② 반복문 : for, while, do~while
for (int i = 0; i < 5; i++) {
print("$i");
}
int i = 0;
while (i < 5) {
print("$i");
i++;
}
③ switch문 : 여러 값 중 하나를 선택하여 실행.
String fruit = "apple";
switch (fruit) {
case "apple":
print("사과");
break;
case "banana":
print("바나나");
break;
default:
print("기타 과일");
}