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

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.

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.

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.

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 ).

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.

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.



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.

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