MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Case Classification by Number

Number and corresponding process
With the methods we have described, it is no longer possible to determine any condition.
However, there is one more useful decision statement provided in the C language that you should be aware of.

In our surroundings, numbering is often used.
As one example, consider a school class attendance number list.
Suppose that the attendance number list for a certain class in a certain school is as follows.

Number Name Gender
1 Nobi Nobita Male
2 Shizuka Minamoto Female
3 Takeshi Gouda Male
4 Suneo Konekawa Male

The above is just an example for clarity, and differs from the setting in the work.

Based on the above, let's create a program that displays the corresponding name when the attendance number is entered.
This can be easily accomplished with the aforementioned else-if statement.

source code
 #include <stdio.h>

int main(void)
{
    int no;
    scanf("%d", &no);

    if (no == 1) {
        printf("Nobi Nobita\n");
    } else if (no == 2) {
        printf("Minamoto Shizuka\n");
    } else if (no == 3) {
        printf("剛田武\n");
    } else if (no == 4) {
        printf("骨川スネ夫\n");
    } else {
        printf("No one has such a number\n");
    }

    return 0;
}


The above program works properly.
However, there are so many else-if statements that the program is a bit cumbersome to type.

A simpler way to write the above is available only when determining a match with more than one number.
The switch statement - case can be used to write more beautifully.
The usage of the switch statement ~case is as follows.

How to use the switch statement ~case
 switch (conditional expression) {
case Numeric:
    Execution statement;
    break;
case number:
    Execution statement;
    break;
}

The switch statement jumps the order of processing to the case with the same value as the value of the specified conditional expression.
At the jump destination, it executes the statements below the case, and when it finds the break statement
Get out of the enclosed {} after the switch statement.
The following program is an example of a rewritten program using the switch statement ~case.

source code
 #include <stdio.h>

int main(void)
{
    int no;
    scanf("%d", &no);

    switch (no) {
    case 1:
        printf("NOBI NOBITA\n");
        break;
    case 2:
        printf("Minamoto Shizuka\n");
        break;
    case 3:
        printf("剛田武\n"); break; case 3: printf("剛田武\n");
        break;
    case 4:
        printf("骨川スネ夫\n");
        break;
    }

    return 0;
}

The correspondence with the number is clearer and more beautiful than before.
Of course, when you enter a number, the corresponding name is displayed.
Note that, unlike an if statement, multiple statements may be arranged without {}.

Programmers are artists.
We have used words such as beautifully, etc., since a few minutes ago.
In fact, the word is used surprisingly commonly among programmers.
A programmer's job is a puzzle of combining simple calculations into complex ones.
In some aspects, it is almost an art form.

What to do if none of the above apply?
In fact, if you run the switch to case statement program described in the previous section, you will see that
The result may be different than when created with the else-if statement.

With the else-if statement, if you specify a number that is not in the roster, the
It would have displayed the error as, "No one has that number."
The switch statement to case program does not display anything.

Thus, to make the process run when the other case values do not apply, you can use the
default ( default) can be used.
Default can be used in place of a case statement.
Jump to default if there is no number that fits in the other cases.

The following program adds the default

source code
 #include <stdio.h>

int main(void)
{
    int no;
    scanf("%d", &no);

    switch (no) {
    case 1:
        printf("NOBI NOBITA\n");
        break;
    case 2:
        printf("Minamoto Shizuka\n");
        break;
    case 3:
        printf("剛田武\n"); break; case 3: printf("剛田武\n");
        break;
    case 4:
        printf("骨川スネ夫\n");
        break;
    default:
        printf("No one with such number\n");
        break;
    }

    return 0;
}

If you run this program and enter 10, the result will be as follows

Execution Result
10 Input data
No one has that number.

If numbers 1-4 are entered, the corresponding names will be displayed correctly.
Putting together a similar process
Next, let's consider a program that displays gender using the previous roster.
This is very easy because all you have to do is rewrite the string.
The following program was created by rewriting the string.

source code
 #include <stdio.h>

int main(void)
{
    int no;
    scanf("%d", &no);

    switch (no) {
    case 1:
        printf("male\n");
        break;
    case 2:
        printf("女性\n"); break; case 2: printf("女性\n");
        break;
    case 3:
        printf("男性\n"); break; case 3: printf("男性\n");
        break;
    case 4:
        printf("男性\n"); break; case 4: printf("男性\n");
        break;
    default:
        printf("No one with such number\n");
        break;
    }

    return 0;
}

It's going to be another familiar way to consider this program.
The string "male" is used three times, which is very wasteful.
Can this somehow be done only once?

In fact, CASE can be used with more than one in succession.
The case itself has no meaning other than to indicate where to jump in a switch statement.
Even if multiple cases are connected, there is no effect on the content of execution.
The following program is a combination of cases 1, 3, and 4.

source code
 #include <stdio.h>

int main(void)
{
    int no;
    scanf("%d", &no);

    switch (no) {
    case 1:
    case 3:
    case 4:
        printf("male\n");
        break;
    case 2:
        printf("女性\n"); break; case 2: printf("女性\n");
        break;
    default:
        printf("No one with such number\n");
        break;
    }

    return 0;
}

In this program, if 1, 3, or 4 is entered, the
Both display "male".

Be careful with the break statement.
As shown in this example, cases can be connected without a break statement.
Conversely, forgetting the break statement will cause unrelated cases to be connected.
There is an actual example of a well-known application that caused a serious bug by forgetting the break statement.
Be very careful with the break statement.

Weak judgment
As you can see from what we have seen so far, using the switch statement ~case
It is easy to write programs that branch in multiple directions.
In fact, however, the switch statement ~case is very weak in judgment.

In the switch statement ~case, only integer values can be placed in the case
Real numbers, variables, and conditional expressions are not allowed.
In other words, it is not possible to compare variables or to compare large and small relationships, as is the case with if statements.

The switch statement ~case can only be used to compare variables and integer values.
When complex decisions are required, there is no other way but to use if statements.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box