C++ Primer Plus 代码学习解析(第五章 5.1-5.5)

本文通过五个C++代码示例讲解了for循环的不同应用场景,包括基本计数、递增/递减循环、表达式赋值、阶乘计算以及步长可变的循环。这些例子展示了for循环在控制程序流程中的核心作用。
摘要由CSDN通过智能技术生成

5.1 dorloop.cpp

// forloop.cpp -- introducing the for loop
#include <iostream>
int main()
{
    using namespace std;
    int i;  // create a counter
//   initialize; test ; update
    for (i = 0; i < 5; i++)
        cout << "C++ knows loops.\n";
    cout << "C++ knows when to stop.\n";
    // cin.get();
    return 0;
}
  1. 该函数主要探究for的特性
  2. i从零开始加到4,到5时退出循环,共循环5次
  3. 结果打印5次

5.2 num_test.cpp

#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter the starting countdown value: ";
    int limit;
    cin >> limit;
    int i;
    for (i = limit; i; i--)     // quits when i is 0
        cout << "i = " << i << "\n";
    cout << "Done now that i = " << i << "\n";
    // cin.get();
    // cin.get();
    return 0;
}
  1. 该函数更改了传统for循环中i累加的情况,使i累减
  2. 并且该函数还将输入值作为i的初始值
  3. 最后将i的值作为终止条件,当i减到0时终止循环
  4. 综上若输入i的值为5,则该循环循环5次,到0退出循环

5.3 express.cpp

#include <iostream>
int main()
{
    using namespace std;
    int x;

    cout << "The expression x = 100 has the value ";
    cout << (x = 100) << endl;
    cout << "Now x = " << x << endl;
    cout << "The expression x < 3 has the value ";
    cout << (x < 3) << endl;
    cout << "The expression x > 3 has the value ";
    cout << (x > 3) << endl;
    cout.setf(ios_base::boolalpha);   //a newer C++ feature
    cout << "The expression x < 3 has the value ";
    cout << (x < 3) << endl;
    cout << "The expression x > 3 has the value ";
    cout << (x > 3) << endl;
    /// cin.get();
    return 0;
}
  1. 该函数首先展示了赋值的方法,(x=100)对x进行赋值
  2. 之后则用0和1分别表示x和3的大小比较是否正确
  3. 之后调用了cout.setf(ios_base::boolalpha)函数设置一个标记,将0和1表示改为true和false

5.4 formore.cpp

#include <iostream>
const int ArSize = 16;      // example of external declaration
int main()
{
    long long factorials[ArSize];
    factorials[1] = factorials[0] = 1LL;
    for (int i = 2; i < ArSize; i++)
        factorials[i] = i * factorials[i - 1];
    for (int i = 0; i < ArSize; i++)
        std::cout << i << "! = " << factorials[i] << std::endl;
    // std::cin.get();
    return 0;
}
  1. 该函数用for循环展示了1-15阶乘的算法,该函数还展示了不使用nameplace的一种用法,使用std::cout等
  2. 用一个数组来储存阶乘值,且第一和第二个数组元素都设为1(例子中用1LL,LL其实代表long long, 1LL是为了在计算时,把int类型的变量转化为long long,然后再赋值给long long类型的变量)
  3. 之后通过for循环使每一个元素都与之前所有的元素相乘,最后得出最终结果
  4. 最后将其输出

5.5 bigstep.cpp

#include <iostream>
int main()
{
    using std::cout;    // a using declaration
    using std::cin;
    using std::endl;;
    cout << "Enter an integer: ";
    int by;
    cin >> by;
    cout << "Counting by " << by << "s:\n";
    for (int i = 0; i < 100; i = i + by)
        cout << i << endl;
    // cin.get();
    // cin.get();
    return 0;
}
  1. 该函数展现了一种修改for循环步长的用法,不再是单纯的+1,而是+by
  2. 并且该函数还展示了另外一种std库的用法,即在开头声明用该库的哪些函数,之后在下面使用
  3. 步长从i++(i = i+1)改为i = i+by

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值