1. 조건문 - if문
1.1 형식1 예제(소스코드, 실행결과)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int score;
printf("점수입력 : ");
scanf("%d", &score);
//if문 형식1 예제
if(score>=80)
{
printf("점수는 %d, 결과는 ", score);
printf("합격");
}
return 0;
}
*텍스트 파일 생성
예제
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int age;
FILE *fp; //1. FILE포인터 선언
printf("나이입력 : ");
scanf("%d", &age);
fp=fopen("age.txt", "w"); //2. 생성할 FILE open
fprintf(fp, "나이 : %d",age);//3. file에 내용 쓰기 : fprintf()
fclose(fp);//4. file 닫기 : fclose()
return 0;
}
1.2 형식2 예제(소스코드, 실행결과)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//if문 형식2
int score;
printf("점수 입력 : ");
scanf("%d", &score);
if(score>=80)
{
printf("점수는 %d이고, ",score);
printf("합격입니다.\n");
}
else
{
printf("점수는 %d이고, ",score);
printf("불합격입니다.\n");
}
return 0;
}
1.3 형식3 예제(소스코드, 실행결과)
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//if문 형식3
int score;
printf("점수입력 : ");
scanf("%d", &score);
if(score >= 90)
printf("A등급\n");
else if(score>=80) //주의 : else와 if사이에 공백이 있어야함.
printf("B등급\n");
else if(score>=70)
printf("C등급\n");
else
printf("F등급\n");
return 0;
}
2. 조건문 - switch문 예제
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int menu;
printf("1. 입금하기 \t 2. 출금하기 \t 3. 송금하기 \n");
printf("메뉴 번호선택 : ");
scanf("%d", &menu);
//주의 : case문 뒤에 반드시 break문을 붙인다.
switch(menu) //정수형 값 또는 정수형 결과
{
case 1: printf("입금하기 화면이동...\n"); break;
case 2: printf("출금하기 화면이동...\n"); break;
case 3: printf("송금하기 화면이동...\n"); break;
default: printf("잘못된 입력입니다. \n");
}
return 0;
}
3. 반복문 - for문 예제
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//for문
int i, sum=0;
printf("1. 1~10출력 \n\n");
for(i=1;i<=10;i++)
printf("%d\n", i);
printf("\n");
printf("2. 10~1출력 \n\n");
for(i=10;i>=1;i--)
printf("%d\n", i);
printf("\n");
printf("\n------------------\n");
printf("3. 1~10합출력");
for(i=1;i<=10;i++)
{
sum = sum+i; // sum+=i;
}
printf("합 = %d\n", sum);
printf("\n");
printf("4. 1~10중 3의 배수 출력\n\n");
for(i=0;i<=10;i+=3) // i+=3 은 i=i+3과 같음
printf("%d\n", i);
printf("\n------------------\n");
printf("\n");
printf("5. 무한반복 : 합이 1000이 넘는 i값 \n\n");
for(i=1;;i++) //조건식을 생략하면 무한반복됨
{
printf("%d\n",i);
sum+=i;
if(sum>1000) break;
}
printf("합은 %d \n",sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int line, star;
for(line=1;line<=5;line++)
{
for(star=1;star<=line;star++)
printf("*");
printf("\n");
}
for(line=1;line<=5;line++)
{
for(star=5;star>=line;star--)
printf("*");
printf("\n");
}
int i,j;
printf("6. 2중 for문 : 구구단\n\n");
for (i=2;i<=9;i++)
{
for(j=1;j<=9;j++)
{
printf("%d * %d = %d\n",i,j,i*j);
}
printf("\n");
}
return 0;
}
4. 반복문 - while문 예제
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
printf("7. while문 : 1~10출력 \n\n");
//while문 형식
// 1. 초기식
// 2. while문(조건식)
// { 반복실행 문장
// 3. 증감식
// }
int i;
i=1; //초기식
while(i<=10)//조건식
{
printf("%d\n",i);
i++; //증감식
}
return 0;
}
5. 반복문 - do~while문 예제
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
printf("8. do~while문 : 1~10출력 \n\n");
//형식
// 1.초기식
//do
// {
// //3.증감식
// } while(조건식); //2. 조건식 : 세미콜론(;) 붙인다.
int i;
i=1; //1. 초기식
do{
printf("%d\n",i);
i++; //3. 증감식
} while(i<=10); //2. 조건식
return 0;
}
'basic > C' 카테고리의 다른 글
6. 함수 (0) | 2016.10.31 |
---|---|
5. 배열 (0) | 2016.10.13 |
3. 연산자 (0) | 2016.09.22 |
2. C프로그래밍의 기초문법 (0) | 2016.09.08 |
1. C프로그래밍의 기초 (0) | 2016.09.05 |