gpt4 book ai didi

c - 我的讲师的 while 循环和我的 while 循环有什么区别吗?

转载 作者:行者123 更新时间:2023-11-30 20:59:38 25 4
gpt4 key购买 nike

我的讲师的 while 循环方法是,例如:

#include <stdio.h>
int main(){
int mark;
mark = 0;
while (mark !=100){
printf("Please input your mark");
scanf("%i",&mark);
if (mark <= 50){
printf("You need to practice more");
}
else if (mark==100){
printf("You are too good. Nothing to say to you. Keep it up");
}
}
return 0;
}

这是我的 while 循环方法:

#include <stdio.h>
int main(){
int mark;
printf("Please input your mark");
scanf("%i",&mark);
while (mark !=100){
if (mark <= 50){
printf("You need to practice more");
}
printf("Please input your mark");
scanf("%i",&mark);

}
if (mark==100){
printf("You are too good. Nothing to say to you. Keep it up");
}

return 0;
}

基于两者的结果。我看没有什么区别。但我的被接受了吗?

最佳答案

第三个选项(do ... while())。还把琴弦清理干净了。尽管优化编译器可能会在编译期间添加与 continue 等效的内容,但 continue 避免了冗余比较。

#include <stdio.h>
int main() {
int mark;
do {
printf("Please input your mark: ");
scanf("%i", &mark);
if (mark <= 50) {
printf("You need to practice more.\n");
continue; /* optimizing compiler may already do this */
}
} while (mark != 100);
printf("You are too good. Nothing to say to you. Keep it up.\n");
return 0;
}

关于c - 我的讲师的 while 循环和我的 while 循环有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46380606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com