The switch statement in the C++

The switch statement in the C++
5 (100%) 2 votes

оператор switch, switch c++, switch в с++, оператор множественного выбора, свич с++, основы программирования

In this article, we slightly modify the format of the study topics. At the beginning, let's look at an example with statement switch (switch), which will be a lot of comments about how, that occurs in the code. A whole theory will be located below. The work of the statement of multiple choice switch in C ++, we consider in addressing this problem: Let's, the user has a numbered list of the metro stations in Barcelona. You must write code, wherein the dialogue with the user is implemented, namely, asked to enter room underground station. After entering the Station Number, we have to show it on the screen and the name of the travel time. If, however, with the number of stations is not, inform and propose to enter the number again. Begin:

To string 13 – everything is clear: Ads necessary variables, conclusions Questions on the screen, and entry into the loop do while. In string 14 we got value answer, which introduces the user. Then look, how interesting works switch() : in parentheses pass him answer (string 16) and next to each word case write possible values answer, the user can enter (1, 2, 3 …). after each case you see the statement : , he required. It is followed by the command, that must be met, if the value case coincides with the value of answer. Statement break at the end of each block case It is also required. It indicates to the compiler, if this case It was performed, you have to get out of switch(). That is actually, when performed switch () begins search and search for the desired case. If answer equal to 1, work case 1 and the screen will show the, that it is registered to a keyword break, and all other case, They will be ignored. If answer equal to 2 – executed only case 2 and exited switch(). What if no value blocks case does not match, with the value, that took switch() (in our case answer)? For this purpose the block default. Before him it comes, just then, when none of case not worked and, respectively, not fulfilled none break these units. In our example,, it will work if the value, which will bring the user, will be <1 or >5. Then the user will see on the screen a message that, that with such a number of subway stations, and there is no need to re-enter.

In the loop, we always check the value answer, which is entered by the user. This is done using if . If the value is within a suitable range, we (from 1 to 5 ), the manipulated variable var It will be set 0. In this case, the user will no longer be prompted to enter a new value.

Here's how the program works:

оператор switch, switch c++, switch в с++, оператор множественного выбора, свич с++

What you should remember:

  • syntax:

оператор switch, switch c++, switch в с++, оператор множественного выбора, свич с++

  • block default It can be positioned anywhere in the block switch() For example the first and second case . His code in any event will run only, if you find the value in the blocks case. But, I suggest you have it at the end, as do most programmers. It is an unwritten rule.
  • a lower block switch(), whether default or case, operator break You can not specify. This is done in this example. But we should remember, that in all other break mandatory!
  • default It is optional. His in switch() may not be at all. In this case, If no value blocks case does not coincide with the, that took switch() , the program moves to the next line of code, located under the switch()
  • sometimes blocks case check character, instead of numerical values. Then you must take these characters in single quotes – case ‘b’: , case ‘G’: , case ‘+’ etc.

Anyone who has a desire to – see video tutorial on the topic. In addition to the multiple choice statement switch (17--s minutes) it considered conditional statements if and else, and ternary statement ?: (15--s minutes)

Still have questions – ask them in the comments. Be sure to answer all. Learn the basics of programming with us! More examples using switch in C ++ discussed in the article Tasks: The switch statement in the C++.

Newsletter of programming:

22 thoughts on “The switch statement in the C++

  1. Fyodor, I did not write this article … and not even read properly. But it said that, that the break in the trees of choice practically almost always necessary, the initial stages of the study is very helpful.

    They, Fyodor, who wrote many thousands of industrial code by profession (like me), in distinction from degeneratov, who write only comments in the forums (like you) know, that skipping break in a switch where it is needed – the most difficult cause of errors, which are only many hours of debugging.

    1. 1. "Chukchi not the reader - Chukchi writer!»
      2. “and not even read properly”
      I have nothing to add to my previous post.

    2. That is all, who by profession is not super steep cheater with mathematical megaintellektom and experience in developing a code of rolls – degenerate?
      Then you – sidedly developed bot with the claim.

      How noviček, I personally understand, I'm not in Cambridge or okforde, what would I have been taught “ideally” and very grateful to Fedor for his work!

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>