If-else 문

조건식이 참일 때와 거짓일 때로 나눠 처리

if (조건식) {
	// 조건식이 true일 때 수행될 문장
}
else {
	// 조건식이 false일 때 수행될 문장
}

 

true일 때

public class practice {
    public static void main(String[] args) {
        if (3 > 1) {
            System.out.println(3); // 3
        }
        else {
            System.out.println(1); // 1
        }
    }
}

3

 

false일 때

public class practice {
    public static void main(String[] args) {
        if (3 < 1) {
            System.out.println(3); // 3
        }
        else {
            System.out.println(1); // 1
        }
    }
}

1
반응형

'이것이 자바다 > JAVA1' 카테고리의 다른 글

임의의 정수 만들기 Math.random() 메서드  (0) 2023.05.17
if - else if 문  (0) 2023.05.17
조건식  (0) 2023.05.17
if 문  (0) 2023.05.17
복합 대입 연산자 +=  (0) 2023.05.17

+ Recent posts