learn through suffering C language learn through suffering 
C language

Execution of multiple processes

The need for multiple processes

I'd like to try making a program to input test scores, suddenly.
This itself can be easily achieved with what we've been doing.
This time, additionally, if a score greater than 100 was mistakenly entered,
Let's try adding a feature that automatically corrects and remembers it as 100 points.

This program itself isn't that difficult.
The following program is an example of how to achieve the processing described above.

Source code
#include <stdio.h>

int main(void)
{
    int score;
    printf("Please enter the score.:");
    scanf("%d", &score);

    if (score > 100) score = 100;

    printf("Your score is %d points.\n", score);

    return 0;
}

If you run this program and enter a number less than or equal to 100, the result will be as follows:

Results
Please enter your score: 58 Entered data
Your score is 58 points.

If you run this program with an input greater than 100, the result will be as follows:

Results
Please enter your score: 135 Entered data
The score is 100 points.

Here, another one, if the entered score is greater than 100,
Adjusting the input because it's greater than 100.」
How would I add functionality to display a message like that?

It doesn't work to simply concatenate a printf statement immediately after an existing if statement.
In an if statement, only the statement immediately following the if condition is used for the judgment result.
The printf statement immediately following an if statement will be executed as a regular statement every time.

One approach is to use two if statements.As follows:

Source code
if (score > 100) printf("The input is greater than 100, so I will correct it.\n");
if (score > 100) score = 100;

You can display a message by using two if statements.

block text

Similar to the previous section, this allows us to execute multiple statements for a single condition,
Repeating comparisons under the same conditions is simply wasteful.
It would be much smarter if there were a way to execute multiple statements with a single if statement.

C has a feature that allows you to group multiple statements together.
That is what's called a block statement (complex sentence) feature.

Keyword
【block text】

A way to group multiple sentences using {}.

This block allows you to place multiple sentences in a space that typically only allows for one.
In addition, it is customary to indent the sentences within a block quotation.
I started by explaining indentation, but for those who forgot, please remember it.
Using block statements, you can execute multiple processes based on the result of an if statement.
The following program adds a message display function using block statements.

Source code
#include <stdio.h>

int main(void)
{
    int score;
    printf("Please enter the score.:");
    scanf("%d", &score);

    if (score > 100)
    {
        printf("The input is greater than 100, so I will correct it.\n");
        score = 100;
    }
    
    printf("Your score is %d points.\n", score);
    return 0;
}

If you run this program and enter a number less than or equal to 100, the result will be as follows:

Results
Please enter your score: 58 Entered data
Your score is 58 points.

If you run this program with an input greater than 100, the result will be as follows:

Results
Please enter your score: 135 Entered data
Your input is greater than 100, so it will be corrected.
The score is 100 points.



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.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...