230303 - switch, while, do while, for, break, continue

๋ฐฑ์Šน์—ฐยท2023๋…„ 3์›” 3์ผ
1

๐Ÿšฉ ์ œ์–ด๋ฌธ - ์กฐ๊ฑด๋ฌธ(Conditional Statement)

์ œ์–ด๋ฌธ์ด๋ž€ ์ฝ”๋“œ์˜ ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ๋ฌธ์žฅ์„ ๋œปํ•œ๋‹ค.
์ œ์–ด๋ฌธ์€ ์กฐ๊ฑด๋ฌธ, ์„ ํƒ๋ฌธ, ๋ฐ˜๋ณต๋ฌธ์ด ์žˆ๋Š”๋ฐ ๊ทธ ์ค‘ ์กฐ๊ฑด๋ฌธ์€ ํ•ด๋‹น ์กฐ๊ฑด์ด true/false์ผ ๋•Œ ๋™์ž‘ํ•œ๋‹ค.

switch ()

๐Ÿ“ ์„ค๋ช…

  • ์ •ํ•ด์ง„ ๋ฒ”์œ„ ์•ˆ์˜ ๊ฐ’์— ๋Œ€ํ•ด ํŠน์ •ํ•œ ์ผ์„ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

  • ์˜ˆ์ œ1

js

switch (day) {
  case 0:
    dayName = "์›”์š”์ผ";
    break;
  case 1:
    dayName = "ํ™”์š”์ผ";
    break;
  case 2:
    dayName = "์ˆ˜์š”์ผ";
    break;
  case 3:
    dayName = "๋ชฉ์š”์ผ";
    break;
  case 4:
    dayName = "๊ธˆ์š”์ผ";
    break;
  case 5:
    dayName = "ํ† ์š”์ผ";
    break;
  case 6:
    dayName = "์ผ์š”์ผ";
    break;
  default: // ์œ„์˜ case์— ํ•ด๋‹น์‚ฌํ•ญ์ด ์—†์„ ๋•Œ(=else)
    console.log("์˜ค๋Š˜์€ ํ•ด๋‹นํ•˜๋Š” ์š”์ผ์ด ์—†์Œ");
}

console.log(`์˜ค๋Š˜์€ ${dayName} ์ž…๋‹ˆ๋‹ค.`);
console.log();


// ์กฐ๊ฑด(case) ์—ฌ๋Ÿฌ ๊ฐœ ์‚ฌ์šฉ
let weather = "snowy";
let feeling;

switch (weather) {
  case "windy":
  case "muddy":
    feeling = "๐Ÿ˜ฎ";
    break;
  case "sunny":
    feeling = "๐Ÿ˜ฅ";
    break;
  case "rainy":
  case "snowy":
    feeling = "๐Ÿ˜";
    break;
}
console.log(feeling);
  • ์˜ˆ์ œ2

html

<style>
    body { background: lightblue; }
    body > div {
      width: 500px;
      margin: 200px auto;
      padding: 50px;
      text-align: center;
      background: rgba(255, 255, 255, .7);
    }
    #p1 { font-size: 20px; }
  </style>
</head>
<body>
  <div>
    <h1>์ ์ˆ˜ ๊ฒ€์ƒ‰</h1>
    <p id="p1">
      ์ ์ˆ˜
    </p>
  </div>

js

let sub = prompt("์˜์–ด, ๊ตญ์–ด,  ์ˆ˜ํ•™, ๊ณผํ•™ ์ค‘ ์•Œ๊ณ  ์‹ถ์€ ์ ์ˆ˜๋Š”?", "");
const elP1 = document.querySelector("#p1");

switch (sub) {
  case "์˜์–ด":
    sub += "๋Š” 88 ์ ์ž…๋‹ˆ๋‹ค." // sub = sub + "๋Š” 88์ ์ž…๋‹ˆ๋‹ค.";
    break;
  case "๊ตญ์–ด":
    sub += "๋Š” 99 ์ ์ž…๋‹ˆ๋‹ค."
    break;
  case "์ˆ˜ํ•™":
    sub += "๋Š” 65 ์ ์ž…๋‹ˆ๋‹ค." 
    break;
  case "๊ณผํ•™":
    sub += "๋Š” 80 ์ ์ž…๋‹ˆ๋‹ค." 
    break;

  default:
    sub += "๋Š”(์€) ํ•ด๋‹น ๊ณผ๋ชฉ์ด ์•„๋‹™๋‹ˆ๋‹ค."
    break;
}
elP1.textContent = sub;

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

์˜ˆ์ œ1

์˜ˆ์ œ2


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๐Ÿšฉ ์ œ์–ด๋ฌธ - ๋ฐ˜๋ณต๋ฌธ(Loop Statement)

์ œ์–ด๋ฌธ์ด๋ž€ ์ฝ”๋“œ์˜ ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ๋ฌธ์žฅ์„ ๋œปํ•œ๋‹ค.
์ œ์–ด๋ฌธ์€ ์กฐ๊ฑด๋ฌธ, ์„ ํƒ๋ฌธ, ๋ฐ˜๋ณต๋ฌธ์ด ์žˆ๋Š”๋ฐ ๊ทธ ์ค‘ ์กฐ๊ฑด๋ฌธ์€ ํ•ด๋‹น ์กฐ๊ฑด์ด true/false์ผ ๋•Œ ๋™์ž‘ํ•œ๋‹ค.

while ()

๐Ÿ“ ์„ค๋ช…

  • ์กฐ๊ฑด์ด true์ผ ๋•Œ ๊ณ„์† ์‹คํ–‰๋˜๋Š” ๋ฐ˜๋ณต๋ฌธ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

  • ์˜ˆ์ œ1

js

let num = 1;

while (num <= 10) {
  console.log(num);
  num++;
}

console.log();
  • ์˜ˆ์ œ2
let isActive = false;
let i = 0;

while (isActive) {
  console.log("์•„์ง ์‚ด์•„์žˆ์Œ" + i);

  if (i == 30) {
    // ์กฐ๊ฑด์— ๋งž์œผ๋ฉด ๋ฉˆ์ถค
    break;
  }
  i++;
}

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

์˜ˆ์ œ1

์˜ˆ์ œ2
isActive๊ฐ€ false๋ผ ์ถœ๋ ฅ ์•ˆ๋จ.
true๋กœ ๋ณ€๊ฒฝ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ





do {} while ()

๐Ÿ“ ์„ค๋ช…

  • ์กฐ๊ฑด์ด true์ผ ๋•Œ ๊ณ„์† ์‹คํ–‰๋˜๋Š” ๋ฐ˜๋ณต๋ฌธ์ด์ง€๋งŒ ๋ฌด์กฐ๊ฑด do ์•ˆ์˜ ๋‚ด์šฉ์„ ํ•œ๋ฒˆ ์‹คํ–‰ํ•œ ํ›„ ์กฐ๊ฑด ์ ์šฉ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

let isActive = true;
let i = 0;

while (isActive) {
  console.log("์•„์ง ์‚ด์•„์žˆ์Œ" + i);

  if (i == 30) {
    // ์กฐ๊ฑด์— ๋งž์œผ๋ฉด ๋ฉˆ์ถค
    break;
  }
  i++;
}

// do {} while ()
// ๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ์„ ์‹คํ–‰ํ•œ ํ›„ ์กฐ๊ฑด ์ ์šฉ
do {
  console.log("isActive๊ฐ€ false์ผ ๋•Œ do while ํ•œ๋ฒˆ ์‹คํ–‰ํ•˜๊ธฐ" + i);
} while (isActive) {
  console.log("์•„์ง ์‚ด์•„์žˆ์Œ" + i);
}

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

isActive๋ฅผ true๋กœ ๋ณ€๊ฒฝ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฌดํ•œ์ • ์ถœ๋ ฅ๋จ



๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ





for ()

๐Ÿ“ ์„ค๋ช…

  • ์–ด๋–ค ํŠน์ •ํ•œ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์œผ๋กœ ํŒ๋ช…๋‚  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • ์‹คํ–‰ ์ˆœ์„œ
    • ๋ณ€์ˆ˜ ์„ ์–ธ, ์ดˆ๊ธฐํ™”
    • ์กฐ๊ฑด์‹์ด ์ฐธ์ด๋ฉด ์ฝ”๋“œ๋ธ”๋Ÿญ ์ˆ˜ํ–‰
    • ์ฆ๊ฐ์‹ ์ˆ˜ํ–‰


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

let i = 0; // ์ „์—ญ๋ณ€์ˆ˜๋กœ for๋ฌธ ๋ฐ–์—์„œ ์„ ์–ธ

for (i = 0; i < 5; i++) {
  // i++์€ i += 2๊ฐ™์€ ์‹๋„ ๊ฐ€๋Šฅ
  console.log(i);
}

console.log("for๋ฌธ ๋ฐ–์œผ๋กœ ๋‚˜์™”์–ด์š” i๋Š”?", i); // ์ง€์—ญ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ ์‹œ ์ถœ๋ ฅ ์˜ค๋ฅ˜(for๋ฌธ ์•ˆ์—์„œ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—)
console.log();


// ์ค‘์ฒฉ
for (let a = 0; a < 5; a++) {
  //a๋Š” ์ง€์—ญ๋ณ€์ˆ˜.
  for (let j = 0; j < 5; j++) {
    // j๊ฐ€ 5๊ฐ€ ๋˜๋ฉด ํ•ด๋‹น for๋ฌธ์„ ๋๋‚ธ ํ›„ ๋‹ค์‹œ ์œ„์˜ for๋ฌธ์œผ๋กœ ์ด๋™ํ•˜์—ฌ a์˜ ๊ฐ’์ด ์ฆ๊ฐ€ํ•จ. (a๊ฐ€ 5๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต)
    console.log(`a๋Š” ${a}, j๋Š” ${j}`);
  }
}

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด



๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๐Ÿšฉ ๋ฐ˜๋ณต๋ฌธ ์ œ์–ด

break / continue

๐Ÿ“ ์„ค๋ช…

  • ๋ฐ˜๋ณต๋ฌธ์ด ๋ฌดํ•œ์œผ๋กœ ๋Œ์ง€ ์•Š๋„๋ก ํ•˜๊ฑฐ๋‚˜ ์›ํ•˜๋Š”๋Œ€๋กœ ์ž‘๋™๋˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ˜๋ณต ์ œ์–ด๋ฌธ.


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

for (let i=0;i<20;i++) {
  if (i==10){
    // continue; // ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ๋ฐ”๋กœ ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ
    console.log("i๊ฐ€ 10์ด ๋˜์—ˆ์–ด์š”");
    break; // "๋ฐ˜๋ณต๋ฌธ" ํƒˆ์ถœ(if๋ฌธ์ด ์•„๋‹ˆ๋ผ for๋ฌธ์—์„œ ๋น ์ ธ๋‚˜๊ฐ)
  }
  console.log(i);
}

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

continue ์‚ฌ์šฉ ์‹œ

break



๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€