如果你平时写 Rust 然后不喜欢 C 的 `do...while` 请看这篇文章

37 阅读1分钟

不是说 C 的 do...while 不好啊。只是我不喜欢写。每次要划到最后面才能看到条件,什么感觉额……

#include <stdio.h>
#include <stdlib.h>

const char* INFO_TOO_BIG = "Wrong! Too big!\n";
const char* INFO_TOO_SMALL = "Wrong! Too small!\n";
const char* INFO_RIGHT = "Right!\n";

int
main() {
    int magic;
    int mut_guess;

    int mut_counter = 0;

    magic = rand() % 100 + 1; // < [stdlib.h]
    
    do {
        printf(
            "Please guess the magic number: "
        ); // < [stdio.h]
    
        int _1 = scanf(
            "%d",
            &mut_guess
        ); // < [stdio.h]

        mut_counter += 1;
   
        if (mut_guess > magic) {
            printf(INFO_TOO_BIG); // < [stdio.h]
        } else if (mut_guess < magic) {
            printf(INFO_TOO_SMALL); // < [stdio.h]
        } else {
            printf(INFO_RIGHT); // < [stdio.h]
        }
    } while (mut_guess != magic);

    printf(
        "counter = %d",
        mut_counter
    );  // < [stdio.h]

    return 0; 
}


介绍两种比较好的方法吧。AI 教我的。

改成类似于 loop 的写法

说实话我挺喜欢这么写的,美名为之,终端文本界面程序。

虽然看起来有点反直觉。

#include <stdio.h>
#include <stdlib.h>

const char* INFO_TOO_BIG = "Wrong! Too big!\n";
const char* INFO_TOO_SMALL = "Wrong! Too small!\n";
const char* INFO_RIGHT = "Right!\n";

int
main() {
    int magic;
    int mut_guess;
    int mut_counter = 0;

    magic = rand() % 100 + 1; // < [stdlib.h]
    
    while (1) {
        printf(
            "Please guess the magic number: "
        ); // < [stdio.h]
    
        int _1 = scanf(
            "%d",
            &mut_guess
        ); // < [stdio.h]

        mut_counter += 1;
   
        if (mut_guess > magic) {
            printf(INFO_TOO_BIG); // < [stdio.h]
        } else if (mut_guess < magic) {
            printf(INFO_TOO_SMALL); // < [stdio.h]
        } else {
            printf(INFO_RIGHT); // < [stdio.h]
            break; // 退出条件在这里,清晰可见!
        }
    }

    printf(
        "counter = %d",
        mut_counter
    );  // < [stdio.h]

    return 0; 
}

一种更符合 rust 阅读习惯的写法

#include <stdio.h>
#include <stdlib.h>

const char* INFO_TOO_BIG = "Wrong! Too big!\n";
const char* INFO_TOO_SMALL = "Wrong! Too small!\n";
const char* INFO_RIGHT = "Right!\n";

int
main() {
    int magic;
    int mut_guess = 0; // 显式初始化
    int mut_counter = 0;

    magic = rand() % 100 + 1; // < [stdlib.h]
    
    while (mut_guess != magic) { // 条件在前面,一眼就能看到!
        printf(
            "Please guess the magic number: "
        ); // < [stdio.h]
    
        int _1 = scanf(
            "%d",
            &mut_guess
        ); // < [stdio.h]

        mut_counter += 1;
   
        if (mut_guess > magic) {
            printf(INFO_TOO_BIG); // < [stdio.h]
        } else if (mut_guess < magic) {
            printf(INFO_TOO_SMALL); // < [stdio.h]
        } else {
            printf(INFO_RIGHT); // < [stdio.h]
        }
    }

    printf(
        "counter = %d",
        mut_counter
    );  // < [stdio.h]

    return 0; 
}

不过我说啊,确实有的时候写 do...while 挺合适的。就不举例了。