MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Input validation

Post-assessment and pre-assessment
C has three loop statements in total.
"It's about while loops, for loops, and do-while loops."

While and for loops have already been explained.
Actually, do-while loops are almost identical to while loops.
"do-while statements are generally used in the following format:"

do-while loop
do {
    繰り返す文;
} while (条件式);

The meaning and behavior of a conditional statement are exactly the same as those of a while statement.
In other words, it repeats the execution of the loop statement as long as the condition is true.
Furthermore, as a difference in notation, in the case of do-while statements,
"A semicolon ; is required after the condition expression in a while statement."

The only difference between a do-while loop and a while loop is
The difference lies in whether the conditional statement is evaluated after the fact or in advance.
A while loop evaluates a condition before executing the statements within the loop.
However, the do-while loop evaluates the condition *after* the block of code it repeats has been executed.

Actually, it doesn't make that much of a difference.
In fact, a while loop and a do-while loop can sometimes produce the same result.
Therefore, do-while loops are not frequently used.

However, do-while loops have one significant advantage.
That's because a loop is guaranteed to execute at least once.

In the case of a while loop, if the condition is initially false,
It will end without the loop ever being executed.
With a do-while loop, the code block will always execute at least once.
Input validation
The advantage of a do-while loop is that it guarantees at least one execution.
Above all, it really shines when it comes to input validation.

For example, let's try creating a program to calculate the area of a circle.
The area of a circle is radius times radius times pi.
"Here, we're checking that the radius isn't a negative value, as that wouldn't make sense."

What is pi?
コンピュータの世界では、円周率を3.14159で計算します。
これは、その次の桁まで表すと、3.141592、となり、
9と2の間で切り捨てても、誤差が大きくならないからis.

Next, let's check the case without using loops.

do-while loop
#include <stdio.h>

int main(void)
{

    int r;
    double s;

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

    if (r < 0) {
        printf("半径は負の値になりません。\n");
    } else {
        s = r * r * 3.14;
        printf("面積は %f is.\n", s);
    }

    return 0;
}

"When running this program with an input of 8, the result is as follows:"

Execution results
Radius?:8 入力した値
The area is 200.960000.

"The result of running this program with the input -8 is as follows."

Execution results
Radius?:-8 入力した値
半径は負の値になりません。

While this technically checks out, it's a bit unfriendly.
If the input is incorrect, let's have them re-enter it.
The following program is an example of re-entrancy implemented with a while loop.

do-while loop
#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("面積は %f is.\n", s);

    return 0;
}

“When running this program with the inputs -8, -5, and 8, the result is as follows.”

Execution results
Radius?:-8 入力した値
Radius?:-5 入力した値
Radius?:8 入力した値
The area is 200.960000.

The retyping was flawlessly done.

However, the problem lies with the program this time.
Looking at the program, I can see that the scanf function is being used twice.
Writing the `scanf` function twice for the same data input is truly wasteful.
The following program is an example of eliminating this waste with a do-while loop.

do-while loop
#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("面積は %f is.\n", s);

    return 0;
}

“When running this program with the inputs -8, -5, and 8, the result is as follows.”

Execution results
Radius?:-8 入力した値
Radius?:-5 入力した値
Radius?:8 入力した値
The area is 200.960000.

"This time, you only need to write the scanf function once."
The advantage of the do-while loop, which guarantees execution at least once, is being utilized.


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. newline character
  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