: 조건문이란 주어진 조건에 따라서 애플리케이션을 다르게 동작하록 하는 것 (프로그래밍의 핵심)
조건문의 문법
프로그래밍에서 문(文, Statements)은 문법적인 완결성을 가진 하나의 완제품이라고 할 수 있다.
if문, for문, while문등이 여기에 해당한다.
절(節마디절, clause)은 문(statements)를 구성하고 있는 부품이라고 할 수 있다.
if
조건문은 if로 시작한다.
if 뒤의 괄호를 if절이라고 부르고, 중괄호가 감싸고 있는 구간을 then 절이라고 부르겠다.
조건문에서는 if 절의 값이 true일 때 then 절이 실행된다.
if 절이 false이면 then 절은 실행되지 않는다.
if(조건식){
실행문;
실행문;
}
조건식이 true값을가질 때 중괄호({ }),then절 안의 실행문을 작동시킨다.
반대로 조건식이 false이면 중괄호({ }),then절 안의 실행문은 동작하지 않고 if문을 빠져나간다.
<True>
package org.opentutorials.javatutorials.condition;
public class Condition1Demo {
public static void main(String[] args) {
if(true){
System.out.println("result : true");
}
}
}
<False>
if(false){
System.out.println("result : true");
}
package org.opentutorials.javatutorials.condition;
public class Condition2Demo {
public static void main(String[] args) {
if (true) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
}
System.out.println(5);
}
}
-> 1, 2, 3, 4, 5 출력
package org.opentutorials.javatutorials.condition;
public class Condition2Demo {
public static void main(String[] args) {
if(false){
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
}
System.out.println(5);
-> 5만 출력
'PL(ProgrammingLanguage) > JAVA' 카테고리의 다른 글
조건문(응용) (0) | 2021.02.28 |
---|---|
조건문(else) (0) | 2021.02.28 |
Boolean과 Comparison Operator (0) | 2021.02.28 |
연산자(단항연산자) (0) | 2021.02.28 |
연산자(형변환) (0) | 2021.02.26 |