Is it OK to use a switch inside a for?

Asked

Viewed 253 times

4

Came to me a question related to the use of a switch within a for. I saw that it works for my purpose but this is correct to do in the world of programming?

There is a better way to get the result without using a switch within the for, from the code below?

    for (var index = 1; index <= 10; index++) {
    switch (index) {
        case 1:
            console.log(`Repeating because one is less than ten !`)
            break;
        case 2:
            console.log(`Repeating because two is less than ten !`)
            break;
        case 3:
        console.log(`Repeating because three is less than ten !`)
            break;
        default:
            console.log(`Repeating because ${index} is less than ten !`)
            break;
    }

}

2 answers

3

Correct is not always easy to say, strictly speaking it is correct because it works and gives the result you want in all situations of this stretch, so no one can say you are wrong. Not the most efficient, shortest, simplest, and even readable way to do.

The switch there is completely unnecessary and putting unnecessary code can be considered wrong by some. I usually choose the one that has better performance whenever the readability is not affected, in this case the performance improves the performance, so I consider it just more correct not to use it.

In other situations the use of switch or other form of selection between options.

I would do differently since the options of caseare invariant and only the default is that it varies in each step:

console.log(`Repeating because one is less than ten !`);
console.log(`Repeating because two is less than ten !`);
console.log(`Repeating because three is less than ten !`);
for (var i = 4; i <= 10; i++) console.log(`Repeating because ${i} is less than ten !`);

I put in the Github for future reference.

  • Two negatives why??? The code refactored of the AP is exactly this!!!

  • I know who, I don’t need to say that they weren’t due to error or something like that. Each had a reason.

  • In this case, when it would be advisable to use a Switch instead of an IF, ELSE and ELSE, for example ?

  • 2

    @Gatodeschrödinger this has already been answered in https://answall.com/q/58192/101 and supplements for specific questions and applies only to some languages: https://answall.com/q/190736/101 and https://answall.com/q/82667/101 and https://en.stackoverflow.com/q/346695/101 and https://answall.com/q/258024/101 and https://answall.com/q/139313/101 and https://answall.com/q/139313/101 and https://pt.stackoverflowcom/q/211841/101 and http://answall.com/q/176675/101 and https://en.stackoverflow.com/q/240410/101.

3


There’s nothing wrong with wearing one switch within a for.

For his example, he wants to make a conditional validation, if it were not a switch, would need for example to use a if, and in that case the switch is much better.

Must be some exercise and probably want to show the description of the value, but see that can better a little... see that part of the message is always the same, so can do the switch only to set the value text:

for (var index = 1; index <= 10; index++) {
    var valor = `${index}`; 
    switch (index) {
        case 1:
            valor = "one";
            break;
        case 2:
            valor = "two";
            break;
        case 3:
            valor = "three";
            break;
    }
    
    console.log(`Repeating because ${valor} is less than ten !`)
}

Note that there is no absolute "right" or "wrong", there are better or worse ways to write the code, and good practices, and here is another suggestion to give you more ideas on how to solve.

  • Ricado, that’s exactly it! This is an exercise of a course I’m doing where I wanted to "mix things up" and I thought if it would be right to do this. The way you answered the code ends up adapting if in the future I change fixed from 10 to a variable that the user comes to type, for example. Thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.