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

Two case classifications

False case handling
The previous chapter explained how to use if statements to control the execution of certain processes according to conditions.
However, this method only gave us the choice of whether or not to execute the process.

At the beginning of the previous chapter

Lunch decisions
If you have money to spare, then have the yakiniku set meal.
If you can't afford it, then plain udon

I wrote an example of this.

In fact, the method described in the previous chapter

Lunch Decision
If you can afford it, eat
If you can't afford it, don't eat

The only decision we could make was whether or not to do so.

If you want the statement to be executed not only when the condition is matched, but also when the condition is not matched, you can use the
The else statement can be used in conjunction with the if statement.
The else statement is used as follows

else statement
if (conditional expression) statement to execute if true; else statement to execute if false ;

The statement following the else statement is executed if the condition is false.
It is not possible for a statement to execute both a statement that executes if true and a statement that executes if false.
Invariably, only one of the two will be executed, depending on the condition.
Usage is the same
The use of the if statement itself is exactly the same, even when the else statement is attached.
The only difference is that the statement to be executed if false is added as an option.
The following program is an example of the program created in the previous section rewritten with the "else" statement.

source code
 #include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);

    if (suuti == 10) printf("Input value is 10. \n"); else printf("Input value is not 10. \n");

    return 0;
}

If you run this program and enter 10, the result will be as follows

Execution Result
10 Data input
Input value is 10.

If you run this program and enter anything other than 10, the results will be as follows

Execution Result
135 Data input
Input value is not 10.

As you can see from the results, the second statement is executed if false.
make it easy to read

As explained in the previous section, the else statement can be used to perform processing if the condition is not met.
However, as you can see from the program in the previous section, the addition of the else statement after the
One line is quite long horizontally, making the program difficult to read.

To write if~else statements in a readable manner, divide them into multiple lines and add indentation.
Specifically, the following writing style will make it easier to read.

Source Code
 #include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);

    if (suuti == 10)
        printf("Input value is 10. \n");
    else
        printf("Input value is not 10. \n");

    return 0;
}

You can also use blocks (compound statements) in if~else statements by writing them in the following way.

source code
 #include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);

    if (suuti == 10) {
        printf("Input value is 10. \n");
    } else {
        printf("Input value is not 10. \n");
    }

    return 0;
}


Using this writing style, the correspondence between if and else statements can be seen at a glance.
It is recommended that this writing style be used on a regular basis because it is very easy to read.

The following style of writing, in which a new line is inserted in the else statement, is also widely used.
The author prefers the former because it is shorter vertically, but either writing style is fine.

source code
 #include <stdio.h>

int main(void)
{
    int suuti;
    scanf("%d", &suuti);

    if (suuti == 10)
    {
        printf("Input value is 10. \n");
    }
    else
    {
        printf("Input value is not 10. \n");
    }

    return 0;
}



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