๐Ÿ”‚ Switch/case

๊น€์ฒ ์ค€ยท2022๋…„ 3์›” 5์ผ
0

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
8/16

Switch

switch/case ๋ฌธ์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ case ์กฐ๊ฑด ์ค‘ ๋งž๋Š” ์กฐ๊ฑด์„ ์ถฉ์กฑ์‹œํ‚ค๋Š” case๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฌธ์ด๋‹ค.


const value1 = 1
const value2 = 2
const value3 = 3

const expression = value2

switch(expression){
  case value1 :
   	console.log(`value is ${value1}`)
    break
  case value2 :
       	console.log(`value is ${value2}`)
    break
  case value3 :
       	console.log(`value is ${value3}`)
   break
  default : 
       	console.log(`value is ${expression}`)
}

switch์˜ ์ธ์ž ์•ˆ์— ๊ฐ’์„ ์ถฉ์กฑ์‹œํ‚ค๋Š” case ๊ตฌ๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

์œ„ ์˜ˆ์ œ์—์„œ switch ์ธ์ž๊ฐ’์ด value2๋ผ๋ฉด value2 statement(value is 2)๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

switch

switch(expression)
switch๋Š” ์ธ์ž์•ˆ์— ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.
ํ• ๋‹นํ•˜๋Š” ๊ฐ’์€ case ๊ฐ’๋“ค ์ค‘ ํ•˜๋‚˜์ด์ƒ์ด ๋  ์ˆ˜ ์žˆ๊ณ  case ๊ฐ’ ์ค‘ ์—†์–ด default ๊ฐ’์œผ๋กœ๋„ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค.

case


switch(expr){
	case1 value1:
    case2 value2:
    defalut:
}

case๋Š” switch์˜ ์ธ์ž๋ฅผ ์ถฉ์กฑ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด๊ฐ’์ด๋‹ค.

default

switch์˜ ์ธ์ž ์ค‘ ์ถฉ์กฑํ•˜๋Š” case๊ฐ€ ์—†๋‹ค๋ฉด default๊ฐ’์ด ์‹คํ–‰๋œ๋‹ค.

const value1 = 1
const value2 = 2
const value3 = 3

const expression = 5

switch(expression){
    
  default : 
       	console.log(`value is ${expression}`)    
    
  case value1 :
   		console.log(`value is ${value1}`)
  
  case value2 :
       	console.log(`value is ${value2}`)
    	break;
  case value3 :
       	console.log(`value is ${value3}`)
   

}
const value1 = 1
const value2 = 2
const value3 = 3

const expression = 5

switch(expression){
    

    
  case value1 :
   		console.log(`value is ${value1}`)
  
  case value2 :
       	console.log(`value is ${value2}`)
    	break;
    
 default : 
       	console.log(`value is ${expression}`)       
    
  case value3 :
       	console.log(`value is ${value3}`)
   

}

default ๊ตฌ๋ฌธ์€ ์–ด๋””์—๋‚˜ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

break

case์˜ break๊ฐ€ ์—†๋‹ค๋ฉด ๋‹ค์Œ๊ตฌ๋ฌธ๊นŒ์ง€ ์‹คํ–‰์ด ๋œ๋‹ค.


const value1 = 1
const value2 = 2
const value3 = 3

const expression = value1

switch(expression){
  case value1 :
   		console.log(`value is ${value1}`)
  
  case value2 :
       	console.log(`value is ${value2}`)
    
  case value3 :
       	console.log(`value is ${value3}`)
   
  default : 
       	console.log(`value is ${expression}`)
}

์œ„ ๊ฐ™์€ ๊ฒฝ์šฐ switch์˜ ์ธ์ž์— value1์„ ํ• ๋‹นํ•˜์˜€์„ ๋•Œ , case value1,2,3์€ break๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ตฌ๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.


const value1 = 1
const value2 = 2
const value3 = 3

const expression = value1

switch(expression){
  case value1 :
   		console.log(`value is ${value1}`)
  
  case value2 :
       	console.log(`value is ${value2}`)
    	break;
  case value3 :
       	console.log(`value is ${value3}`)
   
  default : 
       	console.log(`value is ${expression}`)
}

ํ•˜์ง€๋งŒ case ์ค‘ break๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น case๊นŒ์ง€ ์‹คํ–‰๋˜๊ณ  ๋ฌธ์ด ์ข…๋ฃŒ๊ฐ€ ๋œ๋‹ค.

์œ„ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” value1์„ ํ• ๋‹นํ•˜์˜€์„ ๋•Œ, case value2๊นŒ์ง€๋งŒ ์‹คํ–‰์ด ๋œ๋‹ค.

const value1 = 1
const value2 = 2
const value3 = 3

const expression = 5

switch(expression){
    
  default : 
       	console.log(`value is ${expression}`)    
    
  case value1 :
   		console.log(`value is ${value1}`)
  
  case value2 :
       	console.log(`value is ${value2}`)
    	break;
  case value3 :
       	console.log(`value is ${value3}`)
   

}

์œ„ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ์—์„œ ๋งŒ์•ฝ expression ๊ฐ’์„ case์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด default๊ตฌ๋ฌธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ case value2 ๊ตฌ๋ฌธ๊นŒ์ง€ ์‹คํ–‰์ด ๋œ๋‹ค.

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