03 while in C feat. Python
嘗試用python去認識一個新的語言 — C語言
幫大家回憶一下,還記得在python中while的寫法有兩種嗎?
一種是while,一種是while True ( 應該還…記…得…吧
忘記的話可以看完 Py 星登陸週誌 — 0006 while 再回來!
Get Annby.LO-O’s stories in your inbox
Join Medium for free to get updates from this writer.
在python中while True的結構是:
While True:
# True區
if not you_are_good(): # 離開while loop迴圈的條件
break # leave loop(False區的傳送門)
# False區不一樣的地方是,在C語言中沒有while True的用法,但有類似的結構:
🌐 while(1)
在C中,while(1)是最常見的無限迴圈寫法,1 在bool中就代表 True
#include <stdio.h>
int main() {
int counter = 0 ;
while(1){
printf("total_count: %d\n", counter);
counter++;
if (counter >= 3){
printf("True, break the loop.\n");
break;
}
}
return 0;
}出來的結果就會是:
total_count:0
total_count:1
total_count:2
total_count:3
total_count:4
True, break the loop.還有另外查到一種是 for(;;) ,但我想要下次寫for迴圈的時候再一起比較,畢竟把“for”的寫法放在一起對剛學的人可能比較好歸類。
#include <stdio.h>
int main(){
printf("see you next time. \n");
return 0;
}








