首页题目详情
以下哪项是正确的 if-else 语句嵌套格式?

A.if (a) { if (b) printf("yes"); } else { printf("no"); }
B.if (a) { else if (b) printf("yes"); } else { printf("no"); }
C.if (a) { if (b) { printf("yes"); } else { printf("no"); } } else { printf("error"); }
D.if (a) { if (b); else printf("no"); } else { printf("error"); }
优质解答
答案
C
解析
选项C正确地展示了嵌套 if-else 语句的结构,每个 if 或 else 语句都有完整的代码块。选项B的语法错误在于 else 不能单独出现在 if 后面,而没有大括号。选项D虽然语法正确,但结构不够规范,容易引起歧义。
C语言程序设计单选题中等AI生成