강의

멘토링

커뮤니티

Inflearn Community Q&A

jaehyun0310273864's profile image
jaehyun0310273864

asked

C Programming - From Introduction to Game Development

for

for문에서 i++와 ++i

Written on

·

1.2K

0

for(int i =1; i<=10; ++i){

printf("Hello World %d\n",i);

}

++i를 하면 먼저 i의 값에 1을 증가시키고 printf 하니까 Hello world 2 부터 출력될줄 알았는데 

Hello world 1 부터 출력되네요...

왜 이런 결과가 나왔는지 설명해주세요

c

Answer 1

1

nadocoding님의 프로필 이미지
nadocoding
Instructor

for (선언 ; 조건 ; 증감) { 내용 } 중에서,

증감은 내용이 모두 실행되고 나서 수행됩니다.

그러므로 선언에서 i 는 1 로 정해지고, 조건을 만족하니 내용이 실행되고 이 때 1 이 출력된 이후에 증감에서 1이 더해지는 거예요. ^^

jaehyun0310273864's profile image
jaehyun0310273864

asked

Ask a question