01. if문 (제어문 : 조건문, 반복문, 중지, 건너뛰기)

조건을 주어 true나false로 나타낼 수 있습니다.

{
    if (1) {
        document.write("실행되었습니다(true).");
    }
    else {
        document.write("실행되었습니다(false).");
    }
}
결과보기

02. if문 생략

괄호를 생략할 수 있습니다.

{
    const num = 100;

    if (num) document.write("실행되었습니다(true).");
    else document.write("실행되었습니다(false).");
}
결과보기

03. 다중 if문

if문안에 여러 조건을 넣을 수 있습니다.

{
    const num = 100;

    if (num == 90) {
        document.write("실행되었습니다(num == 90).");
    } else if (num == 100) {
         document.write("실행되었습니다(num == 100).");
    } else if (num == 110) {
        document.write("실행되었습니다(num == 110).");
    } else if (num == 120) {
        document.write("실행되었습니다(num == 120).");
    } else {
        document.write("실행되었습니다(num == 값이 없음).");
    }
}
결과보기

04. 중첩 if문

if문안에 if문을 쓸 수 있습니다.

{
    const num = 100;

    if (num == 100) {
         document.write("실행되었습니다(1).");
        if (num == 100) {
             document.write("실행되었습니다(2).");
            if (num == 100) {
                 document.write("실행되었습니다(3).");
            }
        }
    }
    else {
        document.write("실행되었습니다.(4)");
    }
}
결과보기

05. 삼항 연산자

조건부 삼항 연산자는 JavaScript에서 세 개의 피연산자를 취할 수 있는 유일한 연산자입니다.
맨 앞에 조건문 들어가고. 그 뒤로 물음표(?)와 조건이 참truthy이라면 실행할 식이 물음표 뒤로 들어갑니다. 바로 뒤로 콜론(:)이 들어가며 조건이 거짓falsy이라면 실행할 식이 마지막에 들어갑니다.

{
    const num = 100;
            
    if (num == 100) { //num값이 100이면 true 출력, 아니면 false 출력
        document.write("실행되었습니다(true).");
    } else {
        document.write("실행되었습니다(false).");
        }

    document.write(num == 100 ? document.write("실행되었습니다(true).") :  document.write("실행되었습니다(false)."));
}
결과보기

06. switch문

switch문은 하나 이상의 case문으로 구성됩니다. 대개 default문도 있지만, 이는 필수는 아닙니다.

{
    const num = 100;

    switch (num) {
        case 100:
            document.write("실행되었습니다(num == 100).");
            break;
        case 110:
            document.write("실행되었습니다(num == 110).");
            break;
        case 120:
            document.write("실행되었습니다(num == 120).");
            break;
        case 130:
            document.write("실행되었습니다(num == 130).");
            break;
        default:
            document.write("실행되었습니다(num == 값이 없음).");
    }
}
결과보기

07. while문

while문은 반복할 때 사용하는 문법입니다.

{
    let num = 1;

    while(num <= 5){
        document.write("실행되었습니다.");
        num++;
    }

}    
결과보기

08. do while문

do while문은 반복할 때 사용하는 문법입니다.

{
    let num = 1;

    do {
        document.write("실행되었습니다.");
        num++;
    }while(num <= 5)
}
결과보기

09. for문

for문은 반복할 때 사용하는 문법입니다.

{
    for(let i = 0; i < 100; i++){
         document.write(i + "실행되었습니다.");
    }

    let num = [1,2,3,4,5,6];
    for(let i = 0; i < num.length; i++){
         document.write(num[i] + "실행되었습니다.");
    }
}
결과보기

10. 중첩 for문

for문안에 또 for문을 쓸 수 있습니다.

{
    for(let i = 1; i <= 2; i++){
        for(let j = 1; j <= 5; j++){
            document.write(i+"*"+j+"="+i*j);
        }
    }
}
결과보기

11. break문

코드 실행 중 break문을 만나게 되면 현재 실행문을 중지시키고 다음 실행문으로 넘어가게 합니다.

{ //for문을 이용해서 1-20까지 출력     
    for (let i = 1; i <= 20; i++){
        document.write(i);
        if (i == 10) {
            break; //10이 되었을 때, 끝남 
        }
    }
}
결과보기

12. continue문

continue문은 특정 조건을 만족 했을 때 그 해당하는 값만 건너뛰기 할 수 있습니다.

 { //for문을 이용해서 1-20까지 출력     
    for (let i = 1; i <= 20; i++){
        if (i == 10) {
            continue; //10이 생략되고 출력 
        }
        document.write(i);
    }
}
결과보기