前言:语法很重要,注意一些符号的中英文输入是不同的,即使看着一样编译器读出来的也不一样。
最基本的三种程序运行结构:顺序结构、选择结构、循环结构
- 顺序结构:程序按顺序执行
- 选择结构:依据条件是否满足,有选择的执行相应功能
- 循环结构:依据条件是否满足,循环多次执行某段代码
if语句
作用:执行满足条件的语句
if语句可以进行嵌套使用,达到更加精确的条件判断
if语句的三种形式:
- 单行格式if语句 if(条件){条件满足执行};
- 多行格式if语句
if(条件){满足执行}else{不满足执行}; - 多条件的if语句 if(条件1){满足执行}else if(条件2){满足条件2执行}...else;{都不满足执行}
三目运算符
C++中三目运算符返回的是变量可以继续赋值
作用:实现简单的判断
语法:表达式1?表达式2;表达式3
解释:
- 如果表的是1的值为真,执行表达式2,并返回表达式2的结果;
- 如果表达式1的值为假,执行表达式3,并返回表达式3的结果;
switch语句 作用:执行多条件分支语句 语法: switch(表达式) {case结果1;执行语句;break; case结果2;执行语句;break; ... .;........;.....break; default;执行语句;break;}
while语句
注意避免死循环
在执行循环语句的时候,提供跳出循环的出口
作用:满足循环条件执行循环语句
语法:while(循环条件){循环语句}
解释:如果循环条件为真,执行循环语句
do...while循环语句
作用:满足循环条件,执行循环语句
语法:do{循环语句}while{循环条件};
与while的区别在于do...while会先执行一次循环语句,再判断循环条件
for循环
语法:for(起始表达式;条件表达式;末尾循环体){循环语句}
作用:满足循环条件,执行循环语句
嵌套循环
解释:在循环中在嵌套一层循环,解决实际问题
break语句
作用:用于跳出选择结构或者循环结构
- 出现在switch条件语句中,终止case并跳出switch
- 出现在循环语句中,跳出当前循环语句
- 出现在嵌套循环中,跳出内层循环语句
continue语句
作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
goto语句
作用:无条件跳转语句
语法:goto标记; 标记:
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
一维数组定义方式:
数据类型 数组名[数组长度];
数据类型 数组名[数组长度]={值1,值2.....};
数据类型 数组名[]={值1,值2....};
二维数组
函数
语法:返回值类型 函数名(参数列表){函数体语句 return表达式}
值传递
函数的常见样式:
- 无参无返
- 有参无返
- 无参有返
- 有参有返
函数可以分文件编写,一般有四个步骤。
- 创建.h后缀的头文件
- 创建.cpp后缀的源文件
- 函数声明写头文件里
- 函数定义写源文件里 **在源文件的头部写#include"头文件名",还要声明函数 指针
- 作用:通过指针可以间接访问内存
- 定义:一个变量的地址
- 语法:数据类型*变量名 指针前加*表示解引用,指指针指向的内存的数据 空指针:指向内存编号为0的指针,可以初始化指针变量。 空指针指向的内存无法访问。
const修饰指针的三种情况
-
const修饰指针---常量指针 指针的指向可以改变,指针指向的值不可以改变
-
const修饰常量---指针常量
指针的指向不可以改变,指针指向的值可以改变 -
const即修饰指针又修饰常量
指针的指向不可以改变,指针指向的值也不可以改变
结构体
概念:属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:strut 结构体名{结构体成员列表};
通过结构体创建变量的方式有三种:
- struct结构体名 变量名
- struct结构体名 变量名={成员1值,成员2值...}
- 定义结构体时顺便创建变量
#include<iostream>
using namespace std;
#include<string>;
struct student//创建结构体变量
{ //创建学生数据类型
string name;//名字
int age;//年龄
int secore;//分数
}s3;
int main()
{
student s1;
s1.name = "张三";
s1.age = 18;
s1.secore = 70;
cout << "name:" << s1.name << "age: " << s1.age << "secore:" << s1.secore << endl;
student s2 = { "李四",18,80 };
cout << "name: " << s2.name << "age:" << s2.age << "secore:" << s2.secore << endl;
s3.name = "王五";
s3.age = 18;
s3.secore;
cout << "name: " << s3.name << "age:" << s3.age << "secore:" << s3.secore << endl;
return 0;
}
C++
结构体数组
作用:将自定义的结构体放入到数组中方便维护
语法:struct 结构体名 数组名[元素个数]={ {},{},...{}}
#include <iostream>
using namespace std;
#include <string>;
struct student//创建结构体
{
string name;//名字
int age;//年龄
int secore;//分数
};
int main()
{
student stuarry[3]=
{
{"张三",18,70},
{"李四",18,60},
{"王五", 18, 90}
};
for (int i = 0;i < 3;i++)
{
cout << "姓名:" << stuarry[i].name << endl << "年龄:" << stuarry[i].age << endl << "分数:" << stuarry[i].secore << endl;
}
return 0;
}
C++
当我们加入以下代码时
stuarry[1].name = "赵六";
stuarry[1].age = 20;
stuarry[1].secore = 100;
``` C++
程序变为
```#include <iostream>
using namespace std;
#include <string>;
struct student//创建结构体
{
string name;//名字
int age;//年龄
int secore;//分数
};
int main()
{
student stuarry[3]=
{
{"张三",18,70},
{"李四",18,60},
{"王五", 18, 90}
};
stuarry[1].name = "赵六";
stuarry[1].age = 20;
stuarry[1].secore = 100;
for (int i = 0;i < 3;i++)
{
cout << "姓名:" << stuarry[i].name << endl << "年龄:" << stuarry[i].age << endl << "分数:" << stuarry[i].secore << endl;
}
return 0;
}
C++
第二个人的数据发生了改变
对比上面的代码可以看出结构体数组更加清晰直观方便维护
结构体指针
作用:通过指针访问结构体中的成员
- 利用操作符->可以通过结构体指针访问结构体成员属性
#include <iostream>
using namespace std;
#include<string>;
struct student//创建结构体变量
{
string name;//姓名
int age;//年龄
int secore;//分数
};
int main()
{
struct student stud;
stud.name = "张三";
stud.age = 18;
stud.secore = 70;
student* p = &stud;
cout <<"姓名:" << p->name << endl;
cout << "年龄:" << p->age << endl;
cout <<"分数:" << p->secore << endl;
}
C++
结构体嵌套结构体
作用:结构体的成员可以是另一个结构体
using namespace std;
#include<string>;
struct student
{
string name;
int age;
int secore;
};
struct teacher
{
int id;
string name;
int age;
student student;
};
int main()
{
teacher t;
t.id = 2021103134;
t.name = "老王";
t.age = 40;
t.student.name="张三";
t.student.age=18;
t.student.secore=80;
return 0;
}
C++
结构体做函数参数
作用:将结构体作为参数向函数中传递
传递方式:
- 值传递
- 地址传递
using namespace std;
#include<string>;
struct stu
{
string name;
int age;
int secore;
};
void print1( stu s)
{
s.age = 60;
cout << "子函数1:" << endl;
cout << "名字:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "成绩:" << s.secore << endl;
};
void print2( stu* p)
{
p->name = "王五";
cout << "子函数2:" << endl;
cout << "名字:" << p->name << endl;
cout << "年龄:" << p->age << endl;
cout << "成绩:" << p->secore << endl;
}
int main()
{
stu s;
s.name = "张三";
s.age = 20;
s.secore = 60;
print1(s);
print2(&s);
cout << "主函数:" <<endl<< s.name << endl << s.age << endl << s.secore << endl;
return 0;
}
C++
再一次说明函数的值传递形参发生改变实参不发生改变,利用指针的地址传递形参发生改变实参也会改变
结构体中const的使用场景
作用:用const来防止误触
void print(const student*p)
{
p->age = 18;
cout << "姓名:" << p->name << " " << endl;
cout << "年龄:" << p->age<< " " << endl;
cout << "分数:" << p->secore<< " " << endl;
}
C++
想要修改结构体的值就会报错,用来防止误触