Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a 'for' loop
and inside of him an 'switch' loop.
but the switch loop executes just ONCE.
why? and how can i make it executable as many time as i want to?

example:

for(let i = 0; i < 5; i++){

switch(something[i]){
case 1:
alert("case 1 has been activated!");
break;
case 2:
alert("case 2 has been activated!");
break;
}

What I have tried:

look for similar articles problems.
Posted
Updated 23-Mar-19 8:28am

A switch statement is not a loop. Moreover, if what is at something[i] is not 1 or 2, nothing will be excuted and will silently pass.
You will have to put a breakpoint on the for line and debug line by line to see what is going on.
You may want to add a default case to account for unanticipated values:
C#
switch (something[i]) {
   // ...
   default:
      alert("unexpected value: " + something[i]);
      break;
}
 
Share this answer
 
Comments
CPallini 23-Mar-19 11:57am    
5.
phil.o 23-Mar-19 12:42pm    
Thank you, Carlo :)
Quote:
but the switch loop executes just ONCE.

Probably because a switch is not a loop. You need to learn the language structures, we can't fo it for you.
JavaScript Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900