Two cases
Handling the false case.
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.
such as I wrote an example of
Actually, using the method described in the previous chapter...
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.
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.
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
If you have money to spare, go for the yakiniku set meal. If you're tight on cash, stick to plain udon.
such as I wrote an example of
Actually, using the method described in the previous chapter...
The Lunch Decision
If you have money to spare, eat. If you don't have money to spare, don't eat.
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.
If you run this program and input 10, the result will be as follows:
If you run this program and enter a number other than 10, the result will be as follows:
As the result shows, the second sentence is being executed when the condition is false.
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("ε
₯εε€γ―10It is.\n"); else printf("ε
₯εε€γ―10γ§γ―γγγΎγγγ\n");
return 0;
}
If you run this program and input 10, the result will be as follows:
Results
10γInput Data
ε ₯εε€γ―10It is.
ε ₯εε€γ―10It is.
If you run this program and enter a number other than 10, the result will be as follows:
Results
135γInput Data
ε ₯εε€γ―10γ§γ―γγγΎγγγ
ε ₯εε€γ―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("ε
₯εε€γ―10It is.\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("ε
₯εε€γ―10It is.\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("ε
₯εε€γ―10It is.\n");
}
else
{
printf("ε
₯εε€γ―10γ§γ―γγγΎγγγ\n");
}
return 0;
}
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.




