c++ 学习笔记之循环篇

本文深入探讨了C++中的循环结构,包括for、while和do while循环的使用,详细讲解了循环的各个组成部分、如何修改步长、访问字符串、递增和递减运算符以及指针的应用。此外,还介绍了循环与文本输入的结合,包括不同方式的cin输入及文件尾条件判断,最后阐述了嵌套循环和二维数组的初始化技巧。
摘要由CSDN通过智能技术生成

#include <iostream>
#include <string>

5.1 for循环

void forDemo() {
   
	using namespace std;
	for (int i = 0; i < 10; i++) {
   
		cout << i+1 << " never give up" << endl;
	};
}

5.1.1. for循环的组成部分

void partOfFor() {
   
	using namespace std;
	cout
		<< "for循环的组成部分:\n"
		<< "for (initialization; test-expression; update-expression){\n body\n}\n"
		<< "1.设置初始值\n"
		<< "2.执行测试\n"
		<< "3.执行循环操作\n"
		<< "4.更新用于测试的值" << endl;
}
void backwords() {
   
	using namespace std;
	int num; cout << "please input the number ";
	cin >> num; cout << "you input number is " << num << endl;
	for (num; num > 0; num--) {
   
		cout << "num = " << num << "\n";
	}
	cout.setf(ios_base::boolalpha);
	cout << (1 < 3) << endl;
	cout << (2 > 3) << "\n";
}

5.1.2. 回到for循环

//for循环阶乘应用
void factorial() {
   
	using namespace std;
	const int Max = 16;
	long long archive[Max];
	archive[0] = archive[1] = 1ll;
	for (int i = 2; i < Max; i++) {
   
		archive[i] = i * archive[i-1];
	}
	for (int j = 0; j < Max;j++ ) {
   
		cout << j << " != " << archive[j] << endl;
	}
}

5.1.3. 修改步长

void wideChange() {
   
	using namespace std;
	for (int i = 0; i < 100; i+=14)
	{
   
		cout << "i = " << (i+1) << endl;
	}
}

5.1.4. 使用for循环访问字符串

void visitString() {
   
	using namespace std;
	string str;
	cin >> str;
	cout << str.size() << endl;
	for (int i = str.size() - 1; i >= 0; i--) {
   
		cou
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值