C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
作用: 执行满足条件的语句
if语句的三种形式
单行格式if语句:if(条件){ 条件满足执行的语句 }
示例:
#include
using namespace std;
int main() {
//选择结构-单行if语句
//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
cout << "您输入的分数为: " << score << endl;
//if语句
//注意事项,在if判断语句后面,不要加分号
if (score > 600)
{
cout << "我考上了一本大学!!!" << endl;
}
return 0;
}
注意:if条件表达式后不要加分号
if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 };
示例:
#include
using namespace std;
int main() {
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600)
{
cout << "我考上了一本大学" << endl;
}
else
{
cout << "我未考上一本大学" << endl;
}
return 0;
}
3. 多条件的if语句:if(条件1){ 条件1满足执行的语句 }else if(条件2){条件2满足执行的语句}... else{ 都不满足执行的语句}
示例:
#include
using namespace std;
int main() {
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600)
{
cout << "我考上了一本大学" << endl;
}
else if (score > 500)
{
cout << "我考上了二本大学" << endl;
}
else if (score > 400)
{
cout << "我考上了三本大学" << endl;
}
else
{
cout << "我未考上本科" << endl;
}
return 0;
}
示例:
#include
using namespace std;
int main() {
int score = 0;
cout << "请输入考试分数:" << endl;
cin >> score;
if (score > 600)
{
cout << "我考上了一本大学" << endl;
if (score > 700)
{
cout << "我考上了北大" << endl;
}
else if (score > 650)
{
cout << "我考上了清华" << endl;
}
else
{
cout << "我考上了人大" << endl;
}
}
else if (score > 500)
{
cout << "我考上了二本大学" << endl;
}
else if (score > 400)
{
cout << "我考上了三本大学" << endl;
}
else
{
cout << "我未考上本科" << endl;
}
return 0;
}
作用: 通过三目运算符实现简单的判断
语法:表达式1 ? 表达式2 :表达式3
解释:
如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
如果表达式1的值为假,执行表达式3,并返回表达式3的结果。
示例:
#include
using namespace std;
int main() {
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
cout << "c = " << c << endl;
//C++中三目运算符返回的是变量,可以继续赋值
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
return 0;
}
总结:和if语句比较,三目运算符优点是短小整洁,缺点是如果用嵌套,结构不清晰
作用: 执行多条件分支语句
语法:
switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
...
default:执行语句;break;
}
示例:
#include
using namespace std;
int main() {
//请给电影评分
//10 ~ 9 经典
// 8 ~ 7 非常好
// 6 ~ 5 一般
// 5分以下 烂片
int score = 0;
cout << "请给电影打分" << endl;
cin >> score;
switch (score)
{
case 10:
case 9:
cout << "经典" << endl;
break;
case 8:
cout << "非常好" << endl;
break;
case 7:
case 6:
cout << "一般" << endl;
break;
default:
cout << "烂片" << endl;
break;
}
return 0;
}
注意1:switch语句中表达式类型只能是整型或者字符型
注意2:case里如果没有break,那么程序会一直向下执行
总结:与if语句比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间
作用: 满足循环条件,执行循环语句
语法:while(循环条件){ 循环语句 }
解释:只要循环条件的结果为真,就执行循环语句
示例:
#include
using namespace std;
int main() {
int num = 0;
while (num < 10)
{
cout << "num = " << num << endl;
num++;
}
return 0;
}
注意:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环
作用: 满足循环条件,执行循环语句
语法: do{ 循环语句 } while(循环条件);
注意: 与while的区别在于do…while会先执行一次循环语句,再判断循环条件
示例:
#include
using namespace std;
int main() {
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
return 0;
}
总结:与while循环区别在于,do…while先执行一次循环语句,再判断循环条件
作用: 满足循环条件,执行循环语句
语法:for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
示例:
#include
using namespace std;
int main() {
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
return 0;
}
注意:for循环中的表达式,要用分号进行分隔
总结:while , do…while, for都是开发中常用的循环语句,for循环结构比较清晰,比较常用