嵌套if while循环中的语句C

时间:2015-10-23 22:25:39

标签: loops if-statement while-loop

所以我有以下代码并想知道为什么它不会被执行

int main() {
char select;
printf("Select q to quit\n");
while ((select = getchar()) != 'q') {
    if(select == 'a') {
        printf("You have not selected q\n");
        printf("Select q to quit\n");
    }
return 0;
}

因此,当select为字符select选择'a'时,它不会进入if语句。为什么???

1 个答案:

答案 0 :(得分:0)

对我来说,这段代码正在使用OSX。但你得到的问题是,如果你按下它也会退出。所以只需移动“返回0”

int main() {
char select;
printf("Select q to quit\n");
while ((select = getchar()) != 'q') {
    if(select == 'a') {
        printf("You have not selected q\n");
        printf("Select q to quit\n");
    }
}
return 0;

问候:)

相关问题