一点一点学习C++之笔记001
22 运算符 - 逻辑运算符 - 与 - &&
#include <iostream>
using namespace std;
int main() {
// 逻辑运算符 - 与 - &&
int a = 10;
int b = 11;
cout << a << endl;
cout << b << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main() {
// 逻辑运算符 - 与 - &&
int a = 10;
int b = 11;
int c = 0;
int d = 0;
cout << a << endl;
cout << b << endl;
cout << (a && b) << endl; // 1
cout << (a && c) << endl; // 0
cout << (c && d) << endl; // 0
/* 也就是说,有假则假;全真则真。 */
system("pause");
return 0;
}
同真为真,其余为假。
23 运算符 - 逻辑运算符 - 或 - ||
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 100;
int c = 0;
int d = 0;
cout << (a || b) << endl;
cout << (a || c) << endl;
cout << (c || d) << endl;
system("pause");
return 0;
}
同假为假,其余为真。
24 程序流程结构 - 选择结构 - 单行if语句
支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构。
- 顺序结构:程序按顺序执行,不发生跳转
- 选择结构
- 循环结构
1.选择结构
1.1 if语句
作用:执行满足条件的语句。
if语句的三种形式:
- 单行格式 if 语句
- 多行格式 if 语句
- 多条件的 if 语句
- 单行格式 if 语句:
if(条件){ 条件满足执行的语句 }
#include <iostream>
using namespace std;
int main() {
// 选择结构 单行if语句
// 用户输入分数,如果分数大于600,视为考上一本大学,在屏幕上输出
// step1:用户输入分数
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
// step2:打印用户输入的分数
cout << "你输入的分数为:" << score << endl;
// step3:判断分数是否大于600,如果大于,那么进行输出
if (score > 600) {
cout << score << endl;
}
system("pause");
return 0;
}
- 注意if条件后面不要加
分号
。
25 程序流程结构 - 选择结构 - 多行if语句
- 多行格式 if 语句:
if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 }
#include <iostream>
using namespace std;
int main() {
// 选择结构 - 多行 if 语句
// 输入考试分数,如果大于600,输出“考上一本大学”;如果不大于600,输出“未考上一本大学”。
// step1:输入分数score
int score = 0;
cout << "请输入你的分数:" << endl;
cin >> score;
cout << "你的成绩是:" << score << endl;
// step2:对分数score进行判断,进行分情况输出相应结果
if (score > 600) {
// 满足前面的条件会执行的语句
cout << "恭喜你,能够选择一本大学!" << endl;
}
else {
// 不满足前面的条件会执行的语句
cout << "非常遗憾,你不能选择一本大学!" << endl;
} // 后面的分号可有可无,都可以运行,不知道有没有不同含义
system("pause");
return 0;
}
26 程序流程结构 - 选择结构 - 多条件if语句
- 多条件的 if 语句:
if(条件1){ 条件1满足执行的语句 }else if{ 条件2满足执行的语句 }...else{ 不满足前面所有条件需要执行的语句 }
#include <iostream>
using namespace std;
int main() {
// 选择结构 多条件if语句
// 输入你的语文成绩score
// 如果大于90分,视为优秀
// 如果介于80分到90分之间,视为良好
// 如果介于70分到80分之间,视为可以
// 如果介于60分到70分之间,视为及格
// 如果小于60分,视为不及格
int score = 0;
cout << "请输入你的语文成绩:" << endl;
cin >> score;
cout << "你的语文成绩为" << score << endl;
if (score > 90) {
cout << "优秀" << endl;
}
else if (score > 80) {
cout << "良好" << endl;
}
else if (score > 70) {
cout << "可以" << endl;
}
else if (score > 60) {
cout << "及格" << endl;
}
else {
cout << "不及格" << endl;
}
system("pause");
return 0;
}
27 程序流程结构 - 选择结构 - 嵌套if语句
嵌套if语句:在if语句中,可以嵌套使用if语句,达到更精确的条件判断。
案例需求:
- 提示用户输入一个语文成绩
- 根据用户的成绩进行如下判断:
- 成绩大于80分,一档;大于60分,二档;不大于60分,三档;
- 如果成绩大于90分,优秀;成绩大于80分,良好;成绩大于70分,中等。
#include <iostream>
using namespace std;
int main() {
int score = 0;
cout << "请输入你的语文成绩:"<< endl;
cin >> score;
cout << "你的语文成绩:" << score << endl;
if (score > 80) {
cout << "一档" << endl;
if (score > 90) {
cout << "优秀" << endl;
}
else {
cout << "良好" << endl;
}
}
else if (score > 60) {
cout << "二档" << endl;
if (score > 70) {
cout << "中等" << endl;
}
}
else {
cout << "三档" << endl;
}
system("pause");
return 0;
}