鱼C论坛

 找回密码
 立即注册
查看: 1791|回复: 4

[已解决]关于嵌套的问题

[复制链接]
发表于 2023-2-11 11:45:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 1613551 于 2023-2-11 11:51 编辑

我想问一下这串代码存不存在嵌套,为什么答案说不存在嵌套,不是有两个if语句吗?
#include <stdio.h>
#include <math.h>
#include <stdio.h>
int main()
{
  int s=2,t=1,a=2,b=4;
  if(a>0)s=s+1;      
  if(a>b)t=s+t;
  else if(a=b)t=5;
  else t=2*s;
  printf("s=%d,t=%d",s,t);
  system("pause");
    return 0;
}
最佳答案
2023-2-11 11:48:46
因为前面那个 if 在分号那里就结束了,所以这是两个独立的 if

#include <stdio.h>
#include <math.h>
#include <stdio.h>
int main()
{
  int s=2,t=1,a=2,b=4;
  if(a>0)s=s+1;      
  if(a>b)t=s+t;
  else if(a=b)t=5;
  else t=2*s;
  printf("s=%d,t=%d",s,t);

  system("pause");
    return 0;
}

嵌套是这样的:

#include <stdio.h>
#include <math.h>
#include <stdio.h>
int main()
{
  int s=2,t=1,a=2,b=4;
  if(a>0){ s=s+1;      
  if(a>b)t=s+t;
  else if(a=b)t=5;
  else t=2*s; }
  printf("s=%d,t=%d",s,t);
  system("pause");
    return 0;
}
微信图片_20230202135252.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-11 11:46:15 | 显示全部楼层
本帖最后由 1613551 于 2023-2-11 11:48 编辑

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-11 11:48:46 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +3 鱼币

因为前面那个 if 在分号那里就结束了,所以这是两个独立的 if

#include <stdio.h>
#include <math.h>
#include <stdio.h>
int main()
{
  int s=2,t=1,a=2,b=4;
  if(a>0)s=s+1;      
  if(a>b)t=s+t;
  else if(a=b)t=5;
  else t=2*s;
  printf("s=%d,t=%d",s,t);

  system("pause");
    return 0;
}

嵌套是这样的:

#include <stdio.h>
#include <math.h>
#include <stdio.h>
int main()
{
  int s=2,t=1,a=2,b=4;
  if(a>0){ s=s+1;      
  if(a>b)t=s+t;
  else if(a=b)t=5;
  else t=2*s; }
  printf("s=%d,t=%d",s,t);
  system("pause");
    return 0;
}

点评

我很赞同!: 5.0
我很赞同!: 5
  发表于 2023-2-11 11:50

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
1613551 + 5 + 5 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-2-11 11:50:24 | 显示全部楼层
isdkz 发表于 2023-2-11 11:48
因为前面那个 if 在分号那里就结束了,所以这是两个独立的 if

#include

谢谢大佬,我懂了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 02:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表