程序流程结构1
1.顺序结构
1.选择结构
(1)if语句
if语句的三种形式:单行、多行、多条件
单行:if(条件){条件满足执行的语句}
#include<iostream>
using namespace std;
int main() {
//选择结构 单行
//输入数字比较是否满足条件
int flower = 0;
cout << "鲜花的数量:" << endl;
cin >> flower;
cout << "现有鲜花的数量为:" << flower << endl;
if (flower > 8) {
cout << "一束鲜花" << endl;
}
system("pause");
return 0;
}
多行:
if(条件){条件满足执行的语句}else{条件不满足时执行的语句}
#include<iostream>
using namespace std;
int main() {
int flower = 0;
cout << "鲜花的数量:" << endl;
cin >> flower;
cout << "现有鲜花的数量为:" << flower << endl;
if (flower > 8) {
cout << "一束鲜花" << endl;
}
else {
cout << "几朵鲜花" << endl;
}
system("pause");
return 0;
}
多条件语句:
if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句}…else{都不满足执行的语句}
#include<iostream>
using namespace std;
int main() {
int flower = 0;
cout << "鲜花的数量:" << endl;
cin >> flower;
cout << "现有鲜花的数量为:" << flower << endl;
if (flower > 50) {
cout << "一捧鲜花" << endl;
}
else if(flower >8 ){
cout << "一束鲜花" << endl;
}
else if(flower > 0){
cout << "几朵鲜花" << endl;
}
else {
cout << "没有花" << endl;
}
system("pause");
return 0;
}
(2)嵌套if语句
#include<iostream>
using namespace std;
int main() {
int flower = 0;
cout << "鲜花的数量:" << endl;
cin >> flower;
cout << "现有鲜花的数量为:" << flower << endl;
if (flower > 50) {
cout << "有很多花花" << endl;
if (flower > 10000) {
cout << "花园" << endl;
}
else {
cout << "一捧鲜花" << endl;
}
}
else if(flower >8 ){
cout << "一束鲜花" << endl;
}
else if(flower > 0){
cout << "几朵鲜花" << endl;
}
else {
cout << "没有花" << endl;
}
system("pause");
return 0;
}
举例:三个数字比较
#include<iostream>
using namespace std;
int main() {
int flower1 = 0;
int flower2 = 0;
int flower3 = 0;
cout << "鲜花1的数量:" << endl;
cin >> flower1;
cout << "鲜花2的数量:" << endl;
cin >> flower2;
cout << "鲜花3的数量:" << endl;
cin >> flower3;
cout << "现有鲜花1的数量为:" << flower1 << endl;
cout << "现有鲜花2的数量为:" << flower2 << endl;
cout << "现有鲜花3的数量为:" << flower3 << endl;
if (flower1 > flower2) {
if (flower1 > flower3) {
cout << "鲜花1最多" << endl;
}else {
cout << "鲜花3最多" << endl;
}
}
else if(flower2 > flower1) {
if (flower2 > flower3) {
cout << "鲜花2最多" << endl;
}
else {
cout << "鲜花3最多" << endl;
}
}
system("pause");
return 0;
}
(3)三目运算符
表达式1 ?表达式2:表达式3
#include<iostream>
using namespace std;
int main() {
int flower1 = 0;
int flower2 = 0;
int flower3 = 0;
cout << "鲜花1的数量:" << endl;
cin >> flower1;
cout << "鲜花2的数量:" << endl;
cin >> flower2;
cout << "鲜花3的数量:" << endl;
cin >> flower3;
cout << "现有鲜花1的数量为:" << flower1 << endl;
cout << "现有鲜花2的数量为:" << flower2 << endl;
cout << "现有鲜花3的数量为:" << flower3 << endl;
int flower = (flower1 > flower2 ? flower1 : flower2);
cout << "现有鲜花的数量为:" << flower << endl;
system("pause");
return 0;
}
(4)switch语句
作用:执行多条分支语句
switch比if执行效率高,优先选择。
表达式类型只能是整形或者字符型
case中没有break会一直向下运行
switch不能判断区间
#include<iostream>
using namespace std;
int main() {
//switch语句
//鲜花的种类
//1.玫瑰花、2.牡丹花、3.君子兰、4.满天星
cout << "请输入鲜花种类数目" << endl;
int num = 0;
cin >> num;
cout << "鲜花的种类为: " << num << endl;
switch (num)
{
case 1:
cout << "玫瑰花" << endl;
break;
case 2:
cout << "牡丹花" << endl;
break;
case 3:
cout << "君子兰" << endl;
break;
case 4:
cout << "满天星" << endl;
break;
default:
cout << "没有此种类的花" << endl;
break;
}
system("pause");
return 0;
}