프로그래밍 언어/JAVA
3 - 제어문
jjonse
2023. 1. 3. 21:48
if 문법
if(조건식) {
실행내용;
}
else if(조건식) {
실행내용;
} else {
실행내용;
}
public class Main {
public static void main(String[] args) {
int x = 55;
if(50 <= x && x <= 60) {
System.out.println("승리");
} else {
System.out.println("패배");
}
}
}
반복문(while, do~while, for)
while(조건식) {
실행코드;
}
-> 반복문을 사용하는 이유는 어떤 작업을 n번 반복하기 위해서다.
for문
for(초기식; 조건식; 증감식) {
실행내용;}
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 3; i++) {
System.out.println(i);
}
}
}