PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   疑難雜症區 (https://www.pcdvd.com.tw/forumdisplay.php?f=34)
-   -   C語言問題(Switch) (https://www.pcdvd.com.tw/showthread.php?t=468863)

comefish 2005-04-05 10:37 AM

C語言問題(Switch)
 
引用:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int grade=0;
int stu;
int i;

scanf("%d",&stu);
for (i=0;i<=stu;++i)
{
scanf("%d",&grade);
switch ( grade / 10 )
{
case 10:
++a;
break;
case 9:
++b;
break;
case 8:
++c;
break;
case 7:
++d;
break;
case 6:
++e;
break;
default :
++f;
}
}

printf("a=%d\n",a);
printf("b=%d\n",b);
printf("c=%d\n",c);
printf("d=%d\n",d);
printf("e=%d\n",e);
printf("f=%d\n",f);



system("PAUSE");
return 0;
}


請問那個for (i=0;i<=stu;++i)
是在執行迴圈前先行+1嗎,在scan之前?

Niise 2005-04-05 11:26 AM

引用:
作者comefish
請問那個for (i=0;i<=stu;++i)
是在執行迴圈前先行+1嗎,在scan之前?


不是...
其實不論++i 或i++
在for迴圈的syntax中都是一樣的

因為(initial;terminal condition;iterator)
初值在initial決定
檢查terminial condition
正確->迴圈內容
錯誤->離開回圈
如果正確執行回圈內容,iterator才會被執行

因此不論++i 或i++在for是一樣的
但是在其他地方則不同

例如
array[i++]=1
array[++i]=1
則是如你描述的一樣
第一個先取array[i]的address令他為1再將i+=1
第二個則是曲array[i+1]的address令他為1

oitwtn 2005-04-05 12:26 PM

引用:
作者comefish
請問那個for (i=0;i<=stu;++i)
是在執行迴圈前先行+1嗎,在scan之前?


答案否,因為你scanf輸入的值,後面才送到for迴圈中

和switch有什麼關呢?

redsky 2005-04-05 01:15 PM

for (i = 0; i < size; i++){
...
...
...
}



i = 0;
while (i < size){
...
...
...
i++;
}

是一模一樣的...(大部分的compiler會這麼做)

所以答案可以自己想...


所有的時間均為GMT +8。 現在的時間是10:16 AM.

vBulletin Version 3.0.1
powered_by_vbulletin 2025。