Does the last instruction of a switch need 'break'?

Asked

Viewed 237 times

3

I was watching some tutorials on Youtube and noticed that some programmers leave the last instruction of a block switch without the word break. For example:

switch(frutas){
   case "abacaxi": abacaxi++; break;
   case "morango": morango++; break;
   case "pera": pera++; // sem 'break' aqui...
}

I found it interesting, at first I thought it was just Programming style. I always use break even in the last instruction, but after seeing the videos I realized that there is no need since she is the last of the block. As far as I know, the break serves (correct me if I’m wrong) just to interrupt an instruction.

I would like if this practice can lead to any problem.

  • 4

    If you decide to enter one more case at the end and forget to put the break in the previous, can rather give problem.

1 answer

5


Directly no problem except confusion during maintenance as the bfavaretto put in his comment.

I don’t have much to say about, except that it is stylistic encoding option. By the way, pretty bad if you decide to follow these people.

The problem with these tutorials and information posted where no due importance is given to the quality of the content, as happens a lot on the Internet and on many occasions right here, is that in trying to help people with the best intentions, people who make and defend information without a broad context are providing a disservice in training programmers who seek to benefit from this information.

What you can do is not the same as what you should do. Some programmers only care whether the program works or not. Others worry whether it is right or not, whether it will continue working or not, whether it will give maintenance facilities or not.

Keep doing the right thing you’ve always done, even if it works the new way you’ve learned. And whenever you have any questions, ask in a trusted location. If all goes well, if I said something stupid, someone will post something better or will fix me. It makes the process more reliable.

Just to be more precise break interrupts an instruction pad.

Browser other questions tagged

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