반복문
for (초기값; 조건문; 증감문) {
// 조건문의 결과가 참인 경우 실행
// 실행할 코드
}
ex)
for (int i = 0; i < 10; i++) {
std::cout << i+1 << ". 안녕하세요\n";
}
for (int i = 0; i < 10; ++i) {
std::cout << i+1;
std::cout << std::endl;
}
for (int i = 10; i > 0; i--) {
std::cout << i;
std::cout << std::endl;
}
while (조건문) {
//조건문이 참인 경우 반복적으로 실행하는 명령문
}
ex)
int i;
i=0;
while (i != 10) {
std::cout << i << "\n";
i++;
}
do {
// 조건문이 참인 경우 반복적으로 실행하는 명령문
// 단, 처음 한 번은 무조건 실행된다.
}
while(조건문);
ex)
int i = 0;
do {
std::cout << i << "\n";
i = i+2;
} while (i < 10);
// 0 2 4 6 8
실습
for (int i = 1; i <= 9; i++) {
std::cout << "----- " << i << "단 -----";
std::cout << std::endl;
for (int j = 1; j <= 9; j++) {
std::cout << i << " * " << j << " = " << i * j;
std::cout << std::endl;
}
}
int i, j;
std::cout << "숫자 양의 정수를 입력하세요: ";
std::cin >> j;
std::cout << std::endl;
std::cout << "1부터" << j << "까지의 핪은";
for (i = j; j > 0; --j) {
i = (j - 1) + i ;
}
std::cout << i << "이다.";
//--------------------while문 1~9단------------------
int i = 1;
int j = 0;
while (i != 10) {
std::cout << "----- " << i << "단 -----" << "\n";
while (j != 9) {
std::cout << i << "*" << j << "=" << i * ++j << "\n";
}
i++;
j = 0;
}
// -----------------while문 1부터 n까지의 합---------------
int i;
std::cout << "숫자 양의 정수를 입력하세요: ";
std::cin >> i;
int sum = 0;
std::cout << "1부터" << i << "까지의 합은";
while (i != 0) {
sum = sum + i;
i--;
}
std::cout << sum;
int num1, num2;
num2 = 0;
num1 = 1;
while (num1 != 0) {
std::cout << "더하고 싶은 숫자 입력 (0입력시 종료): ";
std::cin >> num1;
num2 = num1 + num2;
}
std::cout << num2;
int star;
int i,j ;
std::cout << "숫자를 입력하세요 : ";
std::cin >> star;
for (i = 0; i < star; i++) {
for (j = 0; j <= i; j++) {
std::cout << "*";
}
std::cout << "\n";
j = 0;
}
함수
필요할 때마다 호출이 가능하다.
- 반복적으로 수행해야 하는 기능을 한 번 만들어 놓으면 필요할 때 마다 호출해서 사용할 수 있다.
논리적인 단위로 분할이 가능하다.
- 코드를 기능에 따라 나눠서 볼 수 있다.
- 코드를 분석할 때 함수로 구분이 되어있으면 분석이 쉬워진다.
리턴 타입 함수 이름 (매개 변수)
{
//함수의 본문
}
리턴 타입: 이 함수가 결과로 어떤 유형의 값을 리턴(반환)할 지 선언
함수 이름: 함수의 이름을 결정. 추후 호출 할 때 사용될 이름
매개 변수: 함수를 호출할 때 전달된 값을 함수 내부에서 사용할 수 있게 해주는 변수
-------------------------------함수의 선언---------------------------------
int funcEx_1(){
return 10;
}
std::string funcEx_2(){
return "hello";
}
void funcEx_3(){
std::cout << "리턴 타입이 void인 경우 return 필요X" << std::endl;
}
-------------------------------함수의 호출---------------------------------
int a = funcEx_1();
std::cout << a << std::endl; // 10
std::string str = funcEx_2();
std::cout << str << std:: endl; // hello
funcEx_3(); // 리턴 타입이 void인 경우 return 필요 X
-------------------------------함수의 선언---------------------------------
int funcEx_sum(int n1, int n2){
return n1 + n2;
}
-------------------------------함수의 호출---------------------------------
std::cout << funcEx_sum(2,3) << std::endl; // 5
ex)
int functionTestInt();
double functionTestDouble();
std::string functionStr();
void functionTestVoid(int, int, std::string);
int functionTestSum(int, int, std::string);
int functionTestSum(int, int);
int main() {
.
.
.
}
int functionTestSum(int n1, int n2) {
return n1 + n2;
}
int functionTestSum(int n1, int n2, std::string op) {
int result = 0;
if (op == "+") {
result = n1 + n2;
}
else if (op == "-") {
result = n1 - n2;
}
return result;
}
.
.
.
double functionTestDouble() {
return 5.4;
}
int functionTestInt() {
return 5;
}
실습
두 개의 정수를 매개 변수로 받아 그 합을 리턴하는 add함수를 만들자
두 개의 정수를 매개 변수로 받아 그 차를 리턴하는 minus함수를 만들자
- 단, 인수의 순서에 상관없이 큰 수에서 작은 수를 뺀 결과를 리턴
두 개의 정수를 매개 변수로 받아 그 곱을 리턴하는 multiple함수를 만들자
두 개의 정수를 매개 변수로 받아 그 나눗셈을 리턴하는 divide함수를 만들자
- 단, divide함수는 실수를 리턴
main함수에서 위의 4개의 함수를 한 번씩 호출하여 리턴된 값을 출력
int functionadd(int a,int b) {
return a+b;
}
int functionminus(int a, int b) {
int result = 0;
if (a > b) {
result = a - b;
}
else {
result = b - a;
}
return result;
}
int functionmultiple(int a, int b) {
return a*b;
}
double functiondivide(int a, int b) {
return (double)a / (double)b;; // 강제 형변환
}
int main() {
int a, b;
std::cout << "계산하고 싶은 첫 번째 수를 입력해주세요: ";
std::cin >> a;
std::cout << "계산하고 싶은 두 번째 수를 입력해주세요: ";
std::cin >> b;
int add = functionadd(a, b);
int minus = functionminus(a, b);
int multipul = functionmultiple(a, b);
double divide = functiondivide(a, b);
std::cout << "두 수의 합: " << add << std::endl;
std::cout << "두 수의 차: " << minus << std::endl;
std::cout << "두 수의 곱: " << multipul << std::endl;
std::cout << "두 수의 나눗셈: " << divide << std::endl;
}
std::string holZzak(int a) { // 문자열을 반환하기 위한 리턴 탑입 string
std::string result;
if (a % 2 == 0) {
result = "짝수";
}
else {
result = "홀수";
}
return result; // 결과값을 result로 반환
}
int main() {
int a = 0;
std::string result;
std::cout << "숫자 입력: ";
std::cin >> a;
result = holZzak(a);
std::cout << result;
}
int deaso(int a, int b, int c) {
int result = 0;
if (a > b && a > c) {
result = a;
}
else if (b > a && b > c) {
result = b;
}
else{
result = c;
}
return result;
}
int main() {
int a, b, c;
int result;
std::cin >> a;
std::cin >> b;
std::cin >> c;
result = deaso(a, b, c);
std::cout << result;
}
마무리!
뒤쳐지지 말자고 다짐한게 어제인디!
벌써 슬슬..ㅎ.. 컴공 졸업생이 맞는지 의문이다.
뒤쳐지진 않았지만 앞서지도 못 하고 있다.
키패드로 마우스 움직이려니 답답하기도 하고
다행히 벨로그에 정리하면서 이해 안 됐던 부분들이 검색도 하면서 이해가 되었다.
HMI나 PLC보단 확실히 벅차다는 느낌이 들지만 그래도 재밌다!
마우스 배터리도 교체했고 내일부터는 수업 놓치지 말고 더 집중해서 들어야겠다.
내일은 더더더더 파이퉹..!
c++ precision 함수 : 소수점의 자릿수를 조절
ex)
std::cout.precision(숫자)