#include
using namespace std;
int main() {
// 逻辑运算符 - 与 - &&
int a = 10;
int b = 11;
cout << a << endl;
cout << b << endl;
system("pause");
return 0;
}

#include
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;
}

同真为真,其余为假。
#include
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;
}

同假为假,其余为真。
支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构。
作用:执行满足条件的语句。
if语句的三种形式:
if(条件){ 条件满足执行的语句 }#include
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(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 }#include
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;
}
if(条件1){ 条件1满足执行的语句 }else if{ 条件2满足执行的语句 }...else{ 不满足前面所有条件需要执行的语句 }#include
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;
}
嵌套if语句:在if语句中,可以嵌套使用if语句,达到更精确的条件判断。
案例需求:
#include
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;
}