首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2019二级C题库及解析(7)

2019二级C题库及解析(7)

作者头像
用户6755376
发布2020-06-04 17:39:36
3010
发布2020-06-04 17:39:36
举报

5.选择结构 if 嵌套

#include  < stdio .h >
main ( )
{  
    float  c=3.0 , d=4.0;
    if ( c>d ) c=5.0;
    else  if ( c==d ) c=6.0;
          else  c=7.0;
 printf ( “%.1f\n”,c ) ;
}

运行结果为:7.0

此题为if...else...语句的嵌套,第二if...else...作为第一个if...else...语句else部分的复合语句。

若表达式c>d成立,则执行c=5.0;

否则(表达式c>d不成立)

若表达式c==d成立,则执行c=6.0;

否则,执行c=7.0;

输出c中的值

3.0小于4.0,因此表达式c>d不成立,执行第二个if…else…。

3.0不等于4.0,因此表达式c==d不成立,执行c=7.0,将7.0赋给c, 覆盖掉c中的3.0,此时c中的值为7.0

输出此时的c中的值

6. 选择结构 if 嵌套

#include <stdio.h>
main()
{   int m;
    scanf("%d", &m); 
    if (m >= 0) 
    {    if (m%2 == 0)  
            printf("%d is a positive even\n", m);
         else 
            printf("%d is a positive odd\n", m); 
     }
     else
     {   if (m % 2 == 0)  
             printf("%d is a negative even\n", m);  
         else
             printf("%d is a negative odd\n", m); 
      }
}

若键入-9,则运行结果为: -9 is a negative odd

7. 循环结构

#include <stdio.h>
main()
{ 
    int num=0;
    while(num<=2)
    {  num++;
       printf("%d\n",num);
    }
}

运行结果为:

1

2

3

当循环条件num<=2成立的时候,执行循环体{ num++;printf("%d\n",num);}中的语句。

循环初值num为0;

循环条件num<=2成立

第1次循环:执行num++;即将num中的值加1,执行后num为1;

执行printf("%d\n",num);在屏幕上输出num中的值,即输出1,之后换行

此时num中的值为1,循环条件num<=2成立

第2次循环:执行num++;即将num中的值加1,执行后num为2;

执行printf("%d\n",num);在屏幕上输出num中的值,即输出2,之后换行

此时num中的值为2,循环条件num<=2成立

第3次循环:执行num++;即将num中的值加1,执行后num为3;

执行printf("%d\n",num);在屏幕上输出num中的值,即输出3,之后换行

此时num中的值为3,循环条件num<=2不成立,结束循环。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 计算机二级C语言 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档