We've only been able to handle single-choice or dual-choice options so far.
However, in reality, there are countless situations that require decisions involving more than just a choice of three options.
For example, let's say the relationship between zoo entrance fees and age is as follows.
区minute |
year齢 |
入場料 |
幼児 |
3歳以下 |
無料 |
子供 |
4~12歳 |
250円 |
大人 |
13歳以上 |
400円 |
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 honestly created using an if statement.
#include <stdio.h>
int main(void)
{
int age;
printf("year齢:");
scanf("%d", &age);
if (age <= 3) printf("幼児:無料\n");
if (age >= 4 && age <= 12) printf("子供:250円\n");
if (age >= 13) printf("大人:400円\n");
return 0;
}
The program above functions correctly.
However, I can definitively say that they are an adult, neither an infant nor a child.
Similarly, if not an infant, then one is either a child or an adult.
Nevertheless, we're checking age four times, which is a wasteful program.
You can use an `else` statement to handle cases that don't match conditions like "not a toddler" or "not an adult."
Therefore, using an else statement could potentially reduce unnecessary age checks.
However, until now, there has only been one way to handle cases where the conditions are not met.
However, this time there are two options for handling cases that don't match the condition: neither child nor adult.
If there are two ways to handle cases that don't meet the condition, you can write them as a
sequence of if-else statements.
In other words, using an if statement as the execution statement for an else clause...
You can leverage your previous decisions, eliminating waste.
The program, modified with this approach, is as follows.
#include <stdio.h>
int main(void)
{
int age;
printf("year齢:");
scanf("%d", &age);
if (age <= 3) {
printf("幼児:無料\n");
} else {
if (age <= 12) {
printf("子供:250円\n");
} else {
printf("大人:400円\n");
}
}
return 0;
}
The key point of this program is that it uses an if statement in the statement executed by the else clause.
First, the initial if statement checks if the person is a toddler.
If you were not an infant, we would determine if you are a child.
If I were not a child, I would certainly be an adult, and I would simply state it without judgment.
There are now only two age checks, significantly reducing wasted effort.
"While the previous program did reduce the wasted age checks,"
The second judgment being skewed to the right made the program not very readable.
As more conditions are added, it shifts further to the right due to indentation.
It will become even less legible.
The only way to make this readable is to ignore the indentation.
The following program is an example made more readable by ignoring indentation.
#include <stdio.h>
int main(void)
{
int age;
printf("year齢:");
scanf("%d", &age);
if (age <= 3) {
printf("幼児:無料\n");
} else if (age <= 12) {
printf("子供:250円\n");
} else {
printf("大人:400円\n");
}
return 0;
}
This program chains an if statement immediately after an else statement.
Therefore, this style of writing is sometimes referred to as an
else-if statement.
One of the benefits of this writing style is that it doesn't become wider even as the number of conditions increases.
"Adding if statements to else statements causes it to extend downwards like a chain."
It won't expand horizontally, so it will be relatively easy to read.
これは、indentを無視すると読みやすくなる特殊な例is.
ほとんどの場合にはindentで右にずらしたほうが読みやすくなります。