C language learned by suffering
C language learned by suffering
Execution of multiple processes
Need for multiple processing
Suddenly, I would like to create a program to input test scores.
This in itself is easily accomplished with what we've been doing, but
This time, in addition, if you mistakenly enter a score greater than 100, the
The function to automatically correct the score to 100 and store it in memory will be added.
The program itself is not that difficult.
The following program is an example of realizing the process just described.
If you run this program and enter less than 100, the results will be as follows
If you let this program run and input greater than 100, the results will be as follows
Here's one more thing: if the number of points entered is greater than 100, the
Corrects "Input is greater than 100."
What if we want to add a feature to display a message saying
It does not work to connect a printf statement right after an existing if statement.
In an if statement, only the statement immediately following the if statement is used for the result of the decision.
A printf statement written immediately after an if statement will be executed each time as a normal statement.
One way is to use two if statements. As shown in the following example
Two if statements can be used to display the message.
This in itself is easily accomplished with what we've been doing, but
This time, in addition, if you mistakenly enter a score greater than 100, the
The function to automatically correct the score to 100 and store it in memory will be added.
The program itself is not that difficult.
The following program is an example of realizing the process just described.
source code
#include <stdio.h>
int main(void)
{
int score;
printf("Please enter score:");
scanf("%d", &score);
if (score > 100) score = 100;
printf("The score is %d. \n", score);
return 0;
}
If you run this program and enter less than 100, the results will be as follows
Execution Result
Please enter the score: 58 Data entered
The score is 58.
The score is 58.
If you let this program run and input greater than 100, the results will be as follows
Execution Result
Please enter the number of points:135 Data entered
The score is 100.
The score is 100.
Here's one more thing: if the number of points entered is greater than 100, the
Corrects "Input is greater than 100."
What if we want to add a feature to display a message saying
It does not work to connect a printf statement right after an existing if statement.
In an if statement, only the statement immediately following the if statement is used for the result of the decision.
A printf statement written immediately after an if statement will be executed each time as a normal statement.
One way is to use two if statements. As shown in the following example
Source Code
if (score > 100) printf("Input is greater than 100, modify. \n");
if (score > 100) score = 100;
Two if statements can be used to display the message.
block statement
If you do as in the previous section, you can have multiple statements executed for a single condition, but
To make multiple comparisons under the same conditions is, in all likelihood, a waste of time.
It would be much smarter if there was a way to execute multiple statements in a single if statement.
The C language has the ability to combine multiple statements into a single statement.
This function is called a block statement (double statement ).
This block statement allows multiple sentences to be placed where only one can be placed.
It is also customary to write sentences in block sentences with a slight shift to the right.
I explained indentation at the beginning, but if you have forgotten, please remember.
The following program uses block statements to add a message display function.
If you run this program and enter less than 100, the results will be as follows
If you let this program run and input greater than 100, the results will be as follows
To make multiple comparisons under the same conditions is, in all likelihood, a waste of time.
It would be much smarter if there was a way to execute multiple statements in a single if statement.
The C language has the ability to combine multiple statements into a single statement.
This function is called a block statement (double statement ).
Keywords.
[Block statement].
A method of grouping multiple sentences by enclosing them with {}.
This block statement allows multiple sentences to be placed where only one can be placed.
It is also customary to write sentences in block sentences with a slight shift to the right.
I explained indentation at the beginning, but if you have forgotten, please remember.
With a block statement, the result of an if statement can cause multiple processes to be executed.
The following program uses block statements to add a message display function.
Source Code
#include <stdio.h>
int main(void)
{
int score;
printf("Please enter score:");
scanf("%d", &score);
if (score > 100)
{
printf("Input is greater than 100, correct. \n");
score = 100;
}
printf("The score is %d. \n", score);
return 0;
}
If you run this program and enter less than 100, the results will be as follows
Execution Result
Please enter the number of points:58 Data entered
The score is 58.
The score is 58.
If you let this program run and input greater than 100, the results will be as follows
Execution Result
Please enter the number of points:135 Data entered
Correct because the input is greater than 100.
The score is 100.
Correct because the input is greater than 100.
The score is 100.
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 more complete than any book on the market.