Cases with three or more possibilities.
Handling multiple conditions
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.
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.
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.
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.
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.
| Category | Age | Admission fee |
|---|---|---|
| toddler | Ages 3 and under | Free |
| Child | Ages 4 to 12 | 250 Japan Yen |
| Adult | Age 13 and older | 400 Japan 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 honestly created using an if statement.
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("Child:250 Japan Yen\n");
if (age >= 13) printf("Adult:400 Japan Yen\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.
Source code
#include <stdio.h>
int main(void)
{
int age;
printf("Age:");
scanf("%d", &age);
if (age <= 3) {
printf("toddler:Free\n");
} else {
if (age <= 12) {
printf("Child:250 Japan Yen\n");
} else {
printf("Adult:400 Japan Yen\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.
Easy to read writing
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.
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.
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.
Source code
#include <stdio.h>
int main(void)
{
int age;
printf("Age:");
scanf("%d", &age);
if (age <= 3) {
printf("toddler:Free\n");
} else if (age <= 12) {
printf("Child:250 Japan Yen\n");
} else {
printf("Adult:400 Japan Yen\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.
Basically, it should be indented.
This is a special case where ignoring indentation improves readability.
In most cases, indenting to the right improves readability.
In most cases, indenting to the right improves readability.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




