3.选项说明 id++, id-- variable post-increment, post-decrement ++id, --id variable pre-increment
pre-increment profile int index = 0; int value = 0; for (int i = 0; i < count; ++i) { value = ++index; } // post-increment
Result of pre-increment: " << result1 << std::endl; int result2 = i++; std::cout << "After post-increment : " << i << std::endl; std::cout << "Result of post-increment: " << result2 << std::endl; return 0; } 输出结果将是: Before increment: 0 After pre-increment: 1 Result of pre-increment: 1 After post-increment : 2 Result of post-increment: 1 可以看到,++i先将i的值加1,再返回加1后的值,而i++先返回i的当前值,再将i的值加1。
Result of pre-increment: " << result1 << std::endl; int result2 = i++; std::cout << "After post-increment : " << i << std::endl; std::cout << "Result of post-increment: " << result2 << std::endl; return 0; } 输出结果将是: Before increment: 0 After pre-increment: 1 Result of pre-increment: 1 After post-increment : 2 Result of post-increment: 1 可以看到,++i先将i的值加1,再返回加1后的值,而i++先返回i的当前值,再将i的值加1。
operator Array access Member access from a pointer Member access from an object Scoping operator Post-increment
System.out.println("++i : " + ++i); //2 Pre-increment 先增 System.out.println("i++ : " + i++); //2 Post-increment
N/A -- lvalue Pre-decrement Numeric N/A lvalue ++ Post-increment
count = 0; public static void main(String[] args) { while(true) { try { // Post-increment
count = 0; public static void main(String[] args) { while(true) { try { // Post-increment