c语句拼图 [英] puzzle on c statement

查看:65
本文介绍了c语句拼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我们可以将下面提到的4行语句写成1行语句吗? 。


if(flag)// flag是一个int变量

返回1;

else

返回0;


我不想使用三元运算符。请告诉我,有没有其他方法可以在单一陈述上进行相同的操作。


谢谢,

Sridhar.D

Hi All.

Can we write the below mentioned 4 lines statements into 1 line statements? .

if(flag) // flag is a int variable
return 1;
else
return 0;

I dont want to use ternary operater. please let me know , is there any other way to do the same operation on single statement.

Thanks,
Sridhar.D

推荐答案

是的,检查所有逻辑运算符。
Yes there is examine all the logical operators.


您的目标是将此代码段全部放在源文件的一行或实现它在一个语句中?


在大多数情况下,您可以在不改变代码含义的情况下插入或删除C源代码中的换行符。这是对你的问题的一个微不足道的答案。


现在,我可以想到三种方法在一个语句中完成这个代码片段。 (其中一种方法是使用三元运算符;您不希望使用它。)


仅仅因为你可以做某事并不意味着你应该这样做。就我而言,你不应该将这个片段组合成一个语句,除非该形式使得更容易理解该函数的作用。很大程度上取决于返回值的含义。此函数是否返回与逻辑运算符一起使用的true / false布尔值;或者此函数是否返回与算术运算符一起使用的整数值?
Is your goal to put this code snippet all on one line of the source file or to implement it in a single statement?

In most cases, you can insert or remove newlines from C source code without altering the meaning of the code. This is a trivial answer to your question.

Off-hand, I can think of three ways to accomplish this code snippet in a single statement. (One of these ways is with the ternary operator; which you prefer not to use.)

Just because you can do something doesn''t mean that you should. As far as I''m concerned, you shouldn''t combine this snippet into a single statement unless that form makes it easier to understand what the function does. A lot depends on the meaning of the return values. Does this function return a true/false boolean value that is used with logical operators; or does this function return an integer value that is used with arithmetic operators?


任何人都可以给出答案吗?
can anyone please give the answer?


这篇关于c语句拼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆