﻿ More than 3 cases - C language to learn painstakingly
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 {
}
}

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 {
}

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.

Comment
COMMENT

Open the 💬 comment submission box