The purpose of default: In the switch statement, the specified code block is executed when there is no matching case. Usage: 1. Syntax: switch (expression) { case constant 1: code block 1; break; ... default: code block n; break; } 2. The default code block is optional and is recommended to be included to handle unmatched Case. Example: When there is no matching case in the switch, the default code block will be executed, as in the following example that prints "D".
default usage in C language
#default keyword is used in the switch statement in C language. Used to specify the code block to be executed when there is no matching case in the switch.
Syntax
switch (expression) {
case constant 1:
<code>代码块1; break;</code>
case constant 2:
<code>代码块2; break;</code>
...
default:
<code>代码块 n; break;</code>
}
Usage
default code block is optional but recommended to be included in the switch statement It handles all unmatched cases. Without a default block, nothing will be done if there is no matching case in the switch.
Example
The following example demonstrates the use of the default keyword:
<code class="c">#include <stdio.h> int main() { int grade = 85; switch (grade) { case 90: printf("A"); break; case 80: printf("B"); break; case 70: printf("C"); break; default: printf("D"); break; } return 0; }</code>
In this example, when the value of the grade variable is 85, The switch statement has no matching case. Therefore, the default code block is executed, printing "D".
The above is the detailed content of How to use default in c language. For more information, please follow other related articles on the PHP Chinese website!