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

input check

posterior and antecedent judgment
The C language provides a total of three loop statements.
These are the while statement, the for statement, and the do (du)-while statement.

We have already discussed the while and for statements.
In fact, the do~while statement is almost identical to the while statement.
A do~while statement is generally written in the following manner

do to while statement
 do {
    Repeating statement;
} while (conditional expression);

The meaning and behavior of the conditional expression is exactly the same as the while statement.
That is, the execution of the repeating statement is repeated while the conditional expression is true.
One difference in the way it is written is that in the case of a do-to-while statement, the
The ; is required after the () in the conditional expression of the while statement.

The only difference between a do~while statement and a while statement is that a
The difference is whether the conditional expression is a post-determination or a predetermination.
The while statement determines the conditional expression before executing the repeating statement.
The do~while statement, however, determines the conditional expression after the repeating statement is executed.

Actually, this alone does not make much difference.
In fact, the same result can be obtained with either a while or a do~while statement.
For this reason, do~while statements are not often used.

However, the do~while statement has one major advantage.
That is, a repeating statement is always executed at least once.

In the case of a while statement, if the conditional expression is false from the beginning, then
The iterative statement would end up not being executed once.
With a do~while statement, it will be executed at least once.
input check
The advantage of the do~while statement, which is always executed once, is that
After all, it is a powerful tool for input checking.

For example, let's create a program to calculate the area of a circle.
The area of a circle is radius x radius x circumference.
Here, it is strange to say that the radius is a negative value, so we check it.

How many is pi
In the computer world, pi is calculated as 3.14159.
This means that the next digit is 3.141592, and the next digit is 3.141592.
This is because truncating between 9 and 2 does not result in a large error.

The next check is for when loops are not used.

do to while statement
 #include <stdio.h>

int main(void)
{

    int r;
    double s;

    printf("Radius? :");
    scanf("%d", &r);

    if (r < 0) {
        printf("Radius cannot be negative. \n");
    } else {
        s = r * r * 3.14;
        printf("Area is %f. \n", s);
    }

    return 0;
}

The result of running this program and entering 8 is as follows

Execution Result
Radius? :8 Input value
The area is 200.960000.

The result of running this program and entering -8 is as follows

Execution Result
Radius ? :-8 Value entered
Radius cannot be negative.

This is still a check, but it is a bit unfriendly.
If the input value is incorrect, let them re-enter it.
The following program is an example of implementing reentry with a while statement.

do to while statement
 #include <stdio.h>

int main(void)
{

    int r;
    double s;

    printf("Radius? :");
    scanf("%d", &r);

    while (r < 0) {
        printf("Radius? :");
        scanf("%d", &r);
    }

    s = r * r * 3.14;
    printf("Area is %f. \n", s);

    return 0;
}

The results of running this program and entering -8, -5, and 8 are as follows

Execution Result
Radius ? :-8 Value entered
Radius ? Value entered
Radius? :8 Value entered
The area is 200.960000.

The re-entry has been done beautifully.

This time, however, the problem lies on the program side.
If you look at the program, you will see that the scanf function is used twice.
Writing the scanf function twice for the same data input is just wasteful.
The following program is an example of eliminating this waste with a do~while statement.

do to while statement
 #include <stdio.h>

int main(void)
{

    int r;
    double s;

    do {
        printf("Radius? :");
        scanf("%d", &r);
    } while (r < 0);

    s = r * r * 3.14;
    printf("Area is %f. \n", s);

    return 0;
}

The results of running this program and entering -8, -5, and 8 are as follows

Execution Result
Radius ? :-8 Value entered
Radius ? Value entered
Radius? :8 Value entered
The area is 200.960000.

This time, the scanf function only needs to be written once.
The advantage of the do~while statement, which is always executed once, is taken advantage of.


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