博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言if else语句_查找C程序的输出(如果为else语句)| 设置1
阅读量:2529 次
发布时间:2019-05-11

本文共 4026 字,大约阅读时间需要 13 分钟。

c语言if else语句

Find the output of the following programs,

查找以下程序的输出,

Program 1)

程序1)

#include 
int main(){
int x = 400, y, z; if (x >= 500) y = 400; z = 300; printf("%d %d\n", y, z); return 0;}

Output

输出量

32766 300

Explanation:

说明:

In the code, the condition x>=500 is false, so the variable y will not be assigned and the statement z=300 is written after the conditional statement, so it will be assigned with 300. Thus, the value of y will be a garbage value and value of z will be 300.

在代码中,条件x> = 500为假,因此将不分配变量y,并且在条件语句之后写入语句z = 300 ,因此将其分配为300。因此, y的值将为垃圾值和z的值为300。

Program 2)

程序2)

#include 
int main(){
int p = 800, q, r; if (p >= 700) q = 600; r = 500; printf("%d %d\n", q, r); return 0;}

Output

输出量

600 500

Explanation:

说明:

In the code, the condition p>=700 is true, so the variable q will be assigned with the value 600 and the statement r=500 is written after the conditional statement, so it will be assigned with 500. Thus, the value of q will be a 600 value and value of r will be 500.

在代码中,条件p> = 700为true,因此将为变量q分配值600,并在条件语句后写入语句r = 500 ,因此将其分配为500。因此, q将是600的值,而r的值将是500。

Program 3)

程序3)

#include 
int main(){
int a = 30, b = 40; if (a == b); printf("%d %d\n", a, b); return 0;}

Output

输出量

30 40

Explanation:

说明:

In the code, the condition if(a==b); is terminated with semicolon so the statement printf("%d %d\n",a,b); will not be consider as a body of the if statement. Thus, the program will print the value of a and b.

在代码中,条件为if(a == b); 以分号终止,因此语句printf(“%d%d \ n”,a,b); 不会被视为if语句的主体。 因此,程序将打印a和b的值。

Program 4)

程序4)

#include 
int main(){
int e = 4; float f = 4.0; if (e == f) {
printf("E and F are equal\n"); } else {
printf("E and F are not equal"); } return 0;}

Output

输出量

E and F are equal

Explanation:

说明:

In the code, variable e is an integer type and variable f is a float type, while comparing them with an if statement (if(e==f)), the value of f will be truncated to an integer (due to implicit type conversion). Thus, the condition will be true and "E and F are equal" will be printed.

在代码中,变量e是整数类型,变量f是浮点类型,将它们与if语句( if(e == f) )进行比较时, f的值将被截断为整数(由于隐式类型)转换)。 因此,条件为真, 并且将打印“ E和F相等”

Program 5)

程序5)

#include 
int main(){
int p = 4, q, r; q = p = 15; r = p < 15; printf("p = %d q = %d r = %d\n", p, q, r); return 0;}

Output

输出量

p = 15 q = 15 r = 0

Explanation:

说明:

In the code, the statement q = p = 15; is assigning 15 to the variables p and q and the statement r = p<15; is assigning 0 to the variable r because p is not less than 15 (condition is false). Thus, the value of p, q and r will be 15, 15, and 0.

在代码中,语句q = p = 15; 将变量p和q赋值为15,并且语句r = p <15; 因为p不小于15(条件为假),所以将0赋给变量r 。 因此, p , q和r的值将分别为15、15和0。

Program 6)

计划6)

#include 
int main(){
int i = 65; char j = 'A'; if (i == j) {
printf("This place is beautiful\n"); } else {
printf("This place is not beautiful\n"); } return 0;}

Output

输出量

This place is beautiful

Explanation:

说明:

The character variable stores the ASCII code of the given character (it's also a number type of variable). Thus, the value of j will be 65 (The ASCII Code of 'A' is 65). So the condition if(i==j) will be true and "This place is beautiful" will be printed.

字符变量存储给定字符的ASCII码(它也是变量的数字类型)。 因此, j的值为65(“ A”的ASCII码为65)。 因此条件if(i == j)将为true,并且将打印“这个地方很漂亮”

Program 7)

计划7)

#include 
int main(){
float p = 13.25, q = 14.5; if (p = q) {
printf("Hello\n"); } return 0;}

Output

输出量

Hello

Explanation:

说明:

In the statement if(p=q), p=q is not a comparison operation, we used = (assignment operator), thus the value of q will be assigned into p and the statement will be evaluated to 14.5 which is a non-zero value and conditional will be true.

在语句if(p = q) , p = q不是比较运算的情况下,我们使用= (赋值运算符),因此q的值将分配给p,并且该语句的计算结果为14.5 ,这不是零值和条件将为真。

Recommended posts

推荐的帖子

翻译自:

c语言if else语句

转载地址:http://tytzd.baihongyu.com/

你可能感兴趣的文章
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>