2010년/3월
3월16일 while반복문
뽀얀햄스터
2010. 3. 16. 12:07
While
while문에서 제어 변수의 초기화
#include<stdio.h>
int main()
{
char ch;
int count=0;
printf("Please enter characters <q for quit> : ");
ch = getchar();
while(1)
{
if('q'==ch)
{
break;
}
count++;
ch=getchar();
}
printf("\nThe number of character is %d\n",count);
printf("This is end of program\n");
return 0;
}
잘실행이 되도록 바꾸었다.
처음것은 count값에 쓰레기 값이 들어가기때문에.. 초기값을 지정해야하는 0을 넣을경우 while문이 돌지 않다. 그리고while문에 count=0을 넣게되면 돌면서 계속 0값이기때문에.. sum값이 0이 된다. 결론은 getchar로 받을수 있게 위에 하나 더 넣는 것이다 ~!
지금 까지 배운 while() 과 if() 그리고 여러가지를 통해 구구단완성