c/c++分支与循环语句(内含水仙花数,乘法表,猜数字,找7有关数

2022/10/23 1:23:59

本文主要是介绍c/c++分支与循环语句(内含水仙花数,乘法表,猜数字,找7有关数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

分支语句:

1.if语句

/*int a = 4;*/
	单分支if:
	if (a > 1)
	{
		return 0;
	}


	双分支if:
	if (a > 1)
	{
		return 0;
	}
	else
	{
		cout<<a<<endl;
	}

	多条件if:
	if (a < 0)
	{
		cout << 0 << endl;	
	}
	else if (a < 1)
	{
		cout << 1 << endl;

	}
	else if (a < 2)
	{
		cout << 2 << endl;
	}
	else
	{
		cout << a << endl;
	}





	输入成绩判断考入的大学
	int w = 0;
	cout << "输入您的成绩
";
	cin >> w;
	cout << endl << endl;
	if (w >= 600)
	{
		cout << "考入一本!!
";
		if (w > 700)
			cout << "恭喜您考入北大
";
		else if (w > 650)
			cout << "清华" << endl;
		else
			cout << "人民大学" << endl;
	}
	else if (w >= 500)
		cout << "考入二本" << endl;
	else if (w >=400)
		cout << "考入三本" << endl;
	else
		cout << "大专人,大专魂
";


	三只小猪体重
	int a, b, c;
	cout << "输入体重" << endl;
	cin >> a >> b >> c;
	if (a > b)
	{
		if (b>c)
			cout << "a最重" << endl;
		else
		{
			if (a > c)
				cout << "a最重" << endl;
			else
				cout << "c最重
";
		}
	}
	else
	{
		if (a> c)
			cout << "b最重" << endl;
		else
		{
			if (b > c)
				cout << "b最重" << endl;
			else
				cout << "c最重
";
		}
	}

2.switch语句

switch语句
	int a;
	cout << "请输入月份
";
	cin >> a;
	switch (a)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		cout << "您输入的月份有三十一天
";
		break;
	case 2:
		cout << "您输入的月份有28天
";
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		cout << "您输入的月份有三十天
";
		break;
	default:
		cout << "输入有错
";
		break;
	}

三目运算符:

三目运算符:(a > b? true :false)
	int a, b;
	cout << "输入两数
";
	cin >> a >> b;
	cout << "最大的为:" << (a > b ? a : b) << endl;

循环语句:

while语句:

while语句:
int a = 5;
int b = 0;
while (b == 0)
{
	cout << "b的值为:" << b << endl;
	b++;
}

猜数字游戏:

猜数字的游戏
srand((int)time(0));
int a = rand() % 5 + 1;
cout << "请输入一个数:" << endl;
int b;
int c = 1;
cin >> b;
while (b != a)
{
	c++;
	if (b > a)
	{		cout << "输入大了请重新输入" << endl;
	cin >> b;

}
	else if (b < a)
	{
		cout << "输入小了请重新输入" << endl;
		cin >> b;
	}
}
cout << "终于对了,您尝试了" << c << "次" << endl;

do...while语句:

do...while语句   至少执行一次
int a =5;
do
{
	cout << "好好好
";
	a--;
} while (a > 0);




水仙花数
int a = 100;
do
{
	int b = 0;
	int c = 0;
	int d = 0;
	b = a / 100;
	c = (a / 10) % 10;
	d = a % 10;
	if (b * b * b + c * c * c + d * d * d == a)
	{
		cout << "  " << a;
	}
	a++;
} while (a >= 100 && a < 1000);

for循环语句:

for循环语句for(表达式1 ;表达式2 ;表达式3)    表达式1可以写在for函数上面,表达式三可以写在函数内部;表达式2一般不省略;
int a = 0;
for (; a < 5;)
{
	cout << "   " << a << endl;
	a++;

}


从一到一百,含⑦或者七的倍数输出,其余略过
int a = 1;
for (; a <= 100; a++)
{
	int b, c;
	b = a % 10;//个位
	c = a / 10;//shiwei
	if (c == 7 || b == 7 || a % 7 == 0)
		cout << "   " << a << endl;
}



嵌套循环乘法口诀表
int a = 1;
for (; a < 10; a++)
{
	int b = 1;
	for (; b <= a; b++)
	{
		cout << b << "*" << b << "=" << b * b<<"  ";
	}
	cout << endl;
}


int c = 9;
for (; c >0; c--)
{
	int b =1;
	for (; b<=c; b++)
	{
		cout << b << "*" << b << "=" << b * b << "  ";
	}
	cout << endl;
}

跳转语句:

1.break语句:

2.循环语句:

//break跳转语句:跳出  选择结构  或  循环结构

//continue在  循环语句  中跳过未执行的语句开始下一轮循环
//输出奇数
int a = 0;
for (; a <= 100; a++)
{
	if (a % 2==0)
		continue;
	cout << a << "  ";
}

3.goto语句:

//goto语句:破坏可读性;不建议使用
cout << "1" << endl;
cout << 2 << endl;
goto b;
cout << 3 << endl;
cout << 4 << endl;
b:cout << 5 << endl;
cout << 6 << endl;


这篇关于c/c++分支与循环语句(内含水仙花数,乘法表,猜数字,找7有关数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程