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
I wrote an example of this.
In fact, the method described in the previous chapter
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
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.
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
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
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.
If you run this program and enter 10, the result will be as follows
If you run this program and enter anything other than 10, the results will be as follows
As you can see from the results, the second statement is executed if false.
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.
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.
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.