目录
1.三目运算符
语法: 表达式1 ? 表达式2 :表达式3
含义: 若1的值为真,则执行2
若1的值为假则执行3.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 100;
int b = 200;
int c = 0;
a > b ? c = 1 : c = 0;//比较ab的值,成立c=1 不成立c=0
std::cout << "c的值为 =" << c << std::endl;
//三目运算符返回值为变量,可以继续赋值
std::cout << "ab中最大的数 =" <<(a > b ? a : b) << std::endl;
system("pause");
return 0;
}
2.敲桌子案例
#include <iostream>
#include <string>
using namespace std;
int main()
{
for (int i = 0; i <=100; i++)
{
if (i % 7 == 0 || i % 7 ==7 || i / 10 == 7)//7的倍数,各位或者十位有7
{
std::cout << "敲桌子" << std::endl;
}
else
{
std::cout << i << std::endl;
}
}
system("pause");
return 0;
}
3.continue
作用:直接进入新的循环。
#include <iostream>
#include <string>
using namespace std;
int main()
{
for (int i = 0; i <= 100; i++)
{
if (i % 7 == 0 || i % 7 == 7 || i / 10 == 7) //7的倍数,各位或者十位有7
{
continue;
std::cout << "敲桌子" << std::endl;
}
else
{
std::cout << i << std::endl;
}
}
system("pause");
return 0;
}
4. goto
无条件跳到标记位置
goto 标记名;
标记名: