3.程序流程结构
三种结构:顺序结构、选择结构、循环结构
3.1选择结构
3.1.1 if语句
if语句的三种形式:
单行格式if语句;多行格式的if语句;多条件的if语句
(1)单行格式的if语句(满足条件就会执行)
示例:
#include <iostream>
using namespace std;
int main()
{
//用户输入一个数字,如果数字大于100,则在屏幕上输出输入的数字
int number = 0;
cout << "请输入一个数字:" << endl;
cin >> number;
if(number>100){
cout << "您输入的数字是:" << number << endl;
}
system("pause");
return 0;
}
(2)多行格式if语句
格式:if(条件){条件满足执行的语句} else{条件不满足执行的语句};
示例:
#include <iostream>
using namespace std;
int main()
{
//用户输入一个数字,如果数字大于100,则在屏幕上输出输入的数字,否则输出“您输入的数字过小”
int number = 0;
cout << "请输入一个数字:" << endl;
cin >> number;
if(number>100){
cout << "您输入的数字是:" << number << endl;
}
else{
cout << "您输入的数字过小" << endl;
}
system("pause");
return 0;
}
(3)多条件的if语句
格式:if(条件1){条件1满足执行的语句} else if(条件2){条件2满足执行的语句} ..........else{都不满足执行的语句}
示例:
#include <iostream>
using namespace std;
int main()
{
//用户输入一个分数,如果数字大于等于80,则在屏幕上输出“恭喜您取得优异成绩”,如果大于60小于80,输出“恭喜成绩还不错”
//如果等于六十,输出“恭喜您没挂科”,否则输出“恭喜您喜提挂科”
int number = 0;
cout << "请输入一个分数:" << endl;
cin >> number;
if(number>= 80){
cout << "恭喜您取得优异成绩" << endl;
}
else if(60 <number < 80){
cout << "恭喜成绩还不错" << endl;
}
else if (number == 60) {
cout << "恭喜您没挂科" << endl;
}
else{
cout << "恭喜您喜提挂科" << endl;
}
system("pause");
return 0;
}
嵌套if语句:在if语句中,可以使用嵌套if语句,达到更精准的条件判断
示例:
#include <iostream>
using namespace std;
int main()
{
//用户输入一个分数,如果数字大于等于80,则在屏幕上输出“恭喜您取得优异成绩”,如果大于60小于80,输出“恭喜成绩还不错”
//如果等于六十,输出“恭喜您没挂科”,否则输出“恭喜您喜提挂科”
//如果在大于80分中,又等于100,那么输出“恭喜您获得了满分,下学期奖学金有你的份,”
int number = 0;
cout << "请输入一个分数:" << endl;
cin >> number;
if(number>= 80){
cout << "恭喜您取得优异成绩" << endl;
if (number == 100) {
cout << "恭喜您获得了满分,下学期奖学金有你的份" << endl;
}
}
else if(60 <number < 80){
cout << "恭喜成绩还不错" << endl;
}
else if (number == 60) {
cout << "恭喜您没挂科" << endl;
}
else{
cout << "恭喜您喜提挂科" << endl;
}
system("pause");
return 0;
}
3.1.2 三目运算符
语法:表达式1 ?表达式2 :表达式3
解释:
如果表达式1的值返回为真,执行表达式2,并返回表达式2的结果;
如果表达式1的值返回为假,执行表达式3,并返回表达式3的结果;
示例:
#include <iostream>
using namespace std;
int main()
{
//比较a和b,将最大的值赋值给c
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
cout << "c=" << c <<endl;
//在C++中三目运算符返回的是变量,还可以继续赋值
(a > b ? a : b) = 30;
cout << "a=" << a<<endl;
cout << "b=" << b<<endl;
/*结果就是:
a = 10; b = 30;*/
system("pause");
return 0;
}
3.1.3 switch语句
作用:用于多条件分支语句
示例:
#include <iostream>
using namespace std;
int main()
{
//用户输入一个分数,如果等于80,则在屏幕上输出“恭喜您取得优异成绩”,如果等于60,输出“恭喜您没挂科”,如果等于40,输出“恭喜您喜提挂科”
int number = 0;
cout << "请输入一个分数:" << endl;
cin >> number;
switch (number) {
case 80:
cout << "恭喜您取得优异成绩" << endl;
break;
case 60:
cout << "恭喜您没挂科" << endl;
break;
case 40:
cout << "恭喜您喜提挂科" << endl;
break;
}
system("pause");
return 0;
}
3.2循环结构
3.2.1.while循环结构
作用:满足循环条件,并执行循环语句
语法:while(循环条件){循环语句}
示例:
#include <iostream>
using namespace std;
int main()
{
//添加随机数种子,利用当前系统时间生成随机数,避免每次生成的随机数都一样
srand((unsigned int)time(NULL));
//系统生成一个随机数,玩家进行猜测,系统判断玩家的猜测,猜对游戏结束,猜错游戏继续,并且提示玩家猜的结果是过大还是过小
int num = rand() % 100 + 1;//系统生成1~100的随机数
int val = 0;//玩家猜测的数据
while (1){
cin >> val;
if (val > num) {
cout << "猜测过大" << endl;
}
else if (val < num) {
cout << "猜测过小" << endl;
}
else
{
cout << "恭喜您猜对了" << endl;
break;
}
}
system("pause");
return 0;
}
3.2.2 do....while循环语句
作用:满足循环条件,执行循环语句
语法:do {循环语句}while(循环条件);
示例:
#include <iostream>
using namespace std;
int main()
{
//在屏幕中输入0到9的这10个数字
int num = 0;
do {
cout << num << endl;
num++;
} while (num < 10);
system("pause");
return 0;
}
案例:求出所有三位数的水仙花数:
#include <iostream>
using namespace std;
int main()
{
int num = 100;
do {
int a = 0;
int b = 0;
int c = 0;
a = num % 10;
b = num / 10 % 10;
c = num / 100;
if (a*a*a + b*b*b + c*c*c == num) {
cout << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}
3.2.3 for循环语句
作用:满足循环条件,执行循环语句
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
system("pause");
return 0;
}
3.2.4 嵌套循环语句
作用:在循环体中再嵌套一层循环
#include <iostream>
using namespace std;
int main()
{
//打印一副星图
for (int i = 0; i < 10; i++)
{
for (int i = 0; i < 10; i++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
3.3跳转语句
3.3.1 break语句
作用:跳出选择结构或者循环结构
3.3.2 continue语句
作用:在循环语句中,跳过本次循环中还没有执行的语句,继续执行下一次循环
示例:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i <= 100; i++)
{
//奇数输出,偶数不输出
if (i % 2 == 0) {
continue;
}
cout << endl;
}
system("pause");
return 0;
3.3.3 goto语句
作用:无条件跳转语句
语法:goto 标记;
解释:如果标记的名称存在吗,执行到goto语句时,会跳转到标记的位置
示例:
#include <iostream>
using namespace std;
int main()
{
cout << "1111" << endl;
cout << "2222" << endl;
goto FLAG;
cout << "3333" << endl;
cout << "4444" << endl;
cout << "5555" << endl;
cout << "6666" << endl;
FLAG:
cout << "7777" << endl;
cout << "8888" << endl;
system("pause");
return 0;
}