μ¦κ° μ°μ°μλ λμ κ°μ 1μ© μ¦κ° νΉμ κ°μμν΅λλ€.
(variable++: variable = variable + 1)
(variable--: variable = variable - 1)
λ‘ μ΄ν΄νλ©΄ μ’μ΅λλ€.
μ¦κ° μ°μ°μλ μ μ, μ€μ, ν¬μΈν° λ³μμμ μ¬μ© κ°λ₯ν©λλ€.
μ¦κ° μ°μ°μμλ λκ°μ§ notationμ΄ μμ΅λλ€.
Prefix notation: ++num
Postfix notation: num++
κ°κ° μ¦κ° μ°μ°μ statement μ΄μ μ μ μ©ν μ§, statement μ€ν μ΄νμ μ μ©ν μ§λ₯Ό κ²°μ ν©λλ€.
// μ¦κ° μ°μ°μ μ¬μ© μμ
#include <iostream>
using namespace std;
int main(){
int counter {10};
cout << "Counter: " << counter << endl;
// Counter: 10
counter = counter + 1;
cout << "Counter: " << counter << endl;
// Counter: 11
counter ++;
cout << "Counter: " << counter << endl;
// Counter: 12
++ counter;
cout << "Counter: " << counter << endl;
// Counter: 13
return 0;
}
λ€μμ prefix notationκ³Ό postfix notaitonμ μ°¨μ΄λ₯Ό 보μ¬μ£Όλ μ½λμ λλ€.
// prefix notation v.s. postfix notation
#include <iostream>
using namespace std;
int main(){
int counter {10};
int result = {0};
cout << "Counter: " << counter << endl;
// Counter: 10
// prefix notation
result = ++counter;
cout << "Counter: " << counter << endl;
cout << "Result: " << result << endl;
// Counter: 11
// Result: 11
// counter & result κ° μ¬μ μ
counter = 10;
result = 0;
// postfix notation
result = counter++;
cout << "Counter: " << counter << endl;
cout << "Result: " << result << endl;
// Counter: 11
// Result: 10
return 0;
}
prefix notationμ κ²½μ°μλ counter λ³μκ°μ΄ μ°μ μ μΌλ‘ λ³κ²½λκ³ λ μ΄ν,
statementκ° μ€νλκΈ° λλ¬Έμ, ν΄λΉ statment μ€ν μ΄ν λ³μκ°μ νμΈ ν μ,
counterκ³Ό result λͺ¨λ 1μ© μ¦κ°ν λͺ¨μ΅μ λλ€.
λ°λλ‘ postfix notationμμλ statementκ° λͺ¨λ μ§νλ μ΄νμ counter κ°μ΄ 1 μ¦κ°νκΈ° λλ¬Έμ,
counterμ 1 μ¦κ°νμ§λ§, resultλ μ¦κ°νκΈ° μ΄μ μ κ°μΈ 10μ λ°μ κ²μ νμΈν μ μμ΅λλ€.
λ€λ₯Έ μ½λλ₯Ό νλ² λ³΄κ² μ΅λλ€.
// prefix notation
#include <iostream>
using namespace std;
int main(){
int counter = 10;
int result = 0;
cout << "Counter: " << counter << endl;
// Counter: 10
// prefix notation
result = ++counter + 10;
cout << "Counter: " << counter << endl;
cout << "Result: " << result << endl;
// Counter: 11
// Result: 21
return 0;
}
μ΄λ²μλ μμ prefix notationμ΄κΈ° λλ¬Έμ,
resultμ, (counter + 1) + 10 = 11 + 10 = 21μ μ μ₯νκ² λκ³ ,
counterμλ κΈ°μ‘΄κ°μ 1μ μ¦κ°ν 11μ΄ μ μ₯λκ² λ©λλ€.
postfix notationμ κ²½μ°λ λ€μκ³Ό κ°μ΅λλ€.
// postfix notation
#include <iostream>
using namespace std;
int main(){
int counter = 10;
int result = 0;
cout << "Counter: " << counter << endl;
// Counter: 10
result = counter++ + 10;
cout << "Counter: " << counter << endl;
cout << "Result: " << result << endl;
// Counter: 11
// Result: 20
return 0;
}
μ΄λ²μλ result = counter + 10μ statement μμ© μ΄νμ counter = counter + 1μ΄ μ μ©λλ€κ³ μ΄ν΄νλ©΄ λ©λλ€.
κ·Έλ κΈ° λλ¬Έμ, Counterλ 1μ΄ μ¦κ°νμμ§λ§, resultμλ μ¦κ°ν κ°μ΄ μ μ©λμ§ λͺ»ν΄ κΈ°μ‘΄μ κ° 10(counter) + 10 = 20μ΄ μ μ₯λ©λλ€.
λ³Έλ¬Έμμλ μ¦κ° μ°μ°μμ κ²½μ°μλ§ μμ±νμ§λ§, κ°μ μ°μ°μλ μ νν κ°μ λ°©μμΌλ‘ μ μ©λ©λλ€.
* νκ°μ§ statementμμ λμΌν λ³μμ λν΄ 2κ° μ΄μμ μ¦κ° μ°μ°μλ₯Ό μ¬μ©νμ§ μλλ‘ μ£Όμν΄μΌ ν©λλ€!
'C++ > κΈ°ν' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C++] c++μμ boolean κ°μ true/falseλ‘ μΆλ ₯νκ³ μΆμ λ (0) | 2023.12.07 |
---|---|
[C++] μ μλΌλ¦¬μ λλμ (0) | 2023.12.07 |
[C++] Assignment Operator (ν λΉ μ°μ°μ), = (0) | 2023.12.06 |
[C++] λ°°μ΄ (Array) (0) | 2023.12.05 |
[C++] μμ(constant) (0) | 2023.12.05 |