#include <stdio.h>
int main() {
printf("------ 1. 증감연산자------\n");
int a = 10;
printf("a는 %d\n", a);
a = a + 2;
printf("a는 %d\n", a);
a += 2;
printf("a는 %d\n", a);
a++; // ++ : 변수에 1을 더해서 다시 변수에 넣는다
printf("a는 %d\n", a);
a--; // -- : 변수에 1을 빼서 다시 변수에 넣는다
printf("a는 %d\n", a);
int b = 20;
printf("b는 %d\n", ++b); //전위
printf("b는 %d\n", b++); //후위
printf("b는 %d\n", b);
int c = 30;
printf("c는 %d\n", --c); //1.빼기1 계산 2.printf
printf("c는 %d\n", c--); //1.printf 2.빼기1 계산
printf("c는 %d\n", c);
printf("\n------ 2. 반복문 for------\n");
// for (초기식; 조건식; 증감식) {
// 반복할 코드;
// }
/* for문 실행 순서
1. 초기식
2. 조건식
3. 반복할 코드
4. 증감식
2 ~ 4가 조건이 거짓일 때까지 반복된다
5. 조건식
6. 반복할 코드
7. 증감식 ... */
for (int i = 1; i <= 10; i++) {
printf("i는 %d\n", i);
}
int sum = 0;
int i;
for (i = 1; i <= 5; i++) {
sum += i;
printf("sum = %d\n", sum);
}
printf("\n------ 3. 반복문 while------\n");
// 초기값 선언;
// while (조건) {
// // 반복할 코드 (증감 포함)
// }
i = 3;
while (i > 0) {
printf("%d\n", i--);
//i = i - 1;
}
a = 0;
while (a <= 5) {
printf("a는 %d\n", ++a);
}
printf("----문제----\n");
b = 5;
while (b < 10) {
printf("%d ", ++b);
}
printf("\n\n------ 4. 반복문 do-while------\n");
// do-while문 : 무조건 한 번은 실행하고 조건이 참인 동안 반복
// do {
// 반복할 코드;
// } while (조건);
i = 3;
do {
printf("%d\n", i--);
} while (i > 0);
a = 1;
do {
printf("a는 %d\n", a);
a++;
} while (a <= 5);
b = 0;
do {
printf("이 문장은 최소 1번 실행됩니다.\n");
} while (b > 0);
printf("\n------ 5. 이중 반복문------\n");
// 이중 반복문 : 반복문 안에 반복문이 있는 구조
for (int i = 1; i <= 3; i++) {
printf("첫 번째 반복문 : %d\n", i);
for (int j = 1; j <= 5; j++) {
printf("\t두 번째 반복문 : %d\n", j);
}
}
// 구구단 만들기
for (int i = 2; i <= 9; i++) {
printf("%d단 출력\n", i);
for (int j = 1; j <= 9; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
}
printf("\n------ 6. 논리연산자, 연산자 우선순위------\n");
/* 논리 연산자
&& : AND 양쪽 모두 참이면 참
|| : OR 둘 중 하나 이상 참이면 참
! : NOT 참과 거짓을 반대로
C언어에서는 참을 1, 거짓을 0으로 취급한다
*/
a = 5, b = 10, c = 15;
printf("%d\n", a > 3 && b > 5);
printf("%d\n", a < 3 || c == 15);
printf("%d\n", !(a == 5));
printf("%d\n", a + b > c && c == 15);
printf("%d\n", a + (b > c));
/* 연산자 우선순위
1. 괄호()
2. 단항 연산자 ! ++ --
3. 산술 연산자 * / % -> + -
4. 비교 연산자 < > <= >= == !=
5. 논리 연산자 && -> ||
6. 대입 연산자 = += -=
*/
printf("----문제----\n");
// 문제 : 아래 식에서 연산 순서를 적으시오
printf("%d\n", a + b > c && c == 15);
// 1 2 4 3
printf("%d\n", a * b + c > b || a == 5);
// 1 2 3 5 4
printf("%d\n", !(a + b <= c) && b != 10);
// 3 1 2 5 4
printf("\n------ 7. 삼항 연산자------\n");
// 삼항 연산자 : 한 줄로 조건문 만들기
// 조건식 ? 값1 : 값2
// 조건이 참이면 값1, 조건이 거짓이면 값2
printf("%s\n", 5 > 10 ? "참" : "거짓");
int score = 100;
printf("%s\n", score >= 60 ? "합격" : "불합격");
a = 5, b = 9;
int max = (a > b) ? a : b;
printf("더 큰 값은 %d\n", max);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: