조건문과 루프를 c로 작성할 수 있습니다.
int
if
while
for
counter라는 변수 생성 및 0 저장
int: 변수가 정수라는 것을 알려줌
counter: 변수의 이름
0: 0으로 저장(초기화)
변수의 값을 1씩 증가(= 변수에 할당한다는 의미)
if (x < y)
{
printf("x is less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
무언가를 계속 반복
while or for로 구현됨
*무한 루프
while(true)
{
printf("hello, world\n");
}
*특정 횟수만큼 루프
변수명 : i
조건: i<50
i 증가
진행 순서
i = 0으로 설정-> i는 50보다 작은가? -> 작다 -> hello world출력한다-> i를 1증가 시킨다-> i가 50보다 작은가?-> 반복 -> i가 50보다 작은가? -> 작지 않다. -> 종료
int i = 0;
while (i < 50)
{
printf("hello, world\n");
i = i + 1;
}
for (int i = 0; i < 50; i = i + 1)
{
printf("hello, world\n");
}
Q: "개발 공부는 재미있다"를 10번 출력하는 코드 작성
*for
#include <studio.h>
int main(void
{
for (int i = 0; i < 10; i += 1)
{
print("개발 공부는 재미있다\n");
}
}
*while
#include <studio.h>
int main(void)
{
int i = 0;
while (i < 10)
{
print("개발 공부는 재미있다\n");
i ++;
}