본문 바로가기

콤퓨타/Programming) 왕초보 자바

15. (Loops and Conditions) Using If

 

 

while 문, for문 

boolean과 equality  까지 배웠지만 그중에 가장 쓰임세가 많고 중요한 개념은 아마도 IF 인가 보다. (선생님이 강조 하심) 

 

기본적으로 

if() {

}

요렇게 생겼고 if 다음에 들어가는 statement 가 참일 경우에만 그 다음 괄호 {} 안에 있는 내용이 작동된다고. 

 

public class App {

	public static void main(String[] args) {

		if (4 < 5) {
			System.out.println("Condition is true");
		}			
	System.out.println("The end of the program.");
    }
}

따라서, 

이 경우에 조건값이 4<5 가 참이므로 다음과 같이 출력된다. 

 

만약에 if 다음에 참이 아닌 내용이 들어가 있다면? 

public class App {

	public static void main(String[] args) {

		if (4 > 5) {
			System.out.println("Condition is true");
		}			
	System.out.println("The end of the program.");
    }
}

 

 

 

코드 작성하는 단계부터 문제적인 코드임을 노란색으로 표시해준다. 

 

그리고 anyways, 출력도 가능하다. 대신 거짓이기 때문에 if에 속한 문구는 출력되지 않고, 

if 바깥에 있는 문구만 출력된다.

 

여기까지가 기본이고, 

그다음 기본은 int에 변수와 변수값을 지정해서 변주를 주는것이다.

 

public class App {

	public static void main(String[] args) {

		if (4 < 5) {
			System.out.println("Condition is true");
		}
		int apples = 4;
		int bananas = 5;
		if (apples < bananas) {
			System.out.println("We have more bananas than the apples. ");
		}

		System.out.println("End of the program.");
	}

}

 

 

그래봤자 간단하기는 하지만, 

 

 

4분짜리 짧은 강의이지만 IF는 강조를 많이 하셨으니 아마 챗지피티와 연구를 좀 더 많이 해봐얄듯. 

 

해피코딩...