public class Example1 {
public static void main(String[] args) {
int num1 = 15;
int num2 = 5;
if (num1 > num2) // 조건식 참
{
System.out.println("num1이 num2보다 큽니다");
} else // 조건식 거짓
{
System.out.println("num1이 num2보다 작거나 같습니다");
}
}
}
8. 조건문(switch case)
switch(조건){
case 숫자1:
실행문
break;
case 숫자2:
실행문
break;
default:
실행문
break;
}
public class HelloWorld {
public static void main(String[] args) {
int a = 2;
switch (a) {
case 1:
System.out.println("일");
break;
case 2:
System.out.println("이");
break;
case 3:
System.out.println("삼");
break;
default:
throw new IllegalArgumentException("Unexpected value: " + a);
}
}
}
9. 반복문(for)
for(초기화; 종료조건; 반복실행){
반복적으로 실행될 구문
}
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Coding Everybody " + i);
}
}
}
10. 반복문(while)
while(조건){
반복 실행 영역
}
public class HelloWorld{
public static void main(String[] args) {
while (true) {
System.out.println("Coding");
}
}
}
* 반복문 제어
break;
반복작업을 중간에 중단시키고 싶을 때 사용. 해당 라인에서 반복문을 중단
continue;
현재 실행문을 중단하고 넘어가고 다음 반복순서로 이어간다
11. static, final
static : 고정, 한번 생성하여 계속 사용함, 프로그램이 실행될 때 먼저 메모리를 할당하고 시작