我的講師對while循環,例如方法:我的講師的while循環和我的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;
}
。我看到沒有區別。但是我接受了嗎?
通過手動或調試器自行完成代碼,並查看沒有有效的區別。事實上,在你的代碼中,'if'語句是不必要的;循環的唯一出路是標記== 100。 –
你們倆都應該檢查scanf的返回值 –
你的講師版本需要更少的陳述,所以更乾淨。然而,'do {...} while'循環甚至可以更好,因爲它不需要初始化'mark'。 –