单独如果vs if else [英] single if vs if else

查看:69
本文介绍了单独如果vs if else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题源于以下网址的一些代码

http://www.cplusplus.com/reference/c...dio/fread.html


在代码示例中,他们有以下单个if语句


pFile = fopen(" myfile.bin"," rb");

if(pFile == NULL ){fputs(" File error",stderr); exit(1);}

为什么使用单个if语句而不是if-else?只是好奇

因为我看到这在C代码中出现了很多。即使用if-else的单个if语句vs


The question stems from some code at the following url

http://www.cplusplus.com/reference/c...dio/fread.html

In the code example they have a single if statement for the following

pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
Why use a single if statement as opposed to if-else? Just curious
because I see this appear a lot in C code. Ie a single if statement vs
using if-else.

推荐答案

Chad< cd ***** @ gmail.com写的:
Chad <cd*****@gmail.comwrites:

pFile = fopen(" myfile.bin"," rb");

if(pFile == NULL){fputs(" File error",stderr); exit(1);}


为什么使用单个if语句而不是if-else?只是好奇

因为我看到这在C代码中出现了很多。即使用if-else,单个if语句vs


pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
Why use a single if statement as opposed to if-else? Just curious
because I see this appear a lot in C code. Ie a single if statement vs
using if-else.



我认为你粗略地比较以下内容:

A;

if(B ){

C;

退出(1);

}

D;

对此:

A;

如果(B){

C;

退出(1) ;

}否则{

D;

}


许多程序员更喜欢前者因为D,这是函数的整个剩余部分的

,没有缩进到

右边。在这种情况下向远处推动代码浪费

水平空间没有太多的好处,我可以看到。

-

" C has它的问题,但是从头开始设计的语言也会有一些,

我们知道C'的问题。

--Barne Stroustrup

I think that you are, roughly speaking, comparing the following:
A;
if (B) {
C;
exit(1);
}
D;
against this:
A;
if (B) {
C;
exit(1);
} else {
D;
}

Many programmers would prefer the former because D, which is the
entire rest of the function, is not indented as far to the
right. Pushing code far to the right in this case wastes
horizontal space without much benefit that I can see.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C''s problems."
--Bjarne Stroustrup


6月25日,7:58 * pm,Ben Pfaff< b ... @ cs.stanford.eduwrote:
On Jun 25, 7:58*pm, Ben Pfaff <b...@cs.stanford.eduwrote:

Chad< cdal ... @ gmail.comwrites:
Chad <cdal...@gmail.comwrites:

pFile = fopen(" myfile.bin"," rb");

if(pFile == NULL){fputs(" File error",stderr); exit(1);}
pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}


为什么使用单个if语句而不是if-else?只是好奇

因为我看到这在C代码中出现了很多。即使用if-else,单个if语句vs


Why use a single if statement as opposed to if-else? Just curious
because I see this appear a lot in C code. Ie a single if statement vs
using if-else.



我认为你粗略地比较以下内容:

* * * * A;

* * * * if(B){

* * * * * * * * C;

* * * * * * * * exit(1);

* * * *}

* * * * D;

对此:

* * * * A ;

* * * * if(B){

* * * * * * * * C;

* * * * * * * *退出(1);

* * * *}否则{

* * * * * * * * D;

* * * *}


许多程序员更喜欢前者,因为D,这是函数的其余部分,而不是缩进到

对。 *在这种情况下向右推动代码浪费

水平空间我没看到太多好处。

-


I think that you are, roughly speaking, comparing the following:
* * * * A;
* * * * if (B) {
* * * * * * * * C;
* * * * * * * * exit(1);
* * * * }
* * * * D;
against this:
* * * * A;
* * * * if (B) {
* * * * * * * * C;
* * * * * * * * exit(1);
* * * * } else {
* * * * * * * * D;
* * * * }

Many programmers would prefer the former because D, which is the
entire rest of the function, is not indented as far to the
right. *Pushing code far to the right in this case wastes
horizontal space without much benefit that I can see.
--


是的,问题措辞不当。我仍然有*问题*制作

我自己清楚。无论如何,我认为可能会有更多的事情继续发生。也许在工作中有一些更广泛的概念。


Yes, the question was poorly worded. I still have *issues* making
myself clear. Anyhow, I thought that there was maybe something more
going on. Ie maybe there was some kind of broader concept at work.


Chad写道:
Chad wrote:

>

问题源于以下网址中的一些代码

http://www.cplusplus.com/reference/c...dio/fread.html


代码例如,他们对以下内容有一个if语句


pFile = fopen(" myfile.bin"," rb");

if(pFile == NULL){fputs(" File error",stderr);退出(1);}


为什么使用单个if语句而不是if-else?只是好奇

因为我看到这在C代码中出现了很多。即使用if-else,单个if语句vs


>
The question stems from some code at the following url

http://www.cplusplus.com/reference/c...dio/fread.html

In the code example they have a single if statement for the following

pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

Why use a single if statement as opposed to if-else? Just curious
because I see this appear a lot in C code. Ie a single if statement vs
using if-else.



更简单的代码是:


if(!(pFile = fopen(" myfile.bin," rb" ))){

fputs(文件错误,stderr);

退出(EXIT_FAILURE);

}


请注意,exit(1)不可移植。没有其他需要,因为

失败,程序无法继续。


-

[邮件]:Chuck F (cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。

**发自 http://www.teranews.com **

Simpler code is:

if (!(pFile = fopen("myfile.bin, "rb"))) {
fputs("File error", stderr);
exit(EXIT_FAILURE);
}

Note that exit(1) is not portable. No else is needed, because on
failure the program does not continue.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **


这篇关于单独如果vs if else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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