2.5 转义字符
用于表示一些不能显示出来的ASCII字符
#include <iostream>
using namespace std;
int main()
{
//转义字符
//换行符\n
cout << "hello world\n" ;
//反斜杠
cout << "\\" << endl;//要打两个\\才能输出一个\
//水平制表符 \t 作用:可以整齐地输出数据
cout << "aaa\thello world" << endl;
cout << "aaaaa\thello world" << endl;
cout << "a\thello world" << endl;
system("pause");
return 0;
}
?2.6 字符串型
#include <iostream>
using namespace std;
int main()
{
//1.C风格字符串
//注意事项: char 字符串名 []
//注意事项2 等号后面要用双引号
char str[] = "hello world";
cout << str << endl;
//2.c++风格字符串
string str2 = "hello world";
cout << str2 << endl;
system("pause");
return 0;
}
2.7 布尔数据类型bool
布尔数据类型代表真或假两种值
1.true——真(本质是1)
2.false——假(本质是0)
bool数据类型占1个字节大小
2.8数据的输入
用于从键盘获取数据
关键字:cin
语法:cin >> 变量
#include <iostream>
using namespace std;
int main()
{
//1.整型
int a = 0;
cout << "请输入整型变量a的值:" << endl;
cin >> a;
cout << "整型变量a = " << a << endl;
//2.浮点型
float f = 3.14f;
cout << "请给浮点型变量f赋值:" << endl;
cin >> f;
cout << "浮点型变量f = " << f << endl;
//3.字符型
char ch = 'a';
cout << "请给字符型变量ch赋值:" << endl;
cin >> ch;
cout << "字符型变量ch = " << ch << endl;
//4.字符串型
string str = "afbaob";
cout << "请给字符串变量str赋值:" << endl;
cin >> str;
cout << "字符串str = " << str<<endl;
//5.布尔类型
bool flag = true;
cout << "请给布尔类型 flag 赋值:" << endl;
cin >> flag;
cout << "布尔类型flag = " << flag << endl;
system("pause");
return 0;
}
3.运算符
3.1算数运算符
#include <iostream>
using namespace std;
int main()
{
//加减乘除
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl;
//两个整数相除,结果依然是整数,将小数部分去除
int a2 = 10;
int b2 = 20;
cout << a2 / b2 << endl;
int a3 = 10;
//int b3 = 0;
//cout << a3 / b3 << endl;
//两个数字相除,除数是不可以为0的
//两个小数可以相除
double d1 = 0.5;
double d2 = 0.22;
cout << d1 / d2 << endl;
//运算结果也可以是小数
system("pause");
return 0;
}
取模(取余)运算%
只有整型变量才可以进行取模运算
#include <iostream>
using namespace std;
int main()
{
//取模运算本质是求余数
int a1 = 10;
int b1 = 3;
cout << a1 % b1 << endl;
int a2 = 10;
int b2 = 20;
cout << a2 % b2 << endl;
int a3 = 10;
int b3 = 0;
//cout << a3 % b3 << endl;
// 两个数相除,除数不能为0,因此也作不了取模运算
//两个小数是不可以作取模运算的
double d1 = 3.14;
double d2 = 1.1;
//cout << d1 % d2 << endl;
system("pause");
return 0;
}
递增与递减
?
#include <iostream>
using namespace std;
int main()
{
//1.前置递增
int a = 10;
++a;//让变量+1
cout << "a = " << a << endl;
//2.后置递增
int b = 10;
b++;//让变量+1
cout << "b = " << b << endl;
//3.前置与后置的区别
//前置递增:先让变量+1,然后进行表达式运算
int a2 = 10;
int b2 = ++a2 * 10;
cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl;
//后置递增:先进行表达式运算,后让变量+1
int a3 = 10;
int b3 = a3++ * 10;
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;
system("pause");
return 0;
}
3.2 赋值运算符
作用:用于将表达式的值赋值给变量
?
#include <iostream>
using namespace std;
int main()
{
//赋值运算符
// =
int a = 10;
a = 100;
cout << "a = " << a << endl;
// +=
a = 10;
a += 10;
cout << "a = " << a << endl;
// -=
a = 10;
a -= 10;
cout << "a = " << a << endl;
// *=
a = 10;
a *= 10;
cout << "a =" << a << endl;
// /=
a = 10;
a /= 10;
cout << "a = " << a << endl;
// %=
a = 10;
a %= 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
3.3 比较运算符
#include <iostream>
using namespace std;
int main()
{
//比较运算符
//==
int a = 10;
int b = 20;
cout << (a == b)<< endl;
//!=
cout << (a != b) << endl;
//>
cout << (a > b) << endl;
//<
cout << (a < b) << endl;
//>=
cout << (a >= b) << endl;
//<=
cout << (a <= b) << endl;
system("pause");
return 0;
}
3.4 逻辑运算符
作用:用于根据表达式的值返回真值或者假值
?
#include <iostream>
using namespace std;
int main()
{
//逻辑运算符 非 !
int a = 10;
//在c++中,除了0都为真
cout << !a << endl;
cout << !!a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
//逻辑与&&:两个条件都为真,结果才为真
int a = 10;
int b = 10;
cout << (a && b) << endl;
a = 0;
b = 10;
cout << (a && b) << endl;
a = 0;
b = 0;
cout << (a && b) << endl;
//同真为真,其余为假
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
//逻辑或||:如果两个中有一个为真,结果为真,全为假,结果才为假
int a = 10;
int b = 10;
cout << (a || b) << endl;
a = 0;
b = 10;
cout << (a || b) << endl;
a = 0;
b = 0;
cout << (a || b) << endl;
//逻辑或,同假为假,其余为真
system("pause");
return 0;
}
4. 程序流程结构
?4.1 选择结构
4.1.1 if语句
1. 单行格式if语句
2.多行格式if语句
3.多条件if语句
#include <iostream>
using namespace std;
int main()
{
//选择结构
//用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
//1.用户输入分数
//2.打印用户输入的分数
//3.判断分数是否大于100,如果大于,考上一本,否则,没考上一本
int score;
cout << "请输入考试分数:" << endl;
cin >> score;
cout << "考试分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜您考上一本大学" << endl;
}
else {
cout << "很遗憾您没考上一本大学" << endl;
}
system("pause");
return 0;
}
|