learn through suffering C language learn through suffering 
C language

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.

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.

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.



About This Site

Learning C language through suffering (Kushi C) is
This 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.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...