c++流程结构

  1. 选择结构
#include <iostream>
using namespace std;
int main01()
{
	//顺序结构
	//选择结构
	
	//①单行if语句
	//用户输入分数,如果分数大于600,视为考上一本,在屏幕上输出
	/*int score = 0;
	cout << "请输入一个分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)//if后面不要加分号
	{
		cout << "恭喜您考上一本大学" << endl;
	}
	
	//②多行if语句
	//用户输入分数,如果分数大于600,视为考上一本,在屏幕上输出
	//如果没考上一本大学,打印未考上一本大学
	int score1 = 0;
	cout << "请输入一个考试分数:" << endl;
	cin >> score1;
	cout<< "您输入的分数为:" << score1 << endl;
	if (score1 > 600)
	{
		cout << "恭喜您考上一本大学" << endl;
	}
	else
	{
		cout << "未考上一本大学" << endl;
	}
	
	//③多条件if语句
	//大于600视为一本大学,大于500视为考上二本大学,大于400视为考上三本大学
	//小于等于400视为未考上本科生
	int score2 = 0;
	cout << "请输入一个考试分数:" << endl;
	cin >> score2;
	cout << "您输入的分数为:" << score2 << endl;
	if (score2 > 600)
	{
		cout << "恭喜您考上一本大学" << endl;
	}
	else if(score2 > 500)
	{
		cout << "考上二本大学" << endl;
	}
	else if (score2 > 400)
	{
		cout << "考上三本大学" << endl;
	}
	else
	{
		cout << "未考上大学" << endl;
	}*/

	//嵌套if语句
	//输入一个高考成绩,大于600视为一本,大于500视为二本,大于400视为三本,其他为未考上
	//一本分数中,大于700,考入北大,大于650,考入清华,大于600,考入人大
	int score3 = 0;
	cout << "请输入一个考试分数:" << endl;
	cin >> score3;
	cout << "您输入的分数为:" << score3 << endl;
	if (score3 > 600)
	{
		cout << "恭喜您考上一本大学" << endl;
		if (score3 > 700)
		{
			cout << "您能考入北京大学" << endl;
		}
		else if(score3 > 650)
		{
			cout << "您能考入清华大学" << endl;
		}
		else
		{
			cout << "您能考入人民大学" << endl;
		}

	}
	else if (score3 > 500)
	{
		cout << "考上二本大学" << endl;
	}
	else if (score3 > 400)
	{
		cout << "考上三本大学" << endl;
	}
	else
	{
		cout << "未考上大学" << endl;
	}
	system("pause");
	return 0;
}
  1. if案例——3个人比体重
#include <iostream>
using namespace std;
int main02()
{
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;
	cout << "请输入A的体重" << endl;
	cin >> num1;
	cout << "请输入B的体重" << endl;
	cin >> num2;
	cout << "请输入C的体重" << endl;
	cin >> num3;
	cout << "A的体重:" <<num1 <<endl;
	cout << "B的体重:" <<num2 <<endl;
	cout << "C的体重:" <<num3 <<endl;
	if (num1 > num2)
	{
		if (num1 > num3)
		{
			cout << "A最重" << endl;
		}
		else
		{
			cout << "C最重" << endl;
		}
	}
	else
	{
		if (num2 > num3)
		{
			cout << "B最重" << endl;
		}
		else
		{
			cout << "C最重" << endl;
		}
	}
	system("pause");
	return 0;
}
  1. 选择结构三目运算符
#include <iostream>
using namespace std;
int main03()
{
	//三目运算符
	//语法:表达式1?表达式2:表达式3
	//将变量大的值赋给c
	int a = 10;
	int b = 20;
	int c = 0;
	c = (a > b ? a : b);
	cout << "c=" << c << endl;
	//在三目运算符返回的是变量,可以继续赋值
	(a > b ? a : b) = 100;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}
  1. switch语句与if语句
#include <iostream>
using namespace std;
int main04()
{
	//switch语句
	//给电影打分
	//10~9 经典
	//8~7 很好
	//6~5 一般
	//5以下 烂片
	/*int score = 0;
	cout << "请您评分:" << endl;
	cin >> score;
	cout << "您打的分数是:" << score << endl;
	switch (score)
	{
	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;
	}*/
	//switch缺点:判断时候只能是整型或者字符型,不可以是区间
	//switch优点:结构清晰,执行效率高
	double score1 = 0.0;
	cout << "请您评分:" << endl;
	cin >> score1;
	cout << "您打的分数是:" << score1 << endl;
	if (score1 >= 9 && score1 <= 10)
	{
		cout << "经典电影" << endl;
	}
	else if (score1 >= 8 && score1 <= 9)
	{
		cout << "非常好电影" << endl;
	}
	else if (score1 >= 7 && score1 <= 8)
	{
		cout << "好电影" << endl;
	}
	else if (score1 >= 6 && score1 <= 7)
	{
		cout << "不错电影" << endl;
	}
	else if (score1 >= 5 && score1 <= 6)
	{
		cout << "一般电影" << endl;
	}
	else
	{
		cout << "烂片" << endl;
	}
	system("pause");
	return 0;
}
  1. 循环语句——while循环
#include <iostream>
using namespace std;
int main05()
{
	//循环结构
	//while循环语句
	//语法:while(循环条件){ 循环语句 }
	//在屏幕中打印0~9这10个数字
	int num = 0;
	/*cout << num << endl;
	num++;
	cout << num << endl;*/
	while (num < 10)
	{
		cout << num << endl;
		num++;
	}
	system("pause");
	return 0;
}
  1. 循环语句案例——猜体重游戏
#include <iostream>
using namespace std;
#include <ctime>
int main06()
{
	//添加随机数种子利用当前系统时间生成随机数,防止每次随机数都一样
	srand((unsigned int)time(NULL));
	//1.系统生成随机数
	int num = rand() % 100 + 1; //生成0~99的随机数
	//cout << num << endl;
	//2.玩家进行猜测
	int val = 0;
	cout << "输入您猜测的数据" << endl;
	while (1)
	{
		cin >> val;
		//3.判断玩家的猜测
		if (val > num)
		{
			cout << "猜测过大" << endl;
		}
		else if (val < num)
		{
			cout << "猜测过小" << endl;
		}
		else 
		{
			cout << "恭喜您猜对了" << endl;
			break;//break在循环中可以利用该关键字退出当前循环
		}
		//猜对  退出游戏
		//猜错  提示猜的结果  过大或过小  重新返回第第二步
	}
	system("pause");
	return 0;
}
  1. do…while循环
#include <iostream>
using namespace std;
int main07()
{
	//do...while循环语句
	//do{ 循环语句 } while(循环语句);
	//先执行一次
	//在屏幕中输出0~9这10个数字
	int num = 0;
	do 
	{
		cout << num << endl;
		num++;
	} while (num < 10);
	system("pause");
	return 0;
}
  1. do…while循环案例——水仙花数
#include <iostream>
using namespace std;
int main08()
{
	//水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身
	//例如:1^3+5^3+3^3 = 153;
	//利用do...while语句求出所有3位数中的水仙花数
	//获取个位:153 % 10 = 3  取模于10
	//获取十位:153 / 10 = 15 15 % 10 = 5  先整除于10,得到两位数,再取模于10
	//获取百位:153 / 100 = 1  整除于100
	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;
}
  1. for循环
#include <iostream>
using namespace std;
int main09()
{
	//for循环语句
	//满足循环条件,执行循环语句
	//语法:for(起始表达式;条件表达式;末尾循环体) { 循环语句; }
	//打印从0到9
	for (int i = 0;/*0*/ i < 10;/*1*/i++/*3*/)
	{
		cout << i << endl;/*2*/
	}/*执行顺序 0 1 2 3 1 2 3 1 2 3*/
	
	int j = 0;
	for (;;)
	{
		if (j >= 10)
		{
			break;
		}
		cout << j << endl;
		j++;
	}
	system("pause");
	return 0;
}
  1. for循环案例——敲桌子
#include <iostream>
using namespace std;
int main10()
{
	for (int i = 1; i <= 100; i++)
	{
		//7的倍数 取模于7=0
        //个位有7 取模于10=7
		//十位有7 除10取模于10=0
		if (i % 7 == 0 || i % 10 == 7 || i/10 == 7 )//如果是特殊数字,打印敲桌子
		{
			cout << "敲桌子" << endl;
		}
		else
		{
			cout << i << endl;
		}
	}
	system("pause");
	return 0;
}
  1. 嵌套循环
#include <iostream>
using namespace std;
int main11()
{
	//外层执行一次,内层执行一周
	//外层循环
	for (int i = 0; i < 10; i++)
	{
		//内层循环
		for (int j = 0; j < 10; j++)
		{
			cout << "* ";
		}cout << endl;
	}
	system("pause");
	return 0;
}
  1. 嵌套循环案例——乘法口诀表
#include <iostream>
using namespace std;
int main12()
{
	//列 * 行 = 计算结果
	//列 <= 行
	for (int i = 1; i <= 9; i++)
	{
		/*cout << i << endl;*/
		for (int j = 1; j <= i/*9*/; j++)
		{
			cout << j <<" * "<< i <<" = "<< j*i <<"\t";
		}cout << endl;
	}
	system("pause");
	return 0;
}
  1. 跳转语句——break
#include <iostream>
using namespace std;
int main13()
{
	//break语句
	//适用:1.出现在switch条件语句中,作用终止case并跳出switch
	/*cout << "请选择副本难度" << endl;
	cout << "1.普通" << endl;
	cout << "2.中等" << endl;
	cout << "3.困难" << endl;
	int select = 0;//创建选择结果的变量
	cin >> select;//等待用户输入
	switch (select)
	{
	case 1:cout << "您选择的是普通难度" << endl;
		break;
	case 2:cout << "您选择的是中等难度" << endl;
		break;
	case 3:cout << "您选择的是困难难度" << endl;
		break;
	default:break;
	}*/
	
	//     2.出现在循环语句中,作用是跳出当前循环语句
	/*for (int i = 0; i < 10; i++)
	{
		if (i == 5)
		{
			break;
		}
		cout << i;
	}cout << endl;*/
	
	//     3.出现在嵌套语句中,跳出最近的内层循环语句
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (j == 5)
			{
				break;//退出内层循环
			}
		     cout << "* ";
		}cout << endl;
	}
	system("pause");
	return 0;
}
  1. 跳转语句——continue
#include <iostream>
using namespace std;
int main14()
{
	//continue语句
	//在循环语句中,跳过本次尚未执行的语句,执行下一次循环。
	//执行到本行就不执行下面的语句,而执行下一次循环
	for (int i = 0; i <= 100; i++)
	{
		//如果是奇数输出,偶数不输出
		if (i % 2 == 0)
		{
			continue;//可以筛选条件,执行到此就不再往下执行,执行下一次循环
			//break会退出循环
		}
		cout << i << endl;
	}
	system("pause");
	return 0;
}
  1. 跳转语句——goto语句
#include <iostream>
using namespace std;
int main()
{
	//goto语句
	//执行到goto语句,会跳转到标记的位置
	cout << "1..." << endl;
	cout << "2..." << endl;
	goto FLAG;
	cout << "3..." << endl;
	cout << "4..." << endl;
	FLAG:
	cout << "5..." << endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值