break

kukudas·2022년 2월 7일
0

TypeScript

목록 보기
10/39

Using TypeScript break to terminate a loop

break로 반복문 종료하고 반복문 다음에 있는 코드로 프로그램 진행시킬 수 있음.

let products = [
    { name: 'phone', price: 700 },
    { name: 'tablet', price: 900 },
    { name: 'laptop', price: 1200 }
];

// let 안쓰고 var사용한거는 밑에서 i값을 보여주기 위함임.
for (var i = 0; i < products.length; i++) {
    if (products[i].price == 900)
        break;
}

// show the products
console.log(products[i]);

Output:

{ name: 'tablet', price: 900 }

Using the break statement to break a switch

아래는 breakswitch문 나가는 예시임.

let products = [
    { name: 'phone', price: 700 },
    { name: 'tablet', price: 900 },
    { name: 'laptop', price: 1200 }
];

let discount = 0;
let product = products[1];

switch (product.name) {
    case 'phone':
        discount = 5;
        break;
    case 'tablet':
        discount = 10;
        break;
    case 'laptop':
        discount = 15;
        break;
}

console.log(`There is a ${discount}% on ${product.name}.`);

Output:

There is a 10% on tablet.
  • break로 반복문이나 switch 종료가능

출처

0개의 댓글

관련 채용 정보