if,三目运算符,switch,while,do...while,for,嵌套循环,break,continue,goto

类型和C大致相同,此处仅仅列举语法格式和部分例题:

________________________

1.if

格式与C相同:

if(){

}

else if(){

}

else{

};

嵌套也相同:

if(){

  if(){

  };

};

例题:输入三个数字,判断哪个数字最大:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     int a = 0;
 7     int b = 0;
 8     int c = 0;
 9     cout << "请输入A的值" << endl;
10     cin >> a;
11     cout << "请输入B的值" << endl;
12     cin >> b;
13     cout << "请输入C的值" << endl;
14     cin >> c;
15     if(a>b){
16         if(a>c){
17             cout << "最大的值是A,为:" << a << endl;
18         }
19         else{
20             cout << "最大的值是C,为:" << c << endl;
21         }
22     }
23     else{
24         if(b>c){
25             cout << "最大的值是B,为:" << b << endl;
26         }
27         else{
28             cout << "最大的值是C,为:" << c << endl;
29         }
30     }
31     system("pause");
32     return 0;
33 }

_______________________________

2.三目运算符(类似C):

语法:a和b比较,a>b,返回a,否则返回b(真返回第一个,假返回第二个)

 

此处定义int a = 10, b = 20, c = 0;

 

 

 在C++中,三目运算返回的变量可以继续赋值,如:

 

 此时输出a = 10;b = 100;

______________________________________________

3.switch(同C):

语法:

 

 

 ——————————————————————————————

4.while(同C):

语法:

 

 

 

 

 

 例如:

while(1){};

这样就会一直执行空语句。。。

例子:猜数字(输入一个数,猜错则提示数字过大或者过小,猜对恭喜玩家并退出游戏)

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     //此处是随机生成1-100的随机数,种子固定,所以不算是随机
 7     //根据语法:要取得 [a,b] 的随机整数,使用 (rand() % (b-a+1))+ a;
 8     int num = rand() % 100 + 1; //a + rand() % n;其中的 a 是起始值,n 是整数的范围
 9     int a = 0;
10     cout << "开始猜数,请输入一个1-100的整数:" << endl;
11     cin >> a;
12     while(a != num){
13         if(a > num){
14             cout << "猜错啦,数值偏大噢" << endl;
15             cout << "再来一次吧,请输入一个1-100的整数:" << endl;
16             cin >> a;
17         }
18         else{
19             cout << "猜错啦,数值偏小噢" << endl;
20             cout << "再来一次吧,请输入一个1-100的整数:" << endl;
21             cin >> a;
22         }
23     }
24     cout << "猜对啦,正确答案就是:" << num << endl;
25     system("pause");
26     return 0;
27 }

如果要实现真正的随机,需要:

头文件加上:#include <ctime>

主函数第一行加上:srand((unsigned int)time(NULL));

获取当前系统时间作为种子生成随机数。。。

——————————————————————————————

5.do...while(同C):

 

 

 

 

 

 

 

 

 =====================================================

区分while和do..while:

      

=======================================================

新经典:求所有三位数的水仙花数(三位数的每个位上的数的立法之和等于他本身):

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     int num = 100;
 7     int i = 0;
 8     while(num < 1000){
 9         int a = 0;
10         int b = 0;
11         int c = 0;
12 
13         a = num % 10;
14         b = num / 10 % 10;
15         c = num / 100;
16 
17         if(a*a*a+b*b*b+c*c*c == num)
18         {
19             cout << num << endl;
20             i++;
21         }
22         num++;
23     }
24     // do{
25     //     int a = 0;
26     //     int b = 0;
27     //     int c = 0;
28     //     a = num % 10;
29     //     b = num / 10 % 10;
30     //     c = num /100;
31     //     if (a*a*a+b*b*b+c*c*c == num)
32     //     {
33     //         cout << num << endl;
34     //         i++;
35     //     }
36     //     num++;
37     // } while (num<1000);
38     
39     cout << "三位数内的水仙花数共有:" << i << "" << endl;
40     system("pause");
41     return 0;
42 }

————————————————————————————————————————————————————————————————

6.for(同C):

 语法:

 

 

 

 

 

 例子:从1到100,含7的数字输出一次你好,其余数字直接打印输出

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     for (int i = 1; i <= 100; i++){
 7         if (i/10 == 7 || i%10 == 7 || i%7 == 0){
 8             cout << "你好" << endl;
 9         }
10         else{
11             cout << i << endl;
12         }
13     }
14     
15     system("pause");
16     return 0;
17 }

——————————————————————————————————————————————————————————

7.嵌套循环(同C):循环体中再嵌套一层循环。

经典案例:99乘法表

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6     for(int i = 1; i < 10; i++){
 7         for (int j = 1; j <= i; j++){
 8             cout << j << "x" << i << "=" << i*j << " "; //不换行,但是留空
 9         }
10         cout << endl;   //换行
11     }
12     
13     system("pause");
14     return 0;
15 }

————————————————————————————————————————————————————

8.break语句:跳出选择结构或者循环结构(同C)

 switch不采用break可能会出现case穿透。。。

——————————————————————————————————————————————————————————————

9.continue语句:在循环结构中,跳过本次循环余下未执行的语句,继续执行下一次循环(同C)

 

 此处为输出0-100的除以2取余不为0的数(换个说法就是奇数)。

——————————————————————————————————————————————————————————————

10.goto跳转语句:无条件跳转到标记位置(同C)[少用,以免出现程序流程混乱]

语法:

 

 如图:

 

 输出:

1

5

posted @ 2022-10-30 20:53  Morning枫  阅读(114)  评论(0编辑  收藏  举报