#include<iostream>usingnamespace std;intmain03(){//三目运算符//语法:表达式1?表达式2:表达式3//将变量大的值赋给cint 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");return0;}
#include<iostream>usingnamespace std;intmain07(){//do...while循环语句//do{ 循环语句 } while(循环语句);//先执行一次//在屏幕中输出0~9这10个数字int num =0;do{
cout << num << endl;
num++;}while(num <10);system("pause");return0;}
do…while循环案例——水仙花数
#include<iostream>usingnamespace std;intmain08(){//水仙花数是指一个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 整除于100int 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");return0;}
#include<iostream>usingnamespace std;intmain10(){for(int i =1; i <=100; i++){//7的倍数 取模于7=0//个位有7 取模于10=7//十位有7 除10取模于10=0if(i %7==0|| i %10==7|| i/10==7)//如果是特殊数字,打印敲桌子{
cout <<"敲桌子"<< endl;}else{
cout << i << endl;}}system("pause");return0;}
嵌套循环
#include<iostream>usingnamespace std;intmain11(){//外层执行一次,内层执行一周//外层循环for(int i =0; i <10; i++){//内层循环for(int j =0; j <10; j++){
cout <<"* ";}cout << endl;}system("pause");return0;}
嵌套循环案例——乘法口诀表
#include<iostream>usingnamespace std;intmain12(){//列 * 行 = 计算结果//列 <= 行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");return0;}
跳转语句——break
#include<iostream>usingnamespace std;intmain13(){//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");return0;}
跳转语句——continue
#include<iostream>usingnamespace std;intmain14(){//continue语句//在循环语句中,跳过本次尚未执行的语句,执行下一次循环。//执行到本行就不执行下面的语句,而执行下一次循环for(int i =0; i <=100; i++){//如果是奇数输出,偶数不输出if(i %2==0){continue;//可以筛选条件,执行到此就不再往下执行,执行下一次循环//break会退出循环}
cout << i << endl;}system("pause");return0;}