Switch loop

sunghoon·2024년 12월 23일
0

2.0 Glove Project

목록 보기
8/40
post-thumbnail

2.13 Loops - Task

postfix/prefix forms

let i = 0; 
while(++i < 5) alert (i)
// prefix = first 1 last 4
while(i++ < 5) alert (i)
// postfix = first 1, last 5
for (let i = 0; i < 5; i++) alert(i);
for(let i = 0; i < 5; ++i;) alert(i);

/*no different
* for statement last execute step(++i, i++)
*/
for(i=1; i <= 10; i++){
	if(i % 2 == 0) {
		alert(i)
	}
}

Output prime numbers

link - find it prime number from under the typing number

The “switch” statement

The syntax

switch(x) {
	case 'value': // if (x === 'value1)
		...
		[break]
	
	case 'value2': // if (x === 'value2')
		...
		[break]
		
	default:
		...
		[break]
}

grouping of “case”

//want the same code to run for case 3 and case 5
case 3:
case 5:
	alert('Wrong!');
	alert("Why don't you take a math class?");
	break;

Type matter

case valus check strictly eqaul ===

let arg = prompt("Enter a value?");
switch (arg) {
  case 3:
    alert( 'Never executes!' );
    break;
}
/*the result of the prompt is a sting
* which is not strictly eqaul === to the number 3
* So It's dead code case 3
*/

sometimes don’t require break in last case but recomment to use it for code future-proof.

other

  • vs code many lines annotation shift + alt + A _link
profile
프라다 신은 빈지노와 쿠페를 타는 꿈을 꿨다.

0개의 댓글