Home Common Problem Is the default option required in the switch statement?

Is the default option required in the switch statement?

Nov 25, 2020 pm 04:03 PM
switch default option

The default option is not required in the switch statement. The switch statement will first look for the case value that meets the conditions as the program entry after execution. If all cases are not satisfied, it will look for the default entry. If not found, it will exit the entire statement; default is just a backup entry, and it does not matter whether it is present or not.

Is the default option required in the switch statement?

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

Switch is a reserved word in some computer languages, and its function is to make judgments and selections in most cases; it is often used together with case, break, and default.

The switch statement can be used to perform different actions based on different conditions.

The switch statement is a conditional selection statement. First, the case value that meets the condition will be found as the program entry after execution; if all cases are not satisfied, the default entry will be found. If not found, the entire switch will be exited. statement; so default is just a backup entry, it doesn't matter whether it is present or not.

But for error checking or logic checking, the default branch should still be added to the switch statement.

For example, the following switch statement is completely legal:

switch (char_code)
{
      case tyt:
      case 'y': printf ( " You answered YES ! \n" )
           break
      case 'N':
      case 'n': printf ("You answered NO!\n");
           break
}
Copy after login

But what happens if an unknown character is passed to this switch statement? At this time, the program will have no output . Therefore, it is best to add a default branch to handle this situation:

......
default: printf ("Unknown response : %d\n", char_code);
          break
......
Copy after login

In addition, the default branch can bring a lot of convenience to logic checking. For example, if you use a switch statement to process a fixed number of conditions, and it is believed that values ​​outside these conditions are logical errors, you can add a default branch to identify logical errors.

Please see the following:

void move_cursor (int direction)
{
     switch (direction)
     {
           case UP:     cursor_up()
                            break
           case DOWN:   cursor_down()
                            break
           case LEFT:   cursor_left ()
                            break
           case RIGHT:  cursor_ right ( )
                             break
           default:     printf ("Logic error on line number %ld!!! \n",
                                     __ LINE__ )
                             break
     }
}
Copy after login

For more programming-related knowledge, please visit: Programming Learning Website! !

The above is the detailed content of Is the default option required in the switch statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between the Japanese and Hong Kong versions of switch? What are the differences between the Japanese and Hong Kong versions of switch? Jun 20, 2023 pm 02:06 PM

What are the differences between the Japanese and Hong Kong versions of switch?

What should I do if the switch does not respond to the TV? What should I do if the switch does not respond to the TV? Jul 03, 2023 am 11:15 AM

What should I do if the switch does not respond to the TV?

Can the switch be charged on the base all the time? Can the switch be charged on the base all the time? Jul 06, 2023 pm 04:51 PM

Can the switch be charged on the base all the time?

Can Elden's Ring be played on switch? Can Elden's Ring be played on switch? Mar 11, 2024 am 11:31 AM

Can Elden's Ring be played on switch?

What is the difference between switch lite and switch What is the difference between switch lite and switch Jun 28, 2023 pm 02:13 PM

What is the difference between switch lite and switch

Is the default option required in the switch statement? Is the default option required in the switch statement? Nov 25, 2020 pm 04:03 PM

Is the default option required in the switch statement?

Does switch32g have enough memory? Does switch32g have enough memory? Jun 20, 2023 pm 02:28 PM

Does switch32g have enough memory?

It is reported that Sega has a Nintendo Switch 2 development kit, and 'FF7: R' looks like a PS5 game when running on the Switch 2 It is reported that Sega has a Nintendo Switch 2 development kit, and 'FF7: R' looks like a PS5 game when running on the Switch 2 Sep 10, 2023 pm 05:33 PM

It is reported that Sega has a Nintendo Switch 2 development kit, and 'FF7: R' looks like a PS5 game when running on the Switch 2