【零基础C++从入门到精通】(三) 流程控制与语言结构

参考书籍

在这里插入图片描述
**总结:**在本章中,我们首先介绍了简单语句和复合语句的区别,并讲解了空语句的概念。接着我们依次介绍了if和lswitch这2种条件控制语句,以及while、do…while和for这3种循环控制语句的语法、用法和相互之间的转换。最后我们讲解了break、continue和goto这3种跳转语句的适用范围和实例。

3.1 简单语句

3.1.1 空语句

3.1.2 作用域和块

程序的作用域是自顶向下定义的,在最上级首先有一个全局作用域( Clobal Scope )。全局作用域定义的变量可以在文件中的所有地方被访问,它也叫作全局变量(Global Variable ),而其他非全局的变量叫作局部变量(Local Variable ) ;而在其下,我们会用花括号((})定义一个个的块( Block),每个块都定义了一层的作用域,而块中又能嵌套块,于是就有了层级的作用域结构。首先,我们来看一个最基本的作用域示例:

#include <iostream>
using namespace std;

// 全局变量

int num = 3; // 全局变量num

int main() {
	cout << "num的值为:" << num << endl;
	int num = 2; // 局部变量num
	cout << "num的值为:" << num << endl;
	return 0;
}

在这里插入图片描述

3.2 条件控制语句

3.2.1 if语句

#include <iostream>
using namespace std;

// 基本if语句

int main() {
	int num = 5;
	if (num > 4)
		cout << "数字大于4。" << endl;
	num = 3;
	if (num > 4)
		cout << "数字大于4。" << endl;
	else
		cout << "数字小于等于4。" << endl;
	return 0;
}

在这里插入图片描述

3.2.2 switch语句

#include <iostream>
using namespace std;

// 使用switch语句打印一位数中文数字

int main() {
	int num = 6;
	switch (num) {
	case 0:
		cout << "零" << endl;
		break;
	case 1:
		cout << "一" << endl;
		break;
	case 2:
		cout << "二" << endl;
		break;
	case 3:
		cout << "三" << endl;
		break;
	case 4:
		cout << "四" << endl;
		break;
	case 5:
		cout << "五" << endl;
		break;
	case 6:
		cout << "六" << endl;
		break;
	case 7:
		cout << "七" << endl;
		break;
	case 8:
		cout << "八" << endl;
		break;
	case 9:
		cout << "九" << endl;
		break;
	default:
		cout << "数字不在0-9范围内!" << endl;
		break;
	}
	return 0;
}

在这里插入图片描述

3.3 循环控制语句

3.3.1 while语句
#include <iostream>
using namespace std;

// While语句

int main() {
	int i = 0;
	while (i < 10) {
		cout << "打印数字: " << i << endl;
		i++;
	}
	return 0;
}

在这里插入图片描述

3.3.2 do…while语句
#include <iostream>
using namespace std;

// do while

int main() {
	int i = 0;
	do {
		cout << "打印数字: " << i << endl;
		i++;
	} while (i < 10);
	return 0;
}

在这里插入图片描述

3.3.3 for语句

打印1:N数字

#include <iostream>
using namespace std;

// for语句基本用法

int main() {
	for (int i = 0; i < 10; i++) {
		cout << "打印数字: " << i << endl;
	}
	return 0;
}

在这里插入图片描述
可以省略部分

#include <iostream>
using namespace std;

// for语句的省略写法

int main() {
	int i = 0;
	for (; i < 10; ) {
		cout << "打印数字: " << i << endl;
		i++;
	}
	return 0;
}

在这里插入图片描述
更复杂的

#include <iostream>
using namespace std;

// for语句中的逗号操作符

int main() {
	for (int i = 0, j = 10; j > 5 && i < 10; i++, j--) {
		cout << "打印数字: " << i << " 和 " << j << endl;
	}
	return 0;
}

在这里插入图片描述
打印9*9乘法表

#include <iostream>
using namespace std;

// 打印九九乘法表

int main() {
	for (int i = 1; i < 10; i++) {
		for (int j = 1; j < 10; j++) {
			cout << i << " x " << j << " = " << i * j << "    ";
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

3.4 跳转语句

3.4.1 break语句

break语句是用来跳出switch或者循环体的语句。

#include <iostream>
using namespace std;

// break语句

int main() {
	for (int i = 0; i < 10; i++) {
		cout << i << endl;
		if (i > 5) break;
	}
	cout << "循环结束!" << endl;
	return 0;
}

在这里插入图片描述
break语句对于所有循环语句和switch语句都有效,但是对if语句没有作用。

#include <iostream>
using namespace std;

// if语句中的break语句

int main() {
	for (int i = 0; i < 10; i++) {
		cout << i << endl;
		if (i > 5) {
			break;
		}
		cout << "在if语句外!" << endl;
	}
	cout << "跳出循环!" << endl;
	return 0;
}

在这里插入图片描述
我们可以看到,第七次循环中i>5的条件成立,break语句并没有让程序跳到if语句外,打印“在if语句外”,而是直接跳到了循环的外面。

3.4.2 continue语句

break语句解决了提早结束循环的问题,但有时我们只想结束当前一轮的循环,而依然让循环继续下去,这时使用continue语句就可以解决这一问题了。continue语句不会直接跳到整个循环的后面,而是跳回至条件判断,这样当前一轮循环剩余的代码都不会执行,而新一轮的循环依旧可以正常进行。

#include <iostream>
using namespace std;

// continue语句

int main() {
	for (int i = 0; i < 10; i++) {
		if (i % 2 == 0) {
			continue;
		}
		cout << i << endl;
	}
	return 0;
}

在这里插入图片描述
示例在循环中每次碰到i被2整除余0,也就是i是偶数的情况时,就用continue跳过本次循环的所有后续代码,进入下一次循环,这样就只有奇数会被打印出来了。

3.4.3 goto语句

goto语句可以使程序跳转到任意一个用标签( Label)标记过的语句。

#include <iostream>
using namespace std;

// goto语句

int main() {
	goto here;
	cout << "本来应该也打印这句。" << endl;
here:
	cout << "现在只打印这句。" << endl;
	return 0;
}

在这里插入图片描述
示例中的第一个输出语句被跳过了,程序通过goto直接跳转到标签here,并只打印了第二个输出语句。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大桃子技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值