C++流程控制:从条件到循环全解析
·
2.程序流程结构
2.1选择结构-if条件语句
| if单行条件语句 | if(条件语句){条件满足执行语句} |
| if多行条件语句 |
if(条件语句){条件满足执行语句} else{条件不满足执行语句} |
| if多条件语句 |
if(条件语句){条件满足执行语句} else if(条件语句){条件满足执行语句} else if(条件语句){条件满足执行语句} else{条件不满足执行语句} |
| 嵌套if语句 |
if(条件语句) { if(条件语句){条件满足执行语句} else if(条件语句){条件满足执行语句} } else if(条件语句){条件满足执行语句} else{条件不满足执行语句} |
if多行条件语句:
/*描述
体重指数(BMI)是世界卫生组织(WHO)推荐国际统一使用的肥胖分型标准,即BMI=体重/身高2(kg/m2)。小于 18.5 属于"偏瘦",大于等于 18.5 小于 20.9 属于"苗条",大于等于 20.9 小于等于 24.9 属于"适中",超过 24.9 属于"偏胖"。下面由你来编写一段逻辑,输入用户的身高和体重,计算出对应的体重指数,并返回他们的身材状态。
输入描述:
用户的身高(m)和用户的体重(kg)
输出描述:
体重指数对应的身材状态:偏瘦,苗条,适中,偏胖。*/
#include <iostream>
using namespace std;
int main() {
double weight;
double height;
cin >> weight;
cin >> height;
// write your code here......
double BMI;
BMI=weight/(height*height);
if(BMI<18.5){
cout<<"偏瘦"<<endl;
}else if(BMI<20.9&&BMI>=18.5)
{
cout<<"苗条"<<endl;
}else if(BMI<=24.9&&BMI>=20.9)
{
cout<<"适中"<<endl;
}else{
cout<<"偏胖"<<endl;
}
return 0;
}
if多条件语句:
/#include<iostream>
using namespace std;
int main() {
int a;
cout << "输入你的分数" << endl;
cin >> a;
cout << "你的分数为" << a << endl;
if (a > 700) {
cout << "能上清华" << endl;
}
else if (a > 500) {
cout << "能上一本" << endl;
}
else if (a>400) {
cout << "能上二本" << endl;
}
else {
cout << "不能上大学" << endl;
}
}
嵌套if语句:
/*描述
牛牛商场促销活动:
满100打9折;
满500打8折;
满2000打7折;
满5000打6折
牛阿姨算不清楚自己应该付多少钱,请你帮忙算一下
输入描述:
牛阿姨购买商品打折前的总金额
输出描述:
参加活动后,牛阿姨购买商品应付金额。*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double price;
cin >> price;
double cost = 0.0;
if(price>=100){
cost=price*0.9;
if(price>=500){
cost=price*0.8;
if(price>=2000){
cost=price*0.7;
if(price>=5000){
cost=price*0.6;
}
}
}
}
cout << setiosflags(ios::fixed) << setprecision(1) << cost << endl;
return 0;
}
2.2选择结构-Switch语句
作用:执行多条件分支语句
语法:switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
...
default:执行语句;break;
}
#include<iostream>
using namespace std;
int main() {
cout << "给电影打分" << endl;
int a;
cin >> a;
switch (a) {
case 10:
cout << "你认为是经典电影" << endl; break;//break退出当前分支
case 9:
cout << "你认为电影不错" << endl; break;
case 8:
cout << "你认为电影不错" << endl; break;
case 7:
cout << "你认为电影一般" << endl; break;
case 6:
cout << "你认为电影一般" << endl; break;
default:
cout << "你认为电影不好看" << endl; break;
}
return 0;
}
2.3if和switch的区别
switch判断时只能是整型或者字符型,不能是一个区间。
switch比if执行效率更高
2.4循环结构-while结构
- 作用:满足循环条件,执行循环语句
- 语法:while(循环条件){循环语句}
- 只要循环条件结果为真,就执行循环语句
//使用while循环打印0~20
#include<iostream>
using namespace std;
int main() {
int a=0;
while (a < 20) {
cout << a << endl;
a++;
}
return 0;
}
2.4.1do while循环语句
- 语法:do{循环语句}while(循环条件);
- 注意:do while与while语句的区别在于,do while会先执行一次循环语句在判断循环条件
//用do while 循环打印0~20
#include<iostream>
using namespace std;
int main() {
int a = 0;
do {
cout << a << endl;
a++;
} while (a < 20);
return 0;
}
2.5循环结构-for循环
- 语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}
//用for循环打印数字0~20
#include<iostream>
using namespace std;
int main() {
for (int a = 0; a < 20; a++) {
cout << a << endl;
}
return 0;
}

2.6嵌套循环
使用for循环打印10*10的*阵:
#include<iostream>
using namespace std;
int main() {
for (int i=0; i < 10; i++)
{
for (int j = 0; j < 10; j++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
使用while循环打印10*10d 的*阵:
#include<iostream>
using namespace std;
int main() {
int i = 0;
while (i < 10) {
int j = 0;
while (j < 10) {
cout << "* ";
j++;
}
cout << endl;
i++;
}
return 0;
}
利用for循环打印九九乘法表:
#include<iostream>
using namespace std;
int main()
{
//i=9,j=9
//列数*行数
//列数<=当前行数
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++) {
cout << j << "*" << i << "=" << j * i << " \t";
}cout << endl;
}
return 0;
}
2.7跳转语句-break语句
- 作用:用于跳出选择结构或者循环结构
使用:出现在switch语句中,终止case并跳出switch
出现在循环语句中,并跳出当前循环语句
出现在嵌套循环中,并跳出最近的内存循环语句
更多推荐

所有评论(0)