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

Three or more case divisions

Handling of multiple conditions
With the methods we have learned so far, we can only process one or two choices.
In reality, however, there are plenty of times when more than three decisions are required.

For example, suppose that the relationship between admission fee and age at a particular zoo is as follows

Classification Age Admission Fee
Infant Under 3 years old Free of charge
Child 4-12 years old 250 yen
Adult 13 years old and up 400 yen

Based on the above, let's create a program that displays the required admission fee when you enter your age.
The following program is an example of an honestly created program using if statements.

Source code
 #include <stdio.h>

int main(void)
{
    int age;
    printf("Age:");
    scanf("%d", &age);

    if (age <= 3) printf("toddler:free\n");
    if (age >= 4 && age <= 12) printf("Children:¥250\n")
    if (age >= 13) printf("adult:400yen\n");

    return 0;
}

The above program works properly.

However, I can assure you that if you are not an infant or a child, you are certainly an adult.
Likewise, if you are not an infant, you are either a child or an adult.
Nevertheless, the program checks age four times, which is wasteful.

The else statement can be used to handle cases where the condition is not met, such as not an infant or not an adult.
Thus, the use of the else statement is likely to reduce unnecessary age checks.
Until now, however, there has been only one process for when a condition is not matched.
This time, however, there are two processes for when the condition is not matched: not an infant and not an adult.

If there are two cases of processing when a condition is not matched, you can write a series of if~else statements.
In other words, by using an if statement as the executable statement of an else statement
The previous decision can be utilized, and waste is eliminated.
The program modified with this approach is as follows.

Source Code
 #include <stdio.h>

int main(void)
{
    int age;
    printf("Age:");
    scanf("%d", &age);

    if (age <= 3) {
        printf("Infant:free\n");
    } else {
        if (age <= 12) {
            printf("Child:250yen\n");
        } else {
            printf("Adult:400yen\n");
        }
    }

    return 0;
}

The point of this program is that it uses an if statement for the statement to be executed in the else statement.

First, the beginning if statement determines whether the child is an infant or not.
If the child is not an infant, the next step is to determine if the child is a child.
If it is not a child, then it is definitely an adult and will be displayed without judgment.
The age is checked only twice, greatly reducing waste.
Easy-to-read writing style
The program in the previous paragraph certainly reduced the number of wasted age checks, but
The program was not very readable because the second decision was shifted to the right.
As more conditions are added, the indentation shifts them further to the right, so that the
It becomes even more difficult to see.

The only way to make this an easy-to-read sentence is to ignore indentation.
The following program is an example of how indentation can be ignored to make it easier to read.

source code
 #include <stdio.h>

int main(void)
{
    int age;
    printf("Age:");
    scanf("%d", &age);

    if (age <= 3) {
        printf("Infant:free\n");
    } else if (age <= 12) {
        printf("Child:250yen\n");
    } else {
        printf("Adult:400yen\n");
    }

    return 0;
}

In this program, the if statement is connected immediately after the else statement.
For this reason, this writing style is sometimes colloquially called an else-if statement.

The good thing about this writing style is that it is not horizontally long, no matter how many conditions are added.
By adding more if's to the else statement, it grows lower and lower, but
Because it never extends horizontally, the program is relatively easy to read.

Basically, it should be indented.
This is a special case where ignoring indentation makes for easier reading.
In most cases, it is easier to read if the indentation is shifted to the right.



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