C++程序流程结构
1.条件语句
代码示例1(if语句)
#include<iostream>
using namespace std;
int main()
{
//程序流程结构
//顺序结构(顺序执行,不跳转)、选择结构、循环结构
//选择结构-if语句
//1.单行格式if语句:if(条件){条件满足执行语句}
//案例一:600分以上录取至一本大学
//用户输入,打印输入,判断600,打印输出
int score = 0;
cout << "请输入一个分数" << endl;
cin >> score;
cout << "您输入的分数为" << score << endl;
if (score >= 600)//该句后面不要加分号!否则内部代码不执行!
{
cout << "恭喜您被录取为一本大学" << endl;
//4.if的嵌套语句
if (score >= 700)
{
cout << "恭喜您被北大录取" << endl;
}
else if (score >= 650)
{
cout << "恭喜您被清华录取" << endl;
}
else
{
cout << "恭喜你被人大录取" << endl;
}
}
//if (score < 600)//该句后面不要加分号!否则内部代码不执行!
//{
// cout << "NO" << endl;
//}
//2.多行格式if语句
//if(条件){}
//else{}
//else
//{
// cout << "很遗憾,您未被录取一本大学" << endl;
//}
//3.多条件if语句
else if (score >= 500)
{
cout << "恭喜考上二本大学" << endl;
}
else if ( score >= 400)
{
cout << "恭喜考上三本大学" << endl;
}
else
{
cout << "很遗憾,您未考上大学本科" << endl;
}
system("pause");
return 0;
}
案例代码
#include<iostream>
using namespace std;
int main()
{
//输入a,b,c三个数,并判断最大值打印输出
cout << "请输入三个数" << endl;
double a, b, c;
cin >> a;
cin >> b;
cin >> c;
cout << "你输入的值分别为" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
if (a > b)
{
if (a > c)
{
cout << "a为最大值" << "且a=" << a << endl;
}
else
{
cout << "c为最大值" << "且c=" << c << endl;
}
}
else
{
if (b > c)
{
cout << "b为最大值" << "且b=" << b << endl;
}
else
{
cout << "c为最大值" << "且c=" << c << endl;
}
}
system("pause");
return 0;
}
代码示例2(三目运算符)
#include<iostream>
using namespace std;
int main()
{
//三目运算符
//表达式1?表达式2:表达式3
//1真。执行2 1假。执行3
//将变量ab大的值对c进行赋值
int a = 0, b = 0, c = 0; \
cout << "请输入a与b的值" << endl;
cin >> a;
cin >> b;
cout << "您输入的a="<<a<<";b="<<b << endl;
//a > b ? c = a : c = b;
c = (a > b ? a : b);
cout << "c=" << c << endl;
(a > b ? a : b) = 1000;//三目运算符返回的是变量,可继续进行赋值操作
cout << "Fin_a=" << a << endl;
cout << "Fin_b=" << b << endl;
system("pause");
return 0;
}
2.选择结构
代码示例
#include<iostream>
using namespace std;
int main()
{
//switch语句,优点是结构清晰,执行效率高;缺点只可以对整型与字符型判断,不能为一个区间
//switch(表达式)
//{
// case结果1:执行语句;break;
// ......
// default:执行语句;break;
//}
//案例,电影打分
int a;
cout << "请对电影进行评分" << endl;
cin >> a;
cout << "你的评分为" <<a<< endl;
switch (a)
{
case 10:cout << "优秀电影" << endl;
break;//退出分支
case 9:cout << "优秀电影" << endl;
break;
case 8:cout << "优秀电影" << endl;
break;
case 7:cout << "一般电影" << endl;
break;
case 6:cout << "一般电影" << endl;
break;
case 5:cout << "一般电影" << endl;
break;
default:cout << "烂电影" << endl;
break;
}
system("pause");
return 0;
}
3.循环结构
a.代码示例(whlie循环)
//while循环
//在屏幕上打印0-9这10个数字
int a = 0;
while (a<10)//注意避免死循环的出现
{
cout << a << endl;
a++;
}
案例代码
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
//系统生成随机数、玩家猜测、判断猜测、猜对or猜错
srand((unsigned int)time(NULL));//添加随机数种子,利用系统当前时间生成随机数,防止每次随机数一样
int num = rand() % 100 + 1;//生成0-99的随机数
//cout << num << endl;
int a;
int b = 0;
int c = 0;
while (b<5)//死循环可以配合break使用
{
cout << "请输入一个猜测值" << endl;
cin >> a;
b++;
if (a > num)
{
cout << "结果偏大" << endl;
c = 2;
}
else if (a < num)
{
cout << "结果偏小" << endl;
c = 2;
}
else
{
cout << "恭喜您猜对了!" << endl;
c = 0;
break;//在循环中作为退出关键字
}
}
if (c>=2)
{
cout << "游戏结束!You are Loser!" << endl;
}
else
{
cout << "游戏结束,恭喜你挑战成功!" << endl;
}
system("pause");
return 0;
}
b.代码示例(do while循环)
//do while特点即为先执行一次
int a = 0;
do
{
cout << a << endl;
a++;
} while (a < 10);//内部条件满足就循环
system("pause");
return 0;
案例代码(do while三位数内的水仙花)
int a=0, b=0, c=0, d=100;
do
{
a = d % 10;//各位:对数除十取余
b = d / 100;//百位:对数除百
c = (d / 10) % 10;//十位:对数字除十,再对数对十取余
if (d == a * a*a + b * b*b + c * c*c)
{
cout << d << endl;
}
d++;
} while (d <1000);
c.代码示例(for循环)
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
案例代码(敲桌子)
for (int i = 1; i < 100; i++)
{
int a, b, c;
a = i % 10;
b = i / 10;
c = i % 7;
if (a==7||b==7||c==0)
{
cout << "敲桌子" << endl;
}
else cout << i << endl;
}
循环嵌套案例
#include<iostream>
using namespace std;
int main()
{
循环嵌套打印星图
//for (int j = 0; j < 10; j++)
//{
//for (int i = 0; i < 10; i++)
//{
// cout << "* " ;
//}
//cout << endl;
//}
//乘法口诀表
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << (i*j)<<"\t";
}
cout << endl;
}
system("pause");
return 0;
}
4.跳转语句
a.break:常用于跳出循环
b.continue:执行到本行,不在执行接下来的语句,而重新循环,配合if语句使用将极具意义
代码示例(输出1-99中的奇数)
for (int i = 1; i < 100; i++)
{
if (i%2==0)
{
continue;
}
cout << i << endl;
}
c.goto:实际中不推荐使用
cout << "xxx" << endl;
goto flag;
cout << "xxxxx" << endl;//该句将被跳跃
flag:
cout << "xxxx "<< endl;