#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("---------2. 반복문 for---------\n");
// for (초기식; 조건식; 증감식) {
// 반복할 코드;
// }
/* for문 실행 순서
1. 초기식
2. 조건식
3. 반복할 코드
4. 증감식
2~4번이 조건이 거짓일 때까지 반복
5. 조건식
6. 반복할 코드
7. 증감식
*/
for (int i = 1; i <= 3; i++) {
printf("i는 %d\n", i);
}
int sum = 0;
int i;
for (i = 5; i > 0; i--) {
sum += i;
printf("sum = %d\n", sum);
}
printf("---------2. 반복문 while---------\n");
// 초기값 선언;
// while (조건) {
// 반복할 코드 (증감 포함)
// }
i = 3;
while (i > 0) {
printf("%d\n", i--);
// i -= 1;
}
printf("땡\n");
// a는 1 2 3 4 5 나오도록
a = 0;
while (a < 5) {
printf("a는 %d\n", ++a);
}
printf("---------3. 반복문 do-while---------\n");
// do-while문 : 무조건 한 번은 실행, 조건이 참인 동안 반복
// do {
// 반복할 코드;
// } while (조건);
i = 3;
do {
printf("%d\n", i--);
} while (i > 0);
a = 0;
do {
printf("최소 1번 실행됩니다\n");
} while (a < 0);
printf("----문제----\n");
// 1부터 100까지의 합 구하기
int j = 1;
sum = 0;
do {
sum += j;
j++;
} while (j <= 100);
printf("1부터 100까지 합은 %d\n", sum);
printf("---------4. 이중 반복문 ---------\n");
// 이중 반복문 : 반복문 안에 반복문이 있는 구조
for (int i = 1; i <= 3; i++) {
printf("첫 번째 반복문 : %d\n", i);
for (int j = 1; j <= 3; j++) {
printf("\t두 번째 반복문 : %d\n", j);
}
}
// 별 피라미드 만들기
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
printf("\n");
printf("---------5. 논리 연산자 ---------\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 == 15);
printf("%d\n", !(a + b <= c) && b != 10);
printf("%d\n", a + (b > c));
printf("---------6. 연산자 우선 순위, 삼항 연산자 ---------\n");
/* 연산자 우선순위
1. 괄호 ()
2. 단항 연산자 ! ++ --
3. 산술 연산자 * / % -> + -
4. 비교 연산자 > < >= <= == !=
5. 논리 연산자 && -> ||
6. 대입 연산자 = += -=
*/
a = 5, b = 10, c = 15;
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("%s\n", 5 > 10 ? "참" : "거짓");
int score = 100;
printf("%s\n", score >= 60 ? "합격" : "불합격");
int x = 5, y = 9;
int max = x > y ? x : y;
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: