C语言基础复习----for循环运行机制

直接上代码

#include
using namespace std;

int minCount = 0;
int min(int a, int b)
{
    minCount++;
    return a < b ? a : b;
}
int maxCount = 0;
int max(int a, int b)
{
    maxCount++;
    return a >= b ? a : b;
}
int nothCount = 0;
void nothing() 
{
    nothCount++;
}
int increCount = 0;
void increment(int *a)
{
    increCount++;
    (*a)++;
}

int main()
{
    int a, b;
    cin >> a >> b;
    for (int i = min(a, b); i < max(a, b); increment(&i))
        nothing();
    cout << "函数min被调用了 " << minCount << " 次" << endl;
    cout << "函数max被调用了 " << maxCount << " 次" << endl;
    cout << "函数nothing被调用了 " << nothCount << " 次" << endl;
    cout << "函数increment被调用了 " << increCount << " 次" << endl;
    system("pause");
    return 0;
}

测试结果:
C语言基础复习----for循环运行机制_第1张图片

总结:for循环初始化条件只执行一次,判断条件比循环体和自增运算都多执行一次!

你可能感兴趣的:(校招笔试总结,C语言嵌入式开发,C/C++)