How to use default in c language

下次还敢
Release: 2024-05-02 15:18:16
Original
828 people have browsed it

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".

How to use default in c language

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>
Copy after login

case constant 2:

<code>代码块2;
break;</code>
Copy after login

...
default:

<code>代码块 n;
break;</code>
Copy after login

}

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>
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!