In the previous chapter, we explained how to control the execution of specific processes based on conditions using if statements.
However, this method only allowed for the choice of whether or not to execute the process.
At the beginning of the previous chapter.
The Lunch Decision
おGoldに余裕がある ならば 焼肉定食 おGoldに余裕がない ならば 素うどん
such as I wrote an example of
Actually, using the method described in the previous chapter...
The Lunch Decision
おGoldに余裕がある ならば 食べる おGoldに余裕がない ならば 食べない
"It was a situation where one could only make the judgment of 'do' or 'don't do'."
"If you want to execute a statement not only when a condition is met, but also when it's not."
You can use an `else` statement with an `if` statement.
The else statement is used as follows.
else statement
if (condition) statement_if_true; else statement_if_false;
The statement following the `else` statement is executed when the condition is false.
It is impossible for both a statement to be executed when true and a statement to be executed when false.
Depending on the conditions, only one of them will execute.
The usage is the same.
Even when using an else statement, the way you use an if statement remains exactly the same.
It was simply the case that statements to be executed when false were added as options.
The following program is an example of rewriting the program from the previous chapter using an else statement.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10) printf("入力値は10is.\n"); else printf("入力値は10ではありません。\n");
return 0;
}
If you run this program and enter 10 as input, the result will be as follows:
Execution results
10 入力したデータ 入力値は10is.
"If you run this program and enter a number other than 10, the result will be as follows:"
Execution results
135 入力したデータ 入力値は10ではありません。
As the result shows, the second sentence is being executed when the condition is false.
Make it readable.
As explained in the previous section, you can execute code for cases that do not match the condition by using an else statement.
However, as can be seen from the program in the previous section, an else statement was added to the end,
The lines have become excessively long, making the program difficult to read.
To write if-else statements for readability, break them down into multiple lines and add indentation.
Specifically, formatting your text in the following ways will improve readability.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10)
printf("入力値は10is.\n");
else
printf("入力値は10ではありません。\n");
return 0;
}
Furthermore, you can use blocks (compound statements) in if-else statements by writing in the following manner.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10) {
printf("入力値は10is.\n");
} else {
printf("入力値は10ではありません。\n");
}
return 0;
}
Using this formatting makes the correspondence between if statements and else statements immediately clear.
We recommend using this format regularly as it makes the information much easier to read.
In addition, it is also widely used to format `else` statements with line breaks as follows.
The author prefers the former because it's shorter vertically, but either way of writing is fine.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10)
{
printf("入力値は10is.\n");
}
else
{
printf("入力値は10ではありません。\n");
}
return 0;
}
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.