【学习笔记】C++ 基础入门(二)程序流程结构 + 数组

内容来自小破站《黑马程序员C++》复习自用

4 程序流程结构

C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
1.程序按顺序执行,不发生跳转;
2.依据条件是否满足,有选择的执行相应功能;
3.依据条件是否满足,循环多次执行某段代码。

4.1 选择结构

4.1.1 if 语句

  1. 单行格式 if 语句
#include <iostream>
using namespace std;
#include<string>
int main() {
	//用户输入分数,如果大于600,则输出nice
	//1.用户输入分数
	int score = 0;
	cout << "请输入您的分数" << endl;
	cin >> score;
	
	//2.打印用户输入的分数
	cout << "您输入的分数为:" << score << endl;
	
	//3.判断分数
	if (score > 600)           //这里没有分号
	{
		cout << "nice!" << endl;
	}
}
  1. 多行格式 if 语句
    语法:if(条件){条件满足执行的语句}
    else{条件不满足时执行的语句}
#include <iostream>
using namespace std;
#include<string>
int main() {
	int score = 0;
	cout << "请输入您的分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		cout << "nice!" << endl;
	}
	else
	{
		cout << "OH NO!" << endl;
	}
}
  1. 多条件 if 语句
    if(条件1){条件1满足执行的语句}
    else if(条件2){条件2满足执行的语句}
    else if(条件n){条件n满足执行的语句}

    else{都不满足执行的语句}
  2. 嵌套 if 语句
    案例需求:提示用户输入一个分数(0-100),根据分数分为ABC等级,A中95分以上(perfect),90-94(nice)。
#include <iostream>
using namespace std;
#include<string>
int main() {
	int score = 0;
	cout << "请输入您的分数(0-100)" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score >= 90)
	{
		cout << "您的成绩为:" << "A" << endl;
		if (score >= 95)
		{
			cout << "PERFECT!" << endl;
		}
		else
		{
			cout << "NICE!" << endl;
		}
	}
	else if (score >= 80)
	{
		cout << "您的成绩为:" << "B" << endl;
	}
	else
	{
		cout << "OH NO!" << endl;
	}
}

在这里插入图片描述

4.1.2 三目运算符 实现简单的判断

语法:表达式1 ? 表达式2 : 表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
如果表达式1的值为假,执行表达式3,并返回表达式3的结果。

#include <iostream>
using namespace std;
#include<string>
int main() {
	int a = 10;
	int b = 20;
	int c = 0;
	c = (a > b ? a : b);   //将ab作比较,将变量大的值赋给c
	(a > b ? a : b) = 100; //在C++中,三目运算符返回的是变量,可以继续赋值,将输出b=100
}

4.1.3 switch 执行多条件分支语句

语法:
switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;

default:执行语句;break;
}

#include <iostream>
using namespace std;
#include<string>
int main() {
	cout << "请输入您的分数(0-10)" << endl;
	int score = 0;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	switch (score)
	{
	case 10:
		cout << "Pecfect!" << endl;
		break;//退出当前分支
	case 9:
		cout << "Nice" << endl;
		break;
	default:
		cout << "ohohoh" << endl;
		// if和switch的区别
		// switch只能表示整形或者字符型,不可以是一个区间
		// switch结构清晰,执行效率高
	}
}

在这里插入图片描述

4.2 循环结构

4.2.1 while 循环语句

语法:while(循环条件){循环语句}
解释:只要循环条件的结果为真,就执行循环语句

#include <iostream>
using namespace std;
#include<string>
int main() {
	//在屏幕中打印0-9这个10个数字
	int num = 0;
	while (num < 10)
	{
		cout << num << endl;
		num++;
	}
}

游戏练习,猜数字,猜错了会扣分(1-100)
记得加上#include<stdlib.h>

#include <iostream>
using namespace std;
#include <string>
#include <stdlib.h>
#include <ctime>
int main()
{
	srand((unsigned int)time(NULL));
	//添加随机数种子,利用系统当前时间生成随机数,避免每次随机数都一样
	int num = rand() % 100 + 1; //生成一个 0+1 ~ 99+1 的随机数
	int val = 0;
	int score = 10;
	cout << "Q: 有一个0~100的随机数字,猜猜看是多少?" << endl;
	while (1) //true
	{
		cin >> val;//玩家进行猜测
		if (val > num)
		{
			cout << "数字过大^o^" << endl;
			score = score - 1;
			cout << "您的当前成绩为:" << score << endl;
		}
		else if (val < num)
		{
			cout << "数字过小^_^" << endl;
			score = score - 1;
			cout << "您的当前成绩为:" << score << endl;
		}
		else
		{
			cout << "猜对了,游戏结束!正确数字为:" << num << endl;
			cout << "您的最终成绩为:" << score << endl;
			break; //可以利用break关键字来退出当前循环
		}
	}
	system("pause");
	return 0;
}

4.2.2 do while 循环语句

语法:do{循环语句} while(循环条件);
区别:dowhile会先执行一次循环语句,在判断循环条件

#include <iostream>
using namespace std;
#include <string>
int main() {
	int num = 0;
	do
	{
		cout << num << endl;
		num++;
	} while (num < 10);
}

4.2.3 for for for 循环(总用)

语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}

#include <iostream>
using namespace std;
#include <string>
int main() {
	//从数字0 打印到数字9
	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}
}
#include <iostream>
using namespace std;
#include <string>
int main() {
	int i = 0;
	for (; ;)
	{
		if (i >= 10)
		{
			break;
		}
		cout << i << endl;
		i++;
	}
}

案例练习 敲七

#include <iostream>
using namespace std;
#include <string>
int main() {
	//1.先输出1~100这些数字
	for (int i = 1; i <= 100; i++)
	{
		//2.找出特殊数字,改为”敲桌子”
		if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
			//(1)7的倍数:7、14、21 % 7 = 0
			//(2)个位是7:7、17、27 % 10 = 7
			//(3)十位是7:70、71、72 / 10 = 7
		{
			cout << "BOOM 敲桌子(" << i << ")" << endl;
		}
		else //如果不是特殊数字,正常打印数字
		{
			cout << i << endl;
		}
	}
}

4.2.4 嵌套循环

#include <iostream>
using namespace std;
#include <string>
int main() {
	//利用循环实现星图
	for (int i = 0; i < 10; i++)  //外层循环,外层执行一次,内层执行一周
	{
		for (int j = 0; j < 10; j++) //内层循环
		{
			cout << "* ";
		}
		cout << endl;
	}
}

案例练习 乘法口诀表

#include <iostream>
using namespace std;
#include <string>
int main() {
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << " * " << i << " = " << j * i << "  ";

		}
		cout << endl;
	}
}

4.3 跳转语句

4.3.1 break 语句

  1. 出现在switch条件语句中,作用是停止case并跳出switch
#include <iostream>
	using namespace std;
	int main() {
		cout << "请选择副本难度" << endl;
		cout << "1. 普通" << endl;
		cout << "2. 中等" << endl;
		cout << "3. 困难" << endl;
		int select = 0; //创建选择结果的变量
		cin >> select; //等待用户输入
		switch (select)
		{
		case 1:
			cout << "您选择的是普通难度" << endl;
			break;
		case 2:
			cout << "您选择的是中等难度" << endl;
			break;
		case 3:
			cout << "您选择的是困难难度" << endl;
			break;
		default:
			break;
		}
		system("pause");
		return 0;
	}
  1. 出现在循环语句
    作用是跳出当前的循环语句。
  2. 出现在嵌套循环
    跳出最近的内层循环语句。

4.3.2 continue 语句

作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环。

#include <iostream>
	using namespace std;
	int main() {
		for (int i = 0; i <= 9; i++)
		{
			if (i % 2 == 0) //只输出基数
			{
				continue; //可以筛选条件,执行到此就不再向下执行,执行下一次循环
			}
			cout << i << endl;
		}
	}

4.3.3 goto 语句

作用:无条件跳转语句
语法:goto 标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
goto FLAG;
FLAG:

5 数组

5.1 数组概述

一个集合,里边存放了相同类型的数据元素
特点1:数组中每一个元素,数据类型相同
特点2:数组是由连续的内存位置组成的

5.2 一维数组

5.2.1 一维数组的定义方式*3

  1. 数据类型 数组名[数组长度]
  2. 数据类型 数组名[数组长度] = {值1、值2、…}
  3. 数据类型 数组名[ ] = {1、2、…}
    在这里插入图片描述
    [0123]是下标,可以通过下标访问数组的元素
// 方式一
#include <iostream>
	using namespace std;
	int main() {
		int arr[3];
		arr[0] = 1; //数组元素的下标是从0开始索引的
		arr[1] = 10;
		arr[2] = 20;
		cout << arr[1] << endl;
	}
// 方式二
#include <iostream>
	using namespace std;
	int main() {
		int arr2[3] = { 1,10,20 };
		for (int i = 0; i < 3; i++)
		{
			cout << arr2[i] << endl;
		}
	}
// 方式三 定义的时候必须有一个初始长度,[]内可不写
#include <iostream>
	using namespace std;
	int main() {
		int arr3[] = { 2, 4, 6, 8 };
		for (int i = 0; i < 3; i++)
		{
			cout << arr3[i] << endl;
		}
	}

5.2.2 一维数组名

用途:

  1. 可用来统计整个数组在内存中的长度sizeof(arr[2]);
  2. 可以获取内存中的首地址编号
cout << (int)arr << endl;
cout << (int)&arr[0] << endl;

案例练习 找出最大的值

#include <iostream>
	using namespace std;
	int main() {
		// 访问数组中的每一个元素,如果这个元素比我认定的最大值要大,更新最大值。
		int arr[] = { 100,200,300,400,500 };
		int max = 0;
		for (int i = 0; i < 5; i++)
		{
			if (arr[i] > max)
			{
				max = arr[i];
			}
		}
		cout << "max = " << max << endl;
	}

5.2.3 冒泡排序,

对数组内元素进行排序

#include <iostream>
	using namespace std;
	int main() {
		int arr[9] = { 4, 2, 8, 0, 5, 7, 1, 3, 9 };
		cout << "排序前: " << endl;
		for (int i = 0; i < 9; i++)
		{
			cout << arr[i] << " ";
		}
		cout << endl;
		//开始冒泡排序,总排序轮数为 元素个数-1
		for (int i = 0; i < 9 - 1; i++)
		{
			//内层循环对比,次数=元素个数-当前论述-1
			for (int j = 0; j < 9 - i - 1; j++)
			{
				//如果第一个数字比第二个数字大,交换两个数字
				if (arr[j] > arr[j + 1])
				{
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
		//排序后
		cout << "排序后: " << endl;
		for (int i = 0; i < 9; i++)
		{
			cout << arr[i] << " ";
		}
		cout << endl;
		system("pause");
		return 0;
	}

5.3 二维数组

5.3.1 二维数组定义的四种方式

  1. 数据类型 数组名[行数][列数];
  2. 数据类型 数组名[行数][列数] = {{数据1,数据2},{数据3,数据4}};
  3. 数据类型 数组名[行数][列数] = {数据1,数据2,数据3,数据4};
  4. 数据类型 数组名[ ][列数] = {数据1,数据2,数据3,数据4};
    在这里插入图片描述
// 方式一
#include <iostream>
	using namespace std;
	int main() {
		int arr[2][3];
		arr[0][0] = 1;	arr[0][1] = 2;	arr[0][2] = 3;	arr[0][3] = 4;
		arr[1][0] = 5;	arr[1][1] = 6;	arr[1][2] = 7;	arr[1][3] = 8;
		cout << arr[0][0] << endl;	cout << arr[0][1] << endl;
		cout << arr[0][2] << endl;	cout << arr[0][3] << endl;
		cout << arr[1][0] << endl;	cout << arr[1][1] << endl;
		cout << arr[1][2] << endl;	cout << arr[1][3] << endl;
		system("pause");
		return 0;
	}
// 方式二
#include <iostream>
using namespace std;
	int main() {
		int arr[2][3] =
		{
			{1,2,3},
			{4,5,6}
		};
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				cout << arr[i][j] << " ";
			}
			cout << endl;
		}
	}
// 方式三
	int arr[2][3] = { 1,2,3,4,5,6 };
// 方式四
	int arr[ ][3] = { 1,2,3,4,5,6 };

5.3.2 二维数组的数组名

作用:查看二维数组所占空间,获取二维数组首地址 sizeof(arr) (int)arr

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值